updated to use the new version of elliptic that includes hex padding

This commit is contained in:
Braydon Fuller 2014-11-02 16:14:23 -05:00
parent 74cecb7fdf
commit a4309b8f3b
2 changed files with 8 additions and 20 deletions

View File

@ -5,18 +5,6 @@ var bs58 = require('bs58');
var BitAuth = {};
/**
* Will add leading zeros to hex string
*
* @returns {String} A hex string
*/
BitAuth._addPaddingToHex = function(hex){
while( hex.length < 64 ){
hex = '0'+hex;
}
return hex;
}
/**
* Will return a key pair and identity
*
@ -29,7 +17,7 @@ BitAuth.generateSin = function() {
var keys = ecdsa.genKeyPair();
var privateKey = this._addPaddingToHex(keys.getPrivate('hex'));
var privateKey = keys.getPrivate('hex');
var publicKey = this.getPublicKeyFromPrivateKey(privateKey);
var sin = this.getSinFromPublicKey(publicKey);
@ -56,8 +44,8 @@ BitAuth.getPublicKeyFromPrivateKey = function(privkey) {
// compressed public key
var pubKey = keys.getPublic();
var xbuf = new Buffer(this._addPaddingToHex(pubKey.x.toString('hex')), 'hex');
var ybuf = new Buffer(this._addPaddingToHex(pubKey.y.toString('hex')), 'hex');
var xbuf = new Buffer(pubKey.x.toString('hex', 2), 'hex');
var ybuf = new Buffer(pubKey.y.toString('hex', 2), 'hex');
if (ybuf[ybuf.length-1] % 2) { //odd
var pub = Buffer.concat([new Buffer([3]), xbuf]);
@ -114,7 +102,7 @@ BitAuth.getSinFromPublicKey = function(pubkey) {
* @returns {String} signature - A DER signature in hex
*/
BitAuth.sign = function(data, privkey) {
var hash = (new hashjs.sha256()).update(new Buffer(data, 'ascii')).digest('hex');
var hash = (new hashjs.sha256()).update(new Buffer(data)).digest('hex');
var signature = ecdsa.sign(hash, privkey);
var hexsignature = signature.toDER('hex');
return hexsignature;
@ -129,7 +117,7 @@ BitAuth.sign = function(data, privkey) {
* @returns {Function|Boolean} - If the signature is valid
*/
BitAuth.verifySignature = function(data, pubkey, hexsignature, callback) {
var hash = (new hashjs.sha256()).update(new Buffer(data, 'ascii')).digest('hex');
var hash = (new hashjs.sha256()).update(new Buffer(data)).digest('hex');
var signature = new Buffer(hexsignature, 'hex');
var valid = ecdsa.verify(hash, signature, pubkey);
if ( callback ){

View File

@ -26,9 +26,9 @@
"main": "index.js",
"version": "0.1.1",
"dependencies": {
"elliptic": "=0.15.11",
"hash.js": "=0.3.2",
"bs58": "=2.0.0",
"elliptic": "^0.15.12",
"hash.js": "^0.3.2",
"bs58": "^2.0.0",
"request": "^2.36.0",
"express": "^4.3.1",
"base58-native": "^0.1.4",