Browse Source

crypto/ecdsa: moves HmacSHA256 to crypto

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
b208a6ab78
  1. 5
      src/crypto.js
  2. 16
      src/ecdsa.js
  3. 50
      test/crypto.js
  4. 22
      test/fixtures/crypto.js

5
src/crypto.js

@ -34,6 +34,10 @@ function sha256(buffer) {
}
// FIXME: Name not consistent with others
function HmacSHA256(buffer, secret) {
return crypto.createHmac('sha256', secret).update(buffer).digest()
}
function HmacSHA512(data, secret) {
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
@ -51,5 +55,6 @@ module.exports = {
sha256: sha256,
hash160: hash160,
hash256: hash256,
HmacSHA256: HmacSHA256,
HmacSHA512: HmacSHA512
}

16
src/ecdsa.js

@ -1,5 +1,5 @@
var assert = require('assert')
var crypto = require('crypto')
var crypto = require('./crypto')
var sec = require('./sec')
var ecparams = sec("secp256k1")
@ -36,10 +36,6 @@ function implShamirsTrick(P, k, Q, l) {
var ecdsa = {
deterministicGenerateK: function(hash, D) {
function HmacSHA256(buffer, secret) {
return crypto.createHmac('sha256', secret).update(buffer).digest()
}
assert(Buffer.isBuffer(hash), 'Hash must be a Buffer')
assert.equal(hash.length, 32, 'Hash must be 256 bit')
assert(D instanceof BigInteger, 'Private key must be a BigInteger')
@ -50,12 +46,12 @@ var ecdsa = {
k.fill(0)
v.fill(1)
k = HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k)
v = HmacSHA256(v, k)
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k)
v = crypto.HmacSHA256(v, k)
k = HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k)
v = HmacSHA256(v, k)
v = HmacSHA256(v, k)
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k)
v = crypto.HmacSHA256(v, k)
v = crypto.HmacSHA256(v, k)
var n = ecparams.getN()
var kB = BigInteger.fromBuffer(v).mod(n)

50
test/crypto.js

@ -5,57 +5,75 @@ var fixtures = require('./fixtures/crypto')
describe('Crypto', function() {
describe('HASH160', function() {
it('matches the test vector', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var actual = crypto.hash160(new Buffer(hex, 'hex')).toString('hex')
var data = new Buffer(hex, 'hex')
var actual = crypto.hash160(data)
var expected = fixtures.after.hash160[i]
assert.equal(actual, expected)
assert.equal(actual.toString('hex'), expected)
})
})
})
describe('HASH256', function() {
it('matches the test vector', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var actual = crypto.hash256(new Buffer(hex, 'hex')).toString('hex')
var data = new Buffer(hex, 'hex')
var actual = crypto.hash256(data)
var expected = fixtures.after.hash256[i]
assert.equal(actual, expected)
assert.equal(actual.toString('hex'), expected)
})
})
})
describe('SHA1', function() {
it('matches the test vector', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var actual = crypto.sha1(new Buffer(hex, 'hex')).toString('hex')
var data = new Buffer(hex, 'hex')
var actual = crypto.sha1(data)
var expected = fixtures.after.sha1[i]
assert.equal(actual, expected)
assert.equal(actual.toString('hex'), expected)
})
})
})
describe('SHA256', function() {
it('matches the test vector', function() {
it('matches the test vectors', function() {
fixtures.before.hex.forEach(function(hex, i) {
var actual = crypto.sha256(new Buffer(hex, 'hex')).toString('hex')
var data = new Buffer(hex, 'hex')
var actual = crypto.sha256(data)
var expected = fixtures.after.sha256[i]
assert.equal(actual, expected)
assert.equal(actual.toString('hex'), expected)
})
})
})
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)
var expected = fixtures.after.hmacsha256[i]
assert.equal(actual.toString('hex'), expected)
})
})
})
describe('HMAC SHA512', function() {
it('matches the test vector', function() {
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.after.hmacsha512.secret)
var secret = new Buffer(fixtures.before.secret)
var actual = crypto.HmacSHA512(data, secret)
var expected = fixtures.after.hmacsha512.hash[i]
var expected = fixtures.after.hmacsha512[i]
assert.equal(actual.toString('hex'), expected)
})

22
test/fixtures/crypto.js

@ -1,5 +1,6 @@
module.exports = {
before: {
secret: 'vires is numeris',
hex: [
'0000000000000001',
'0101010101010101',
@ -33,14 +34,17 @@ module.exports = {
'12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca',
'a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da'
],
hmacsha512: {
secret: 'vires is numeris',
hash: [
'4c0595aed1f5d066ea9f797727c060eb86cb55ff29d4d4fd2cd0ad3a012386763aea604c030619c79aa7fd8d03cda1b73a9ebd17906a3d2a350108d1a98b24ac',
'f80b90d63b804b3d2ab03b9bfb3ac94ee271352eb8bddfb6b4f5cf2a4fc9176acea35f517728e64943d1eb8af1e4674a114082c81bc8874d88b408b3b406d6a4',
'134cf60c30a5cd412c7a5cd6c3f878279e139b47c19550b7456fa137fbf90e580ae0a923a22052f42ec801ac658db32821e271161b563eac4926285ba6b8f410',
'7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029'
]
}
hmacsha256: [
'73442dc8dd7f71a106a20fddd49d31856b1db12956c75070c8186b0b3eb71251',
'7204c72af7c73f5e84447a752dc8a2708f91b896f29de5fcf4b7f42f13a30c6e',
'a03c2ac6e9ca86678b5608a3d8682de46d17026f5fac4fd7147d2e5022061833',
'a780cd6e5c29cf11f756536ea5779992687c1b3b5e37f31b027a392d94e91fb8'
],
hmacsha512: [
'4c0595aed1f5d066ea9f797727c060eb86cb55ff29d4d4fd2cd0ad3a012386763aea604c030619c79aa7fd8d03cda1b73a9ebd17906a3d2a350108d1a98b24ac',
'f80b90d63b804b3d2ab03b9bfb3ac94ee271352eb8bddfb6b4f5cf2a4fc9176acea35f517728e64943d1eb8af1e4674a114082c81bc8874d88b408b3b406d6a4',
'134cf60c30a5cd412c7a5cd6c3f878279e139b47c19550b7456fa137fbf90e580ae0a923a22052f42ec801ac658db32821e271161b563eac4926285ba6b8f410',
'7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029'
]
}
}

Loading…
Cancel
Save