Browse Source

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.
patch-2
Ryan X. Charles 11 years ago
parent
commit
ba59d97a73
  1. 6
      BIP32.js
  2. 1
      browser/build.js
  3. 1
      package.json
  4. 1
      test/index.html
  5. 14
      util/util.js

6
BIP32.js

@ -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);

1
browser/build.js

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

1
package.json

@ -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",

1
test/index.html

@ -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>

14
util/util.js

@ -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,6 +14,19 @@ if (inBrowser) {
var sha256 = exports.sha256 = function(data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};
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');

Loading…
Cancel
Save