crypto/ecdsa: moves HmacSHA256 to crypto

This commit is contained in:
Daniel Cousens 2014-05-16 12:36:09 +10:00
parent 99a1b7274c
commit b208a6ab78
4 changed files with 58 additions and 35 deletions

View file

@ -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
}

View file

@ -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)