-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (43 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const crypto = require('crypto')
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 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 send(address, message) {
return Uint8Array.from(crypto.publicEncrypt(address, message))
}
function receive(key, message) {
return Uint8Array.from(crypto.privateDecrypt(key, 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: createSync,
createAsync: createAsync,
send: send,
receive: receive,
signature: signature,
un_signature: un_signature,
}