Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaoqi committed Oct 3, 2019
1 parent 32a4fe7 commit bba4f42
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
52 changes: 33 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
const NodeRSA = require('node-rsa')
const crypto = require('crypto')

function create(bits = 1024) {
const key = new NodeRSA({b: bits})
const private = key.exportKey('pkcs8-private-der')
const public = key.exportKey('pkcs8-public-der')
return [public, private]
async function createAsync(bits = 4096) {
const {privateKey, publicKey} = await new Promise((resolve, reject)=>
crypto.generateKeyPair('rsa',
{modulusLength:bits,
publicKeyEncoding:{type:'pkcs1',format:'pem'},
privateKeyEncoding:{type:'pkcs1',format:'pem'}},
(err,publicKey,privateKey)=>
err==null?resolve({privateKey:privateKey,publicKey:publicKey}):reject(err)))
const address = publicKey
const key = privateKey
return [address, key]
}

function send(pub, message) {
const public = new NodeRSA(pub, 'pkcs8-public-der')
return public.encrypt(message)
function createSync(bits = 4096) {
const {privateKey, publicKey} =
crypto.generateKeyPairSync('rsa',
{modulusLength:bits,
publicKeyEncoding:{type:'pkcs1',format:'pem'},
privateKeyEncoding:{type:'pkcs1',format:'pem'}})
const address = publicKey
const key = privateKey
return [address, key]
}

function receive(pri, message) {
const private = new NodeRSA(pri, 'pkcs8-private-der')
return private.decrypt(message)
function send(address, message) {
return Uint8Array.from(crypto.publicEncrypt(address, message))
}

function signature(pri, message) {
const private = new NodeRSA(pri, 'pkcs8-private-der')
return private.encryptPrivate(message)
function receive(key, message) {
return Uint8Array.from(crypto.privateDecrypt(key, message))
}

function un_signature(pub, message) {
const public = new NodeRSA(pub, 'pkcs8-public-der')
return public.decryptPublic(message)
function signature(key, message) {
return Uint8Array.from(crypto.privateEncrypt(key, message))
}

function un_signature(address, message) {
return Uint8Array.from(crypto.publicDecrypt(address, message))
}

module.exports = {
create: create,
create: createSync,
createAsync: createAsync,
send: send,
receive: receive,
signature: signature,
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"repository": "https://github.com/zaoqi/uninet-address",
"author": "zaoqi <[email protected]>",
"license": "AGPL-3.0-or-later",
"dependencies": {
"node-rsa": "^1.0.6"
},
"devDependencies": {
"randomstring": "^1.1.5",
"tape": "^4.11.0"
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('send/receive', t => {
t.plan(5*100)
gen(()=>mod.create(), 5).map(([address, key]) => {
gen_str(100).map(msg => {
t.equal(mod.receive(key, mod.send(address, Buffer.from(msg))).toString(), msg)
t.equal(Buffer.from(mod.receive(key, mod.send(address, Buffer.from(msg)))).toString(), msg)
})
})
})
Expand All @@ -29,7 +29,7 @@ test('signature/un_signature', t => {
t.plan(5*100)
gen(()=>mod.create(), 5).map(([address, key]) => {
gen_str(100).map(msg => {
t.equal(mod.un_signature(address, mod.signature(key, Buffer.from(msg))).toString(), msg)
t.equal(Buffer.from(mod.un_signature(address, mod.signature(key, Buffer.from(msg)))).toString(), msg)
})
})
})
19 changes: 0 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-1.0.2.tgz#5fcc373920775723cfd64d65c64bef53bf9eba6d"
integrity sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0=

asn1@^0.2.4:
version "0.2.4"
resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136"
integrity sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==
dependencies:
safer-buffer "~2.1.0"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -164,13 +157,6 @@ minimist@~1.2.0:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=

node-rsa@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/node-rsa/-/node-rsa-1.0.6.tgz#47d22eba8b41192cd6f06db15870c67126f00aa4"
integrity sha512-v42495lozKpuQmrcIzld9ds/Tn7pwjuh0BHSHnhPrKkAVSyTAyrZodFLFafOfWiUKamLt4lgWdngP8W/LzCm2w==
dependencies:
asn1 "^0.2.4"

object-inspect@^1.6.0, object-inspect@~1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.6.0.tgz#c70b6cbf72f274aab4c34c0c82f5167bf82cf15b"
Expand Down Expand Up @@ -219,11 +205,6 @@ resumer@~0.0.0:
dependencies:
through "~2.3.4"

safer-buffer@~2.1.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

string.prototype.trim@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz#d04de2c89e137f4d7d206f086b5ed2fae6be8cea"
Expand Down

0 comments on commit bba4f42

Please sign in to comment.