came to fix a square bracket, ended up touch a bunch of other things :)

This commit is contained in:
David Dias
2016-03-03 17:31:33 +00:00
parent 1cdbf19857
commit 59b89039b6
8 changed files with 70 additions and 24302 deletions

View File

@ -2,24 +2,25 @@
* Id is an object representation of a peer Id. a peer Id is a multihash
*/
var fs = require('fs')
var multihashing = require('multihashing')
var base58 = require('bs58')
var forge = require('node-forge')
var protobuf = require('protocol-buffers')
const fs = require('fs')
const multihashing = require('multihashing')
const base58 = require('bs58')
const forge = require('node-forge')
const protobuf = require('protocol-buffers')
const path = require('path')
var isNode = !global.window
const isNode = !global.window
// protobuf read from file
var messages = isNode ? protobuf(fs.readFileSync(__dirname + '/../pb/crypto.proto'))
: protobuf(require('buffer!./../pb/crypto.proto'))
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto')))
: protobuf(require('buffer!./pb/crypto.proto'))
exports = module.exports = Id
exports.Buffer = Buffer
function Id (id, privKey, pubKey) {
var self = this
const self = this
if (!(self instanceof Id)) {
throw new Error('Id must be called with new')
@ -56,8 +57,7 @@ function Id (id, privKey, pubKey) {
// unwrap the private key protobuf
function unmarshal (key) {
var dpb = messages.PrivateKey.decode(key)
return dpb
return messages.PrivateKey.decode(key)
}
// create a public key protobuf to be base64 string stored in config
@ -83,36 +83,36 @@ function marshal (data, type) {
// this returns a base64 encoded protobuf of the public key
function formatKey (key, type) {
// create der buffer of public key asn.1 object
var der = forge.asn1.toDer(key)
const der = forge.asn1.toDer(key)
// create forge buffer of der public key buffer
var fDerBuf = forge.util.createBuffer(der.data, 'binary')
const fDerBuf = forge.util.createBuffer(der.data, 'binary')
// convert forge buffer to node buffer public key
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
// protobuf the new DER bytes to the PublicKey Data: field
var marshalKey = marshal(nDerBuf, type)
const marshalKey = marshal(nDerBuf, type)
// encode the protobuf public key to base64 string
var b64 = marshalKey.toString('base64')
const b64 = marshalKey.toString('base64')
return b64
}
// generation
exports.create = function () {
// generate keys
var pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
const pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
// return the RSA public/private key to asn1 object
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
const asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
// format the keys to protobuf base64 encoded string
var protoPublic64 = formatKey(asnPub, 'Public')
var protoPrivate64 = formatKey(asnPriv, 'Private')
const protoPublic64 = formatKey(asnPub, 'Public')
const protoPrivate64 = formatKey(asnPriv, 'Private')
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new Id(mhId, protoPrivate64, protoPublic64)
}
@ -130,35 +130,35 @@ exports.createFromB58String = function (str) {
}
exports.createFromPubKey = function (pubKey) {
var buf = new Buffer(pubKey, 'base64')
var mhId = multihashing(buf, 'sha2-256')
const buf = new Buffer(pubKey, 'base64')
const mhId = multihashing(buf, 'sha2-256')
return new Id(mhId, null, pubKey)
}
exports.createFromPrivKey = function (privKey) {
// create a buffer from the base64 encoded string
var buf = new Buffer(privKey, 'base64')
const buf = new Buffer(privKey, 'base64')
// get the private key data from the protobuf
var mpk = unmarshal(buf)
const mpk = unmarshal(buf)
// create a forge buffer
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
// create an asn1 object from the private key bytes saved in the protobuf Data: field
var asnPriv = forge.asn1.fromDer(fbuf)
const asnPriv = forge.asn1.fromDer(fbuf)
// get the RSA privatekey data from the asn1 object
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
const privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
// set the RSA public key to the modulus and exponent of the private key
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
// return the RSA public key to asn1 object
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
const asnPub = forge.pki.publicKeyToAsn1(publicKey)
// format the public key
var protoPublic64 = formatKey(asnPub, 'Public')
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
const protoPublic64 = formatKey(asnPub, 'Public')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new Id(mhId, privKey, protoPublic64)
}

13
src/pb/crypto.proto Normal file
View File

@ -0,0 +1,13 @@
enum KeyType {
RSA = 0;
}
message PublicKey {
required KeyType Type = 1;
required bytes Data = 2;
}
message PrivateKey {
required KeyType Type = 1;
required bytes Data = 2;
}