Convert Encryption to ES6
This commit is contained in:
parent
02f5af4bc0
commit
c39a991edf
@ -2,16 +2,11 @@
|
||||
|
||||
var fs = require('fs');
|
||||
var crypto = require('crypto');
|
||||
var Base58 = require('bs58');
|
||||
|
||||
var algorithm = 'aes-256-ecb';
|
||||
var useEncryption = true;
|
||||
|
||||
/* ENCRYPTION / DECRYPTION */
|
||||
function encrypt(text, privkey, pubkey) {
|
||||
if(!useEncryption)
|
||||
return text;
|
||||
|
||||
class Encryption {
|
||||
static encrypt(text, privkey, pubkey) {
|
||||
var encrypted;
|
||||
try {
|
||||
var cipher = crypto.createCipher(algorithm, privkey)
|
||||
@ -21,12 +16,9 @@ function encrypt(text, privkey, pubkey) {
|
||||
console.log("Error while encrypting:", e, e.stack);
|
||||
}
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
function decrypt(text, privkey, pubkey) {
|
||||
if(!useEncryption)
|
||||
return text;
|
||||
}
|
||||
|
||||
static decrypt(text, privkey, pubkey) {
|
||||
var decrypted;
|
||||
try {
|
||||
var cipher = crypto.createDecipher(algorithm, privkey)
|
||||
@ -36,50 +28,34 @@ function decrypt(text, privkey, pubkey) {
|
||||
console.log("Error while decrypting:", e, e.stack);
|
||||
}
|
||||
return decrypted;
|
||||
}
|
||||
}
|
||||
|
||||
/* SIGNING FUNCTIONS */
|
||||
function hashWithSHA512(data, salt) {
|
||||
static hashWithSHA512(data, salt) {
|
||||
if(!salt) salt = "null";
|
||||
var hash = crypto.createHmac('sha512', salt);
|
||||
hash.update(data);
|
||||
var value = hash.digest('hex');
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
function sign(data, privkey, seq, salt) {
|
||||
static sign(data, privkey, seq, salt) {
|
||||
if(!salt) salt = "null";
|
||||
var sign = crypto.createSign('RSA-SHA256');
|
||||
var hash = hashWithSHA512(data, "" + salt)
|
||||
var hash = Encryption.hashWithSHA512(data, "" + salt)
|
||||
sign.update("" + seq + hash);
|
||||
var sig = sign.sign(privkey, 'hex');
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
|
||||
function verify(data, pubkey, sig, seq, salt) {
|
||||
static verify(data, pubkey, sig, seq, salt) {
|
||||
if(!salt) salt = "null";
|
||||
var verify = crypto.createVerify('RSA-SHA256');
|
||||
var hash = hashWithSHA512(data, salt);
|
||||
var hash = Encryption.hashWithSHA512(data, salt);
|
||||
verify.update("" + seq + hash);
|
||||
var verified = verify.verify(pubkey, sig, 'hex');
|
||||
return verified;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* PUBLIC */
|
||||
module.exports = {
|
||||
encrypt: function(payload, privkey, pubkey) {
|
||||
return encrypt(payload, privkey, pubkey);
|
||||
},
|
||||
decrypt: function(payload, privkey, pubkey) {
|
||||
return decrypt(payload, privkey, pubkey);
|
||||
},
|
||||
sign: (data, privateKey, sequenceNumber, salt) => {
|
||||
return sign(data, privateKey, sequenceNumber, salt);
|
||||
},
|
||||
verify: (data, publicKey, signature, sequenceNumber, salt) => {
|
||||
return verify(data, publicKey, signature, sequenceNumber, salt);
|
||||
},
|
||||
hash: (data, salt) => {
|
||||
return hashWithSHA512(data, salt);
|
||||
}
|
||||
}
|
||||
module.exports = Encryption;
|
||||
|
Loading…
x
Reference in New Issue
Block a user