-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.es6.js
120 lines (108 loc) · 2.79 KB
/
index.es6.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var crypto = require("crypto");
var eccrypto = require("eccrypto");
var protobuf = require("google-protobuf");
const secp256k1 = require('secp256k1')
const keystore =require('./lib/keystore')
const msgpack = require('./lib/msgpack/index')
/**
* create private key
*/
const createPrivateKey = ()=>{
return crypto.randomBytes(32)
}
/**
* create public key from private key
* @param {* buffer} privateKey
*/
const privateKey2pubKey = (privateKey)=>{
return eccrypto.getPublic(privateKey)
}
/**
* create public key and private key
*/
const createPubPrivateKeys = ()=>{
let privateKey = crypto.randomBytes(32)
let publicKey = eccrypto.getPublic(privateKey)
return {privateKey,publicKey}
}
/**
* sign message
* @param {* string} msg sign message
* @param {*} privateKey privateKey
*/
const sign = (msg,privateKey)=>{
const signObj = secp256k1.sign(msg,privateKey)
return signObj.signature;
}
/**
* verify the public key
* @param {* buffer} privateKey
* @param {* buffer } pubKey
*/
const isPublicKey = (privateKey,pubKey)=>{
let publicKey = eccrypto.getPublic(privateKey)
return publicKey.toString('hex')==pubKey.toString('hex')
}
/**
* verify the sign and signed message
* @param {* buffer} protoEncode proto encode buffer
* @param {* buffer} sign sign buffer
* @param {* buffer} publicKey private key
*/
const verify = (protoEncode,sign,publicKey)=>{
let msg = crypto.createHash("sha256").update(buf2hex(protoEncode.buffer)).digest()
return secp256k1.verify(msg,sign,publicKey)
}
/**
* aes encrypto
* @param {* string} message
* @param {* string} secretKey
*/
const aesEncrypto = (message,secretKey)=>{
return cryptojs.AES.encrypt(message, secretKey);
}
/**
* aes decrypto
* @param {* buffer} aesSecretMessage
* @param {* string} secretKey
*/
const aesDecrypto = (aesSecretMessage, secretKey)=>{
let bytes = cryptojs.AES.decrypt(aesSecretMessage, secretKey);
let plaintext = bytes.toString(cryptojs.enc.Utf8);
return plaintext;
}
/**
* buffer type to string
* @param {* buffer} buffer
*/
const buf2hex = (buffer)=>{
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
/**
* string type to buffer
* @param {* string} str
* @param {* hex || base64} enc
*/
const str2buf = (str, enc = "hex")=>{
if (!str || str.constructor !== String) return str;
if (!enc && this.isHex(str)) enc = "hex";
if (!enc && this.isBase64(str)) enc = "base64";
return Buffer.from(str, enc);
}
const sha256 = (msg)=>{
return crypto.createHash("sha256").update(msg).digest()
}
module.exports = {
createPubPrivateKeys,
isPublicKey,
privateKey2pubKey,
sign,
verify,
aesEncrypto,
aesDecrypto,
buf2hex,
str2buf,
sha256,
keystore,
msgpack
}