crypto/ecdsa: moves HmacSHA256 to crypto
This commit is contained in:
parent
99a1b7274c
commit
b208a6ab78
4 changed files with 58 additions and 35 deletions
|
@ -34,6 +34,10 @@ function sha256(buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Name not consistent with others
|
// FIXME: Name not consistent with others
|
||||||
|
function HmacSHA256(buffer, secret) {
|
||||||
|
return crypto.createHmac('sha256', secret).update(buffer).digest()
|
||||||
|
}
|
||||||
|
|
||||||
function HmacSHA512(data, secret) {
|
function HmacSHA512(data, secret) {
|
||||||
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
|
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
|
||||||
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
|
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
|
||||||
|
@ -51,5 +55,6 @@ module.exports = {
|
||||||
sha256: sha256,
|
sha256: sha256,
|
||||||
hash160: hash160,
|
hash160: hash160,
|
||||||
hash256: hash256,
|
hash256: hash256,
|
||||||
|
HmacSHA256: HmacSHA256,
|
||||||
HmacSHA512: HmacSHA512
|
HmacSHA512: HmacSHA512
|
||||||
}
|
}
|
||||||
|
|
16
src/ecdsa.js
16
src/ecdsa.js
|
@ -1,5 +1,5 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('crypto')
|
var crypto = require('./crypto')
|
||||||
var sec = require('./sec')
|
var sec = require('./sec')
|
||||||
var ecparams = sec("secp256k1")
|
var ecparams = sec("secp256k1")
|
||||||
|
|
||||||
|
@ -36,10 +36,6 @@ function implShamirsTrick(P, k, Q, l) {
|
||||||
|
|
||||||
var ecdsa = {
|
var ecdsa = {
|
||||||
deterministicGenerateK: function(hash, D) {
|
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(Buffer.isBuffer(hash), 'Hash must be a Buffer')
|
||||||
assert.equal(hash.length, 32, 'Hash must be 256 bit')
|
assert.equal(hash.length, 32, 'Hash must be 256 bit')
|
||||||
assert(D instanceof BigInteger, 'Private key must be a BigInteger')
|
assert(D instanceof BigInteger, 'Private key must be a BigInteger')
|
||||||
|
@ -50,12 +46,12 @@ var ecdsa = {
|
||||||
k.fill(0)
|
k.fill(0)
|
||||||
v.fill(1)
|
v.fill(1)
|
||||||
|
|
||||||
k = HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k)
|
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k)
|
||||||
v = HmacSHA256(v, k)
|
v = crypto.HmacSHA256(v, k)
|
||||||
|
|
||||||
k = HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k)
|
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k)
|
||||||
v = HmacSHA256(v, k)
|
v = crypto.HmacSHA256(v, k)
|
||||||
v = HmacSHA256(v, k)
|
v = crypto.HmacSHA256(v, k)
|
||||||
|
|
||||||
var n = ecparams.getN()
|
var n = ecparams.getN()
|
||||||
var kB = BigInteger.fromBuffer(v).mod(n)
|
var kB = BigInteger.fromBuffer(v).mod(n)
|
||||||
|
|
|
@ -5,57 +5,75 @@ var fixtures = require('./fixtures/crypto')
|
||||||
|
|
||||||
describe('Crypto', function() {
|
describe('Crypto', function() {
|
||||||
describe('HASH160', function() {
|
describe('HASH160', function() {
|
||||||
it('matches the test vector', function() {
|
it('matches the test vectors', function() {
|
||||||
fixtures.before.hex.forEach(function(hex, i) {
|
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]
|
var expected = fixtures.after.hash160[i]
|
||||||
|
|
||||||
assert.equal(actual, expected)
|
assert.equal(actual.toString('hex'), expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('HASH256', function() {
|
describe('HASH256', function() {
|
||||||
it('matches the test vector', function() {
|
it('matches the test vectors', function() {
|
||||||
fixtures.before.hex.forEach(function(hex, i) {
|
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]
|
var expected = fixtures.after.hash256[i]
|
||||||
|
|
||||||
assert.equal(actual, expected)
|
assert.equal(actual.toString('hex'), expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('SHA1', function() {
|
describe('SHA1', function() {
|
||||||
it('matches the test vector', function() {
|
it('matches the test vectors', function() {
|
||||||
fixtures.before.hex.forEach(function(hex, i) {
|
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]
|
var expected = fixtures.after.sha1[i]
|
||||||
|
|
||||||
assert.equal(actual, expected)
|
assert.equal(actual.toString('hex'), expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('SHA256', function() {
|
describe('SHA256', function() {
|
||||||
it('matches the test vector', function() {
|
it('matches the test vectors', function() {
|
||||||
fixtures.before.hex.forEach(function(hex, i) {
|
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]
|
var expected = fixtures.after.sha256[i]
|
||||||
|
|
||||||
assert.equal(actual, expected)
|
assert.equal(actual.toString('hex'), expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('HMAC SHA512', function() {
|
describe('HmacSHA256', function() {
|
||||||
it('matches the test vector', 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 secret = new Buffer(fixtures.after.hmacsha512.secret)
|
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('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)
|
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)
|
assert.equal(actual.toString('hex'), expected)
|
||||||
})
|
})
|
||||||
|
|
22
test/fixtures/crypto.js
vendored
22
test/fixtures/crypto.js
vendored
|
@ -1,5 +1,6 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
before: {
|
before: {
|
||||||
|
secret: 'vires is numeris',
|
||||||
hex: [
|
hex: [
|
||||||
'0000000000000001',
|
'0000000000000001',
|
||||||
'0101010101010101',
|
'0101010101010101',
|
||||||
|
@ -33,14 +34,17 @@ module.exports = {
|
||||||
'12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca',
|
'12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca',
|
||||||
'a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da'
|
'a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da'
|
||||||
],
|
],
|
||||||
hmacsha512: {
|
hmacsha256: [
|
||||||
secret: 'vires is numeris',
|
'73442dc8dd7f71a106a20fddd49d31856b1db12956c75070c8186b0b3eb71251',
|
||||||
hash: [
|
'7204c72af7c73f5e84447a752dc8a2708f91b896f29de5fcf4b7f42f13a30c6e',
|
||||||
'4c0595aed1f5d066ea9f797727c060eb86cb55ff29d4d4fd2cd0ad3a012386763aea604c030619c79aa7fd8d03cda1b73a9ebd17906a3d2a350108d1a98b24ac',
|
'a03c2ac6e9ca86678b5608a3d8682de46d17026f5fac4fd7147d2e5022061833',
|
||||||
'f80b90d63b804b3d2ab03b9bfb3ac94ee271352eb8bddfb6b4f5cf2a4fc9176acea35f517728e64943d1eb8af1e4674a114082c81bc8874d88b408b3b406d6a4',
|
'a780cd6e5c29cf11f756536ea5779992687c1b3b5e37f31b027a392d94e91fb8'
|
||||||
'134cf60c30a5cd412c7a5cd6c3f878279e139b47c19550b7456fa137fbf90e580ae0a923a22052f42ec801ac658db32821e271161b563eac4926285ba6b8f410',
|
],
|
||||||
'7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029'
|
hmacsha512: [
|
||||||
]
|
'4c0595aed1f5d066ea9f797727c060eb86cb55ff29d4d4fd2cd0ad3a012386763aea604c030619c79aa7fd8d03cda1b73a9ebd17906a3d2a350108d1a98b24ac',
|
||||||
}
|
'f80b90d63b804b3d2ab03b9bfb3ac94ee271352eb8bddfb6b4f5cf2a4fc9176acea35f517728e64943d1eb8af1e4674a114082c81bc8874d88b408b3b406d6a4',
|
||||||
|
'134cf60c30a5cd412c7a5cd6c3f878279e139b47c19550b7456fa137fbf90e580ae0a923a22052f42ec801ac658db32821e271161b563eac4926285ba6b8f410',
|
||||||
|
'7dee95aa3c462d3eb7ecb61536cb215e471d1fa73d8643a967905946e26c536588c5058abd5a049a22b987db95a7fb420f3bff12359dc53d03d7ce7df714e029'
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue