Comparing version 0.2.1 to 0.3.0
103
index.js
@@ -8,6 +8,22 @@ var fs = require('fs') | ||
var path = require('path') | ||
var curve = ecc.curves.k256 | ||
var createHmac = require('hmac') | ||
function bsum (value) { | ||
return new Blake2s().update(value).digest() | ||
function hash (data, enc) { | ||
return new Blake2s().update(data, enc).digest('base64') + '.blake2s' | ||
} | ||
function isHash (data) { | ||
return isString(data) && /^[A-Za-z0-9\/+]{43}=\.blake2s$/.test(data) | ||
} | ||
exports.isHash = isHash | ||
exports.hash = hash | ||
function isString(s) { | ||
return 'string' === typeof s | ||
} | ||
function empty(v) { return !!v } | ||
@@ -17,4 +33,3 @@ | ||
var privateKey = crypto.randomBytes(32) | ||
var k = ecc.restore(k256, privateKey) | ||
k.id = bsum(k.public) | ||
var k = keysToBase64(ecc.restore(k256, privateKey)) | ||
k.keyfile = [ | ||
@@ -38,10 +53,47 @@ '# this is your SECRET name.', | ||
function toBuffer(buf) { | ||
if(buf == null) return buf | ||
return new Buffer(buf.substring(0, buf.indexOf('.')), 'base64') | ||
} | ||
function keysToBase64 (keys) { | ||
var pub = tag(keys.public, 'k256') | ||
return { | ||
public: pub, | ||
private: tag(keys.private, 'k256'), | ||
id: hash(pub) | ||
} | ||
} | ||
function hashToBuffer(hash) { | ||
if(!isHash(hash)) throw new Error('sign expects a hash') | ||
return toBuffer(hash) | ||
} | ||
function keysToBuffer(key) { | ||
return isString(key) ? toBuffer(key) : { | ||
public: toBuffer(key.public), | ||
private: toBuffer(key.private) | ||
} | ||
} | ||
function reconstructKeys(privateKeyStr) { | ||
privateKeyStr = privateKeyStr.replace(/\s*\#[^\n]*/g, '').split('\n').filter(empty).join('') | ||
var privateKey = new Buffer(privateKeyStr, 'hex') | ||
var k = ecc.restore(k256, privateKey) | ||
k.id = bsum(k.public) | ||
return k | ||
privateKeyStr = privateKeyStr | ||
.replace(/\s*\#[^\n]*/g, '') | ||
.split('\n').filter(empty).join('') | ||
var privateKey = ( | ||
!/\./.test(privateKeyStr) | ||
? new Buffer(privateKeyStr, 'hex') | ||
: toBuffer(privateKeyStr) | ||
) | ||
return keysToBase64(ecc.restore(k256, privateKey)) | ||
} | ||
function tag (key, tag) { | ||
return key.toString('base64')+'.' + tag | ||
} | ||
exports.load = function(namefile, cb) { | ||
@@ -92,1 +144,34 @@ fs.readFile(namefile, 'ascii', function(err, privateKeyStr) { | ||
} | ||
//this should return a key pair: | ||
// {public: Buffer, private: Buffer} | ||
exports.generate = function () { | ||
return keysToBase64(ecc.restore(curve, crypto.randomBytes(32))) | ||
} | ||
//takes a public key and a hash and returns a signature. | ||
//(a signature must be a node buffer) | ||
exports.sign = function (keys, hash) { | ||
var hashTag = hash.substring(hash.indexOf('.')) | ||
return tag( | ||
ecc.sign(curve, keysToBuffer(keys), hashToBuffer(hash)), | ||
hashTag + '.k256' | ||
) | ||
} | ||
//takes a public key, signature, and a hash | ||
//and returns true if the signature was valid. | ||
exports.verify = function (pub, sig, hash) { | ||
return ecc.verify(curve, keysToBuffer(pub), toBuffer(sig), hashToBuffer(hash)) | ||
} | ||
function createHash() { | ||
return new Blake2s() | ||
} | ||
exports.hmac = function (data, key) { | ||
return createHmac(createHash, 64, key) | ||
.update(data).digest('base64')+'.blake2s.hmac' | ||
} | ||
{ | ||
"name": "ssb-keys", | ||
"description": "create or load a keypair file for secure-scuttlebutt", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/pfraze/ssb-keys", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/pfraze/ssb-keys.git" | ||
"url": "git://github.com/pfraze/ssb-crypto.git" | ||
}, | ||
@@ -13,3 +13,4 @@ "dependencies": { | ||
"blake2s": "~1.0.0", | ||
"mkdirp": "~0.5.0" | ||
"mkdirp": "~0.5.0", | ||
"hmac": "~1.0.1" | ||
}, | ||
@@ -24,2 +25,2 @@ "devDependencies": { | ||
"license": "MIT" | ||
} | ||
} |
# SSB-Keys | ||
A common module for secure-scuttlebutt projects, provides an API to create or load elliptic-curve keypairs. | ||
A common module for secure-scuttlebutt projects, provides an API to create or load elliptic-curve keypairs and to execute related crypto operations. | ||
@@ -10,5 +10,5 @@ ```js | ||
console.log(k) /* => { | ||
id: Buffer(...), | ||
public: Buffer(...), | ||
private: Buffer(...) | ||
id: String, | ||
public: String, | ||
private: String | ||
}*/ | ||
@@ -19,5 +19,5 @@ }) | ||
console.log(k) /* => { | ||
id: Buffer(...), | ||
public: Buffer(...), | ||
private: Buffer(...) | ||
id: String, | ||
public: String, | ||
private: String | ||
}*/ | ||
@@ -28,5 +28,5 @@ }) | ||
console.log(k) /* => { | ||
id: Buffer(...), | ||
public: Buffer(...), | ||
private: Buffer(...) | ||
id: String, | ||
public: String, | ||
private: String | ||
}*/ | ||
@@ -36,6 +36,21 @@ | ||
console.log(k) /* => { | ||
id: Buffer(...), | ||
public: Buffer(...), | ||
private: Buffer(...) | ||
id: String, | ||
public: String, | ||
private: String | ||
}*/ | ||
var k = ssbkeys.generate() | ||
console.log(k) /* => { | ||
id: String, | ||
public: String, | ||
private: String | ||
}*/ | ||
var hash = ssbkeys.hash(new Buffer('deadbeef', 'hex')) | ||
ssbkeys.isHash(hash) // => true | ||
var sig = ssbkeys.sign(k, hash) | ||
ssbkeys.verify(k.public, sig, hash) | ||
ssbkeys.hmac(new Buffer('deadbeef', 'hex'), k.private) // => String | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6864
169
52
4
+ Addedhmac@~1.0.1
+ Addedhmac@1.0.1(transitive)