mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-06-27 12:11:52 +00:00
keys now generated and derived match with go client
This commit is contained in:
15
src/crypto.proto
Normal file
15
src/crypto.proto
Normal file
@ -0,0 +1,15 @@
|
||||
package crypto.pb;
|
||||
|
||||
enum KeyType {
|
||||
RSA = 0;
|
||||
}
|
||||
|
||||
message PublicKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
||||
|
||||
message PrivateKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
128
src/index.js
128
src/index.js
@ -2,10 +2,14 @@
|
||||
* 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 keypair = require('keypair')
|
||||
var forge = require('node-forge')
|
||||
var protobuf = require('protocol-buffers')
|
||||
|
||||
//protobuf read from file
|
||||
var messages = protobuf(fs.readFileSync(__dirname+'/crypto.proto'))
|
||||
|
||||
exports = module.exports = Id
|
||||
|
||||
@ -26,7 +30,7 @@ function Id (id, privKey, pubKey) {
|
||||
|
||||
self.toPrint = function () {
|
||||
return {
|
||||
id: self.toHexString(),
|
||||
id: self.toB58String(),
|
||||
privKey: privKey.toString('hex'),
|
||||
pubKey: pubKey.toString('hex')
|
||||
}
|
||||
@ -47,18 +51,78 @@ function Id (id, privKey, pubKey) {
|
||||
}
|
||||
}
|
||||
|
||||
function fix (str) {
|
||||
return str.replace(/\r/g, '') + '\n'
|
||||
//unwrap the private key protobuf stream
|
||||
function unmarshal (key) {
|
||||
var dpb = messages.PrivateKey.decode(key)
|
||||
return dpb
|
||||
}
|
||||
|
||||
//create a public key protobuf to be base64 string stored in config
|
||||
function marshal (data, type) {
|
||||
if(type == 'Public'){
|
||||
var epb = messages.PublicKey.encode({
|
||||
Type: 0,
|
||||
Data: data
|
||||
})
|
||||
}
|
||||
|
||||
if(type == 'Private'){
|
||||
var epb = messages.PrivateKey.encode({
|
||||
Type: 0,
|
||||
Data: data
|
||||
})
|
||||
}
|
||||
|
||||
return epb
|
||||
}
|
||||
|
||||
// generation
|
||||
|
||||
exports.create = function () {
|
||||
var pair = keypair()
|
||||
//generate keys
|
||||
var pair = forge.rsa.generateKeyPair({bits:2048, e: 0x10001})
|
||||
|
||||
var mhId = multihashing(pair.public, 'sha2-256')
|
||||
//Create Public Key
|
||||
//return the RSA public key to asn1 object and DER encode
|
||||
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
||||
|
||||
return new Id(mhId, pair.private, pair.public)
|
||||
//create der buffer of public key asn.1 object
|
||||
var derPub = forge.asn1.toDer(asnPub)
|
||||
|
||||
//create forge buffer of der public key buffer
|
||||
var fDerBuf = forge.util.createBuffer(derPub.data, 'binary')
|
||||
|
||||
//convert forge buffer to node buffer public key
|
||||
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||
|
||||
//protobuf the new DER bytes to the PublicKey Data: field
|
||||
var marPubKey = marshal(nDerBuf, 'Public')
|
||||
|
||||
//encode the protobuf public key to base64 string
|
||||
var pubKeyb64 = marPubKey.toString('base64')
|
||||
|
||||
|
||||
//create Private Key
|
||||
//return the RSA private key to asn1 object and DER encode
|
||||
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
||||
|
||||
//create der buffer of private key asn.1 object
|
||||
var derPriv = forge.asn1.toDer(asnPriv)
|
||||
|
||||
//create forge buffer of der private key buffer
|
||||
var fDerBufPriv = forge.util.createBuffer(derPriv.data, 'binary')
|
||||
|
||||
//convert forge buffer to node buffer private key
|
||||
var nDerBufPriv = new Buffer(fDerBufPriv.getBytes(), 'binary')
|
||||
|
||||
//protobuf the new DER bytes to the PrivateKey Data: field
|
||||
var marPrivKey = marshal(nDerBufPriv, 'Private')
|
||||
|
||||
//encode the protobuf private key to base64 string
|
||||
var privKeyb64 = marPrivKey.toString('base64')
|
||||
|
||||
var mhId = multihashing(marPubKey, 'sha2-256')
|
||||
|
||||
return new Id(mhId, privKeyb64, pubKeyb64)
|
||||
}
|
||||
|
||||
exports.createFromHexString = function (str) {
|
||||
@ -74,17 +138,49 @@ exports.createFromB58String = function (str) {
|
||||
}
|
||||
|
||||
exports.createFromPubKey = function (pubKey) {
|
||||
var buf = new Buffer(pubKey, 'base64')
|
||||
var mhId = multihashing(pubKey, 'sha2-256')
|
||||
return new Id(mhId, null, pubKey)
|
||||
}
|
||||
|
||||
exports.createFromPrivKey = function (privKey) {
|
||||
var privateKey = forge.pki.privateKeyFromPem(privKey)
|
||||
var publicKey = {
|
||||
n: privateKey.n,
|
||||
e: privateKey.e
|
||||
}
|
||||
var pubKey = fix(forge.pki.publicKeyToRSAPublicKeyPem(publicKey, 72))
|
||||
var mhId = multihashing(pubKey, 'sha2-256')
|
||||
return new Id(mhId, privKey, pubKey)
|
||||
//create a buffer from the base64 encoded string
|
||||
var buf = new Buffer(privKey, 'base64')
|
||||
|
||||
//get the private key data from the protobuf
|
||||
var mpk = unmarshal(buf)
|
||||
|
||||
//create a forge buffer
|
||||
var 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)
|
||||
|
||||
//get the RSA privatekey data from the asn1 object
|
||||
var 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)
|
||||
|
||||
//return the RSA public to asn1 object and DER encode
|
||||
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
||||
|
||||
//create der buffer of public key asn.1 object
|
||||
var derPub = forge.asn1.toDer(asnPub)
|
||||
|
||||
//create forge buffer of der buffer
|
||||
var fDerBuf = forge.util.createBuffer(derPub.data, 'binary')
|
||||
|
||||
//convert forge buffer to node buffer
|
||||
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||
|
||||
//protobuf the new DER bytes to the PublicKey Data: field
|
||||
var marPubKey = marshal(nDerBuf, 'Public')
|
||||
|
||||
//encode the protobuf public key to base64 string
|
||||
var pubKeyb64 = marPubKey.toString('base64')
|
||||
|
||||
var mhId = multihashing(marPubKey, 'sha2-256')
|
||||
|
||||
return new Id(mhId, privKey, pubKeyb64)
|
||||
}
|
||||
|
Reference in New Issue
Block a user