bitmessage-js/lib/platform.browser.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-12-13 18:56:14 +00:00
/**
2015-01-03 10:29:22 +00:00
* Browser implementation of platform-specific routines.
2014-12-30 17:00:28 +00:00
* @see {@link http://www.w3.org/TR/WebCryptoAPI/}
* @see {@link http://caniuse.com/#feat=cryptography}
* @see {@link https://sites.google.com/a/chromium.org/dev/blink/webcrypto}
2014-12-13 18:56:14 +00:00
*/
"use strict";
2014-12-18 16:47:18 +00:00
require("es6-promise").polyfill();
var assert = require("assert");
2014-12-18 16:47:18 +00:00
var ripemd160 = require("ripemd160");
2014-12-18 20:54:27 +00:00
// 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
2014-12-19 12:34:33 +00:00
exports.sha256 = function(buf) {
return subtle.digest({name: "SHA-256"}, buf).then(function(arr) {
return new Buffer(new Uint8Array(arr));
});
};
2014-12-18 16:47:18 +00:00
exports.ripemd160 = function(buf) {
2014-12-27 21:04:23 +00:00
// XXX(Kagami): RIPEMD is not defined in WebCryptoAPI so we provide it
// using pure JS third-party implementation.
2014-12-18 16:47:18 +00:00
return Promise.resolve(ripemd160(buf));
};
2014-12-26 17:17:01 +00:00
exports.randomBytes = function(size) {
var arr = new Uint8Array(size);
window.crypto.getRandomValues(arr);
return new Buffer(arr);
};