Wei Lu
11 years ago
15 changed files with 182 additions and 88 deletions
@ -0,0 +1,40 @@ |
|||
// Crypto, crypto, where art thou crypto
|
|||
var CryptoJS = require('crypto-js') |
|||
var crypto = require('crypto') |
|||
var convert = require('./convert') |
|||
|
|||
function hash160(buffer) { |
|||
if (!Buffer.isBuffer(buffer)) buffer = new Buffer(buffer) // FIXME: transitionary
|
|||
|
|||
var step1 = sha256(buffer) |
|||
|
|||
var step2a = convert.bytesToWordArray(step1) |
|||
var step2b = CryptoJS.RIPEMD160(step2a) |
|||
|
|||
return new Buffer(convert.wordArrayToBytes(step2b)) |
|||
} |
|||
|
|||
function hash256(buffer) { |
|||
if (!Buffer.isBuffer(buffer)) buffer = new Buffer(buffer) // FIXME: transitionary
|
|||
|
|||
return sha256(sha256(buffer)) |
|||
} |
|||
|
|||
function sha1(buffer) { |
|||
if (!Buffer.isBuffer(buffer)) buffer = new Buffer(buffer) // FIXME: transitionary
|
|||
|
|||
return crypto.createHash('sha1').update(buffer).digest() |
|||
} |
|||
|
|||
function sha256(buffer) { |
|||
if (!Buffer.isBuffer(buffer)) buffer = new Buffer(buffer) // FIXME: transitionary
|
|||
|
|||
return crypto.createHash('sha256').update(buffer).digest() |
|||
} |
|||
|
|||
module.exports = { |
|||
sha1: sha1, |
|||
sha256: sha256, |
|||
hash160: hash160, |
|||
hash256: hash256 |
|||
} |
@ -1,13 +0,0 @@ |
|||
var convert = require('./convert.js') |
|||
var Crypto = require('crypto-js') |
|||
var RIPEMD160 = Crypto.RIPEMD160 |
|||
var SHA256 = Crypto.SHA256 |
|||
|
|||
exports.sha256ripe160 = function (data) { |
|||
var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(data))) |
|||
return convert.wordArrayToBytes(wordArray) |
|||
} |
|||
|
|||
exports.error = function (msg) { |
|||
throw new Error(msg) |
|||
} |
@ -0,0 +1,49 @@ |
|||
var assert = require('assert') |
|||
var crypto = require('../').crypto |
|||
var fixture = require('./fixtures/crypto') |
|||
|
|||
describe('Crypto', function() { |
|||
describe('HASH160', function() { |
|||
it('matches the test vector', function() { |
|||
fixture.before.hex.forEach(function(hex, i) { |
|||
var actual = crypto.hash160(new Buffer(hex, 'hex')).toString('hex') |
|||
var expected = fixture.after.hash160[i] |
|||
|
|||
assert.equal(actual, expected) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('HASH256', function() { |
|||
it('matches the test vector', function() { |
|||
fixture.before.hex.forEach(function(hex, i) { |
|||
var actual = crypto.hash256(new Buffer(hex, 'hex')).toString('hex') |
|||
var expected = fixture.after.hash256[i] |
|||
|
|||
assert.equal(actual, expected) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('SHA1', function() { |
|||
it('matches the test vector', function() { |
|||
fixture.before.hex.forEach(function(hex, i) { |
|||
var actual = crypto.sha1(new Buffer(hex, 'hex')).toString('hex') |
|||
var expected = fixture.after.sha1[i] |
|||
|
|||
assert.equal(actual, expected) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('SHA256', function() { |
|||
it('matches the test vector', function() { |
|||
fixture.before.hex.forEach(function(hex, i) { |
|||
var actual = crypto.sha256(new Buffer(hex, 'hex')).toString('hex') |
|||
var expected = fixture.after.sha256[i] |
|||
|
|||
assert.equal(actual, expected) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
@ -0,0 +1,37 @@ |
|||
module.exports = { |
|||
before: { |
|||
hex: [ |
|||
'0000000000000001', |
|||
'0101010101010101', |
|||
'FFFFFFFFFFFFFFFF', |
|||
'4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320' |
|||
], |
|||
}, |
|||
|
|||
after: { |
|||
hash160: [ |
|||
'cdb00698f02afd929ffabea308340fa99ac2afa8', |
|||
'abaf1119f83e384210fe8e222eac76e2f0da39dc', |
|||
'f86221f5a1fca059a865c0b7d374dfa9d5f3aeb4', |
|||
'9763e6b367c363bd6b88a7b361c98e6beee243a5' |
|||
], |
|||
hash256: [ |
|||
'3ae5c198d17634e79059c2cd735491553d22c4e09d1d9fea3ecf214565df2284', |
|||
'728338d99f356175c4945ef5cccfa61b7b56143cbbf426ddd0e0fc7cfe8c3c23', |
|||
'752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad', |
|||
'033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799' |
|||
], |
|||
sha1: [ |
|||
'cb473678976f425d6ec1339838f11011007ad27d', |
|||
'c0357a32ed1f6a03be92dd094476f7f1a2e214ec', |
|||
'be673e8a56eaa9d8c1d35064866701c11ef8e089', |
|||
'10d96fb43aca84e342206887bbeed3065d4e4344' |
|||
], |
|||
sha256: [ |
|||
'cd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50', |
|||
'04abc8821a06e5a30937967d11ad10221cb5ac3b5273e434f1284ee87129a061', |
|||
'12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca', |
|||
'a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da' |
|||
] |
|||
} |
|||
} |
Loading…
Reference in new issue