bitmessage-js/lib/crypto.browser.js

33 lines
995 B
JavaScript
Raw Normal View History

2014-12-13 18:56:14 +00:00
/**
* Browser version of the crypto for Bitmessage JS implementation.
2014-12-18 16:47:18 +00:00
*
* Documentation: <http://www.w3.org/TR/WebCryptoAPI/>
* Browsers support: <http://caniuse.com/#feat=cryptography>
* Blink implementation details: <https://sites.google.com/a/chromium.org/dev/blink/webcrypto>
*
* @module bitmessage/crypto.browser
2014-12-13 18:56:14 +00:00
*/
"use strict";
2014-12-18 16:47:18 +00:00
require("es6-promise").polyfill();
var ripemd160 = require("ripemd160");
2014-12-18 20:54:27 +00:00
var assert = require("./utils").assert;
// Support `webkit` prefix for Safari (not tested yet).
// TODO(Kagami): Try to support IE11.
var subtle = window.crypto.subtle || window.crypto.webkitSubtle;
assert(subtle, "WebCryptoAPI is not supported");
2014-12-18 16:47:18 +00:00
exports.sha512 = function(buf) {
2014-12-18 20:54:27 +00:00
return subtle.digest({name: "SHA-512"}, buf).then(function(arr) {
return new Buffer(new Uint8Array(arr));
});
};
2014-12-18 16:47:18 +00:00
exports.ripemd160 = function(buf) {
// XXX(Kagami): No support in browsers via Web Crypto API currently,
// so use module.
return Promise.resolve(ripemd160(buf));
};