Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

3 changed files with 14 additions and 39 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "botp", "name": "botp",
"main": "index.js", "main": "index.js",
"version": "3.0.2", "version": "1.0.0",
"homepage": "https://github.com/Daplie/botp", "homepage": "https://github.com/Daplie/botp",
"authors": [ "authors": [
"AJ ONeal <aj@daplie.com>", "AJ ONeal <aj@daplie.com>",

View File

@ -12,19 +12,8 @@ exports.sha1Hmac = function (key, bytes) {
var Unibabel = window.Unibabel; var Unibabel = window.Unibabel;
function useForge() { if (window.crypto) {
var forge = window.forge; return window.crypto.subtle.importKey(
var hmac = forge.hmac.create();
var digest;
hmac.start('sha1', Unibabel.bufferToBinaryString(key));
hmac.update(Unibabel.bufferToBinaryString(bytes));
digest = hmac.digest().toHex();
return window.Promise.resolve(digest);
}
function useWebCrypto() {
return (window.crypto.subtle||window.crypto.webkitSubtle).importKey(
"raw" "raw"
, key , key
, { name: "HMAC" , { name: "HMAC"
@ -51,10 +40,10 @@ exports.sha1Hmac = function (key, bytes) {
["sign", "verify"] //can be any combination of "sign" and "verify" ["sign", "verify"] //can be any combination of "sign" and "verify"
) )
*/ */
.then(function (cryptoKey) { .then(function (key) {
return (window.crypto.subtle||window.crypto.webkitSubtle).sign( return window.crypto.subtle.sign(
{ name: "HMAC" } { name: "HMAC" }
, cryptoKey // from generateKey or importKey above , key // from generateKey or importKey above
, new Uint8Array(bytes) // ArrayBuffer of data you want to sign , new Uint8Array(bytes) // ArrayBuffer of data you want to sign
) )
.then(function(signature){ .then(function(signature){
@ -63,29 +52,15 @@ exports.sha1Hmac = function (key, bytes) {
}); });
}); });
} }
if (window.crypto) {
// WebCrypto is so unreliable right now... ugh...
try {
return useWebCrypto().then(function (result) {
return result;
}, function (err) {
console.warn(err);
console.warn(err.stack);
console.warn("WebCrypto failed, trying forge.js");
return useForge();
});
} catch(e) {
console.warn(e);
console.warn(e.stack);
console.warn("WebCrypto threw exception, trying forge.js");
return useForge();
}
}
else if (window.forge) { else if (window.forge) {
return useForge(); var forge = window.forge;
var hmac = forge.hmac.create();
var digest;
hmac.start('sha1', key);
hmac.update(Unibabel.bufferToBinaryString(bytes));
digest = hmac.digest().toHex();
return window.Promise.resolve(digest);
} }
else { else {
throw new Error("WebCrypto or forge.js is required to create a sha1 hmac"); throw new Error("WebCrypto or forge.js is required to create a sha1 hmac");