Browse Source

crypto: add RIPEMD160 tests

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
9d2784a441
  1. 15
      src/crypto.js
  2. 43
      test/crypto.js
  3. 9
      test/fixtures/crypto.json

15
src/crypto.js

@ -5,18 +5,20 @@ var crypto = require('crypto')
var convert = require('./convert')
function hash160(buffer) {
var step1 = sha256(buffer)
var step2a = convert.bufferToWordArray(step1)
var step2b = CryptoJS.RIPEMD160(step2a)
return convert.wordArrayToBuffer(step2b)
return ripemd160(sha256(buffer))
}
function hash256(buffer) {
return sha256(sha256(buffer))
}
function ripemd160(buffer) {
var array = convert.bufferToWordArray(buffer)
var result = CryptoJS.RIPEMD160(array)
return convert.wordArrayToBuffer(result)
}
function sha1(buffer) {
return crypto.createHash('sha1').update(buffer).digest()
}
@ -43,6 +45,7 @@ function HmacSHA512(data, secret) {
}
module.exports = {
ripemd160: ripemd160,
sha1: sha1,
sha256: sha256,
hash160: hash160,

43
test/crypto.js

@ -8,10 +8,9 @@ describe('Crypto', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var actual = crypto.hash160(data)
var expected = fixtures.after.hash160[i]
var actual = crypto.hash160(data).toString('hex')
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.hash160[i])
})
})
})
@ -20,10 +19,20 @@ describe('Crypto', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var actual = crypto.hash256(data)
var expected = fixtures.after.hash256[i]
var actual = crypto.hash256(data).toString('hex')
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.hash256[i])
})
})
})
describe('RIPEMD160', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var actual = crypto.ripemd160(data).toString('hex')
assert.equal(actual, fixtures.after.ripemd160[i])
})
})
})
@ -32,10 +41,9 @@ describe('Crypto', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var actual = crypto.sha1(data)
var expected = fixtures.after.sha1[i]
var actual = crypto.sha1(data).toString('hex')
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.sha1[i])
})
})
})
@ -44,10 +52,9 @@ describe('Crypto', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var actual = crypto.sha256(data)
var expected = fixtures.after.sha256[i]
var actual = crypto.sha256(data).toString('hex')
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.sha256[i])
})
})
})
@ -57,11 +64,9 @@ describe('Crypto', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var secret = new Buffer(fixtures.before.secret)
var actual = crypto.HmacSHA256(data, secret).toString('hex')
var actual = crypto.HmacSHA256(data, secret)
var expected = fixtures.after.hmacsha256[i]
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.hmacsha256[i])
})
})
})
@ -71,11 +76,9 @@ describe('Crypto', function() {
fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex')
var secret = new Buffer(fixtures.before.secret)
var actual = crypto.HmacSHA512(data, secret).toString('hex')
var actual = crypto.HmacSHA512(data, secret)
var expected = fixtures.after.hmacsha512[i]
assert.equal(actual.toString('hex'), expected)
assert.equal(actual, fixtures.after.hmacsha512[i])
})
})
})

9
test/fixtures/crypto.json

@ -8,7 +8,6 @@
"4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320"
]
},
"after": {
"hash160": [
"cdb00698f02afd929ffabea308340fa99ac2afa8",
@ -22,6 +21,12 @@
"752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad",
"033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799"
],
"ripemd160": [
"8d1a05d1bc08870968eb8a81ad4393fd3aac6633",
"5825701b4b9767fd35063b286dca3582853e0630",
"cb760221600ed34337ca3ab70016b5f58c838120",
"cad8593dcdef12ee334c97bab9787f07b3f3a1a5"
],
"sha1": [
"cb473678976f425d6ec1339838f11011007ad27d",
"c0357a32ed1f6a03be92dd094476f7f1a2e214ec",
@ -47,4 +52,4 @@
"7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029"
]
}
}
}
Loading…
Cancel
Save