thrownewError("Unibabel.js is required to convert between buffers and binary strings");
}
if('string'===typeofkey){
thrownewError("use one of Unibabel.utf8ToBuffer(key), Unibabel.base64ToBuffer(key), or Unibabel.hexToBuffer(key) before passing to sha1Hmac(key, bytes)");
}
varUnibabel=window.Unibabel;
if(window.crypto){
returnwindow.crypto.subtle.importKey(
"raw"
,key
,{name:"HMAC"
,hash:{name:"SHA-1"}
}
,false
,["sign","verify"]
)
/*
returncrypto.subtle.importKey(
"jwk",//can be "jwk" or "raw"
{//this is an example jwk key, "raw" would be an ArrayBuffer
kty:"oct",
k:"Y0zt37HgOx-BY7SQjYVmrqhPkO44Ii2Jcb9yydUDPfE",
alg:"HS256",
ext:true,
},
{//this is the algorithm options
name:"HMAC",
hash:{name:"SHA-256"},//can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
//length: 256, //optional, if you want your key length to differ from the hash function's block length
},
false,//whether the key is extractable (i.e. can be used in exportKey)
["sign","verify"]//can be any combination of "sign" and "verify"
)
*/
.then(function(key){
returnwindow.crypto.subtle.sign(
{name:"HMAC"}
,key// from generateKey or importKey above
,newUint8Array(bytes)// ArrayBuffer of data you want to sign
)
.then(function(signature){
// returns an ArrayBuffer containing the signature