make things work in the browser by fixing sha512

...had to use jsSHA package to do SHA512 in the browser. Unfortunately it is
quite slow compared to node.
This commit is contained in:
Ryan X. Charles 2014-03-22 13:49:58 -07:00
parent 22b57feb7b
commit ba59d97a73
5 changed files with 20 additions and 5 deletions

View File

@ -290,8 +290,7 @@ BIP32.prototype.derive_child = function(i) {
var il = new BigInteger(hash.slice(0, 64), 16);
var ir = Crypto.util.hexToBytes(hash.slice(64, 128));
*/
var hmac = crypto.createHmac('sha512', this.chain_code);
var hash = hmac.update(data).digest();
var hash = coinUtil.sha512hmac(data, this.chain_code);
var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
var ir = hash.slice(32, 64);
@ -317,8 +316,7 @@ BIP32.prototype.derive_child = function(i) {
var ir = Crypto.util.hexToBytes(hash.slice(64, 128));
*/
var data = Buffer.concat([this.eckey.public, ib]);
var hmac = crypto.createHmac('sha512', this.chain_code);
var hash = hmac.update(data).digest();
var hash = coinUtil.sha512hmac(data, this.chain_code);
var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
var ir = hash.slice(32, 64);

View File

@ -24,6 +24,7 @@ var pack = function (params) {
var modules = [
'Address',
'BIP32',
'Block',
'Bloom',
'Buffers.monkey',

View File

@ -47,6 +47,7 @@
"postinstall": "node browser/build.js -a"
},
"dependencies": {
"jssha": "=1.5.0",
"soop": "=0.1.5",
"base58-native": "=0.1.3",
"bindings": "=1.1.1",

View File

@ -17,6 +17,7 @@
<script src="test.Address.js"></script>
<script src="test.basic.js"></script>
<script src="test.BIP32.js"></script>
<script src="test.Block.js"></script>
<script src="test.Bloom.js"></script>
<script src="test.Connection.js"></script>

View File

@ -3,6 +3,7 @@ var bignum = require('bignum');
var Binary = require('binary');
var Put = require('bufferput');
var buffertools = require('buffertools');
var jssha = require('jssha');
var browser;
var inBrowser = !process.versions;
if (inBrowser) {
@ -13,7 +14,20 @@ if (inBrowser) {
var sha256 = exports.sha256 = function(data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};
var ripe160 = exports.ripe160 = function(data) {
var sha512hmac = exports.sha512hmac = function (data, key) {
if (inBrowser) {
var j = new jssha(data.toString('hex'), 'HEX');
var hash = j.getHMAC(key.toString('hex'), "HEX", "SHA-512", "HEX");
hash = new Buffer(hash, 'hex');
return hash;
};
var hmac = crypto.createHmac('sha512', key);
var hash = hmac.update(data).digest();
return hash;
};
var ripe160 = exports.ripe160 = function (data) {
if (!Buffer.isBuffer(data)) {
throw new Error('arg should be a buffer');
}