Skip to content

Instantly share code, notes, and snippets.

@WyohKnott
Created March 14, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WyohKnott/151edcc3e6c53679aacf to your computer and use it in GitHub Desktop.
Save WyohKnott/151edcc3e6c53679aacf to your computer and use it in GitHub Desktop.
function signatureMethod (consumerSecret, tokenSecret, signatureBaseString) {
var encoder = new TextEncoder('utf-8');
var passphrase;
consumerSecret = consumerSecret;
tokenSecret = tokenSecret || '';
passphrase = consumerSecret + '&' + tokenSecret;
return crypto.subtle.importKey(
"raw",
encoder.encode(passphrase),
{
name: "HMAC",
hash: {
name: "SHA-1"
}
},
false,
["sign", "verify"]
).then(function (key) {
return crypto.subtle.sign(
{
name: "HMAC"
},
key,
encoder.encode(signatureBaseString)
);
}).then(function (signature) {
var result = [];
(new Uint8Array(signature)).forEach(function (char) {
result.push(String.fromCharCode(char));
});
return result.join('');
}).catch(function (err) {
console.error('Could not sign: ', err);
});
}
signatureMethod("blah blah blah", "something", "long signature string to")
.then((response) => console.log("sign", response));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment