Browse Source

Merge pull request #229 from dcousens/ripemd

Browserify RIPEMD160
hk-custom-address
Kyle Drake 11 years ago
parent
commit
27ab41c3bb
  1. 6
      package.json
  2. 12
      src/crypto.js
  3. 43
      test/crypto.js
  4. 7
      test/fixtures/crypto.json

6
package.json

@ -36,7 +36,7 @@
"url": "https://github.com/bitcoinjs/bitcoinjs-lib.git" "url": "https://github.com/bitcoinjs/bitcoinjs-lib.git"
}, },
"devDependencies": { "devDependencies": {
"browserify": "~4.1.5", "browserify": "4.1.11",
"coveralls": "~2.10.0", "coveralls": "~2.10.0",
"helloblock-js": "^0.2.1", "helloblock-js": "^0.2.1",
"istanbul": "0.1.30", "istanbul": "0.1.30",
@ -68,9 +68,13 @@
"test": "npm run-script unit", "test": "npm run-script unit",
"unit": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- --reporter list `find test -maxdepth 1 -not -type d`" "unit": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- --reporter list `find test -maxdepth 1 -not -type d`"
}, },
"browser": {
"crypto": "crypto-browserify"
},
"dependencies": { "dependencies": {
"bigi": "1.1.0", "bigi": "1.1.0",
"crypto-js": "3.1.2-3", "crypto-js": "3.1.2-3",
"crypto-browserify": "2.1.8",
"ecurve": "0.10.0", "ecurve": "0.10.0",
"secure-random": "0.2.1" "secure-random": "0.2.1"
} }

12
src/crypto.js

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

43
test/crypto.js

@ -8,10 +8,9 @@ describe('Crypto', function() {
it('matches the test vectors', function() { it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var actual = crypto.hash160(data) var actual = crypto.hash160(data).toString('hex')
var expected = fixtures.after.hash160[i]
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() { it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var actual = crypto.hash256(data) var actual = crypto.hash256(data).toString('hex')
var expected = fixtures.after.hash256[i]
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() { it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var actual = crypto.sha1(data) var actual = crypto.sha1(data).toString('hex')
var expected = fixtures.after.sha1[i]
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() { it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var actual = crypto.sha256(data) var actual = crypto.sha256(data).toString('hex')
var expected = fixtures.after.sha256[i]
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) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var secret = new Buffer(fixtures.before.secret) var secret = new Buffer(fixtures.before.secret)
var actual = crypto.HmacSHA256(data, secret).toString('hex')
var actual = crypto.HmacSHA256(data, secret) assert.equal(actual, fixtures.after.hmacsha256[i])
var expected = fixtures.after.hmacsha256[i]
assert.equal(actual.toString('hex'), expected)
}) })
}) })
}) })
@ -71,11 +76,9 @@ describe('Crypto', function() {
fixtures.before.hex.forEach(function(hex, i) { fixtures.before.hex.forEach(function(hex, i) {
var data = new Buffer(hex, 'hex') var data = new Buffer(hex, 'hex')
var secret = new Buffer(fixtures.before.secret) var secret = new Buffer(fixtures.before.secret)
var actual = crypto.HmacSHA512(data, secret).toString('hex')
var actual = crypto.HmacSHA512(data, secret) assert.equal(actual, fixtures.after.hmacsha512[i])
var expected = fixtures.after.hmacsha512[i]
assert.equal(actual.toString('hex'), expected)
}) })
}) })
}) })

7
test/fixtures/crypto.json

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

Loading…
Cancel
Save