Ryan X. Charles
11 years ago
3 changed files with 93 additions and 14 deletions
@ -0,0 +1,33 @@ |
|||||
|
var Bn = require('./bn'); |
||||
|
var Privkey = require('./privkey'); |
||||
|
var Point = require('./point'); |
||||
|
var Pubkey = require('./pubkey'); |
||||
|
var Key = require('./key'); |
||||
|
var Hash = require('./hash'); |
||||
|
|
||||
|
function KDF() { |
||||
|
}; |
||||
|
|
||||
|
KDF.buf2key = function(buf) { |
||||
|
return KDF.sha256hmac2key(buf); |
||||
|
}; |
||||
|
|
||||
|
KDF.sha256hmac2key = function(buf) { |
||||
|
var privkey = KDF.sha256hmac2privkey(buf); |
||||
|
var key = new Key(privkey); |
||||
|
key.privkey2pubkey(); |
||||
|
return key; |
||||
|
}; |
||||
|
|
||||
|
KDF.sha256hmac2privkey = function(buf) { |
||||
|
var bn; |
||||
|
var concat = new Buffer([]); |
||||
|
do { |
||||
|
var hash = Hash.sha256hmac(buf, concat); |
||||
|
var bn = Bn.fromBuffer(hash); |
||||
|
concat = Buffer.concat([concat, new Buffer(0)]); |
||||
|
} while(!bn.lt(Point.getN())); |
||||
|
return new Privkey(bn); |
||||
|
}; |
||||
|
|
||||
|
module.exports = KDF; |
@ -0,0 +1,39 @@ |
|||||
|
var should = require('chai').should(); |
||||
|
var KDF = require('../lib/kdf'); |
||||
|
var Hash = require('../lib/hash'); |
||||
|
|
||||
|
describe('kdf', function() { |
||||
|
|
||||
|
describe('#buf2key', function() { |
||||
|
|
||||
|
it('should compute these known values', function() { |
||||
|
var buf = Hash.sha256(new Buffer('test')); |
||||
|
var key = KDF.buf2key(buf); |
||||
|
key.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); |
||||
|
key.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#sha256hmac2key', function() { |
||||
|
|
||||
|
it('should compute these known values', function() { |
||||
|
var buf = Hash.sha256(new Buffer('test')); |
||||
|
var key = KDF.sha256hmac2key(buf); |
||||
|
key.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); |
||||
|
key.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#sha256hmac2privkey', function() { |
||||
|
|
||||
|
it('should compute this known privkey', function() { |
||||
|
var buf = Hash.sha256(new Buffer('test')); |
||||
|
var privkey = KDF.sha256hmac2privkey(buf); |
||||
|
privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
Loading…
Reference in new issue