diff --git a/src/crypto.js b/src/crypto.js index 6d81bb7..dec8a79 100644 --- a/src/crypto.js +++ b/src/crypto.js @@ -20,23 +20,10 @@ function sha256 (buffer) { return crypto.createHash('sha256').update(buffer).digest() } -// FIXME: Name not consistent with others -function HmacSHA256 (buffer, secret) { - console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead') - return crypto.createHmac('sha256', secret).update(buffer).digest() -} - -function HmacSHA512 (buffer, secret) { - console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead') - return crypto.createHmac('sha512', secret).update(buffer).digest() -} - module.exports = { - ripemd160: ripemd160, - sha1: sha1, - sha256: sha256, hash160: hash160, hash256: hash256, - HmacSHA256: HmacSHA256, - HmacSHA512: HmacSHA512 + ripemd160: ripemd160, + sha1: sha1, + sha256: sha256 } diff --git a/test/crypto.js b/test/crypto.js index e1c6306..91827ca 100644 --- a/test/crypto.js +++ b/test/crypto.js @@ -3,84 +3,21 @@ var assert = require('assert') var crypto = require('../src/crypto') -var fixtures = require('./fixtures/crypto.json') +var fixtures = require('./fixtures/crypto') describe('Crypto', function () { - describe('HASH160', 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).toString('hex') - - assert.equal(actual, fixtures.after.hash160[i]) - }) - }) - }) - - describe('HASH256', 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).toString('hex') - - 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]) - }) - }) - }) - - describe('SHA1', 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).toString('hex') - - assert.equal(actual, fixtures.after.sha1[i]) - }) - }) - }) - - describe('SHA256', 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).toString('hex') - - assert.equal(actual, fixtures.after.sha256[i]) - }) - }) - }) - - describe('HmacSHA256', function () { - it('matches the test vectors', 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') - - assert.equal(actual, fixtures.after.hmacsha256[i]) - }) - }) - }) - - describe('HmacSHA512', function () { - it('matches the test vectors', 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') - - assert.equal(actual, fixtures.after.hmacsha512[i]) + ['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(function (algorithm) { + describe(algorithm, function () { + fixtures.valid.forEach(function (f) { + var fn = crypto[algorithm] + var expected = f[algorithm] + + it('returns ' + expected + ' for ' + f.hex, function () { + var data = new Buffer(f.hex, 'hex') + var actual = fn(data).toString('hex') + + assert.equal(actual, expected) + }) }) }) }) diff --git a/test/fixtures/crypto.json b/test/fixtures/crypto.json index 0875eb7..aa66fec 100644 --- a/test/fixtures/crypto.json +++ b/test/fixtures/crypto.json @@ -1,55 +1,36 @@ { - "before": { - "secret": "vires is numeris", - "hex": [ - "0000000000000001", - "0101010101010101", - "FFFFFFFFFFFFFFFF", - "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320" - ] - }, - "after": { - "hash160": [ - "cdb00698f02afd929ffabea308340fa99ac2afa8", - "abaf1119f83e384210fe8e222eac76e2f0da39dc", - "f86221f5a1fca059a865c0b7d374dfa9d5f3aeb4", - "9763e6b367c363bd6b88a7b361c98e6beee243a5" - ], - "hash256": [ - "3ae5c198d17634e79059c2cd735491553d22c4e09d1d9fea3ecf214565df2284", - "728338d99f356175c4945ef5cccfa61b7b56143cbbf426ddd0e0fc7cfe8c3c23", - "752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad", - "033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799" - ], - "ripemd160": [ - "8d1a05d1bc08870968eb8a81ad4393fd3aac6633", - "5825701b4b9767fd35063b286dca3582853e0630", - "cb760221600ed34337ca3ab70016b5f58c838120", - "cad8593dcdef12ee334c97bab9787f07b3f3a1a5" - ], - "sha1": [ - "cb473678976f425d6ec1339838f11011007ad27d", - "c0357a32ed1f6a03be92dd094476f7f1a2e214ec", - "be673e8a56eaa9d8c1d35064866701c11ef8e089", - "10d96fb43aca84e342206887bbeed3065d4e4344" - ], - "sha256": [ - "cd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50", - "04abc8821a06e5a30937967d11ad10221cb5ac3b5273e434f1284ee87129a061", - "12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca", - "a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da" - ], - "hmacsha256": [ - "73442dc8dd7f71a106a20fddd49d31856b1db12956c75070c8186b0b3eb71251", - "7204c72af7c73f5e84447a752dc8a2708f91b896f29de5fcf4b7f42f13a30c6e", - "a03c2ac6e9ca86678b5608a3d8682de46d17026f5fac4fd7147d2e5022061833", - "a780cd6e5c29cf11f756536ea5779992687c1b3b5e37f31b027a392d94e91fb8" - ], - "hmacsha512": [ - "4c0595aed1f5d066ea9f797727c060eb86cb55ff29d4d4fd2cd0ad3a012386763aea604c030619c79aa7fd8d03cda1b73a9ebd17906a3d2a350108d1a98b24ac", - "f80b90d63b804b3d2ab03b9bfb3ac94ee271352eb8bddfb6b4f5cf2a4fc9176acea35f517728e64943d1eb8af1e4674a114082c81bc8874d88b408b3b406d6a4", - "134cf60c30a5cd412c7a5cd6c3f878279e139b47c19550b7456fa137fbf90e580ae0a923a22052f42ec801ac658db32821e271161b563eac4926285ba6b8f410", - "7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029" - ] - } + "valid": [ + { + "hex": "0000000000000001", + "hash160": "cdb00698f02afd929ffabea308340fa99ac2afa8", + "hash256": "3ae5c198d17634e79059c2cd735491553d22c4e09d1d9fea3ecf214565df2284", + "ripemd160": "8d1a05d1bc08870968eb8a81ad4393fd3aac6633", + "sha1": "cb473678976f425d6ec1339838f11011007ad27d", + "sha256": "cd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50" + }, + { + "hex": "0101010101010101", + "hash160": "abaf1119f83e384210fe8e222eac76e2f0da39dc", + "hash256": "728338d99f356175c4945ef5cccfa61b7b56143cbbf426ddd0e0fc7cfe8c3c23", + "ripemd160": "5825701b4b9767fd35063b286dca3582853e0630", + "sha1": "c0357a32ed1f6a03be92dd094476f7f1a2e214ec", + "sha256": "04abc8821a06e5a30937967d11ad10221cb5ac3b5273e434f1284ee87129a061" + }, + { + "hex": "ffffffffffffffff", + "hash160": "f86221f5a1fca059a865c0b7d374dfa9d5f3aeb4", + "hash256": "752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad", + "ripemd160": "cb760221600ed34337ca3ab70016b5f58c838120", + "sha1": "be673e8a56eaa9d8c1d35064866701c11ef8e089", + "sha256": "12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca" + }, + { + "hex": "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320", + "hash160": "9763e6b367c363bd6b88a7b361c98e6beee243a5", + "hash256": "033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799", + "ripemd160": "cad8593dcdef12ee334c97bab9787f07b3f3a1a5", + "sha1": "10d96fb43aca84e342206887bbeed3065d4e4344", + "sha256": "a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da" + } + ] } \ No newline at end of file