-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.js
38 lines (30 loc) · 1.03 KB
/
utils.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
"use strict";
let crypto = require('crypto');
var keypair = require('keypair');
// CRYPTO settings
const HASH_ALG = 'sha256';
const SIG_ALG = 'RSA-SHA256';
exports.hash = function hash(s, encoding) {
encoding = encoding || 'hex';
return crypto.createHash(HASH_ALG).update(s).digest(encoding);
}
exports.generateKeypair = function() {
return keypair();
}
exports.sign = function(privKey, msg) {
let signer = crypto.createSign(SIG_ALG);
// Convert an object to its JSON representation
let str = (msg === Object(msg)) ? JSON.stringify(msg) : ""+msg;
return signer.update(str).sign(privKey, 'hex');
}
exports.verifySignature = function(pubKey, msg, sig) {
let verifier = crypto.createVerify(SIG_ALG);
// Convert an object to its JSON representation
let str = (msg === Object(msg)) ? JSON.stringify(msg) : ""+msg;
return verifier.update(str).verify(pubKey, sig, 'hex');
}
exports.calcAddress = function(key) {
let addr = exports.hash(""+key, 'base64');
//console.log(`Generating address ${addr} from ${key}`);
return addr;
}