hd-auth-wallet/lib/core/cryptoUtil.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-11-23 22:34:59 +02:00
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
const bip32 = require('bip32');
module.exports.encrypt = (plainText, password) => {
// TODO encrypt text
// const cipher = crypto.createCipheriv(algorithm, password);
// var cipherText = cipher.update(plainText, 'utf8', 'hex');
// cipher += cipher.final('hex');
// console.log("CipherText: ", cipherText);
return plainText;
}
module.exports.decrypt = (cipherText, password) => {
// TODO decrypt cipherText
// const decipher = crypto.createDecipheriv(algorithm, password)
// var plainText = decipher.update(cipherText, 'hex', 'utf8');
// plainText += decipher.final('utf8');
return cipherText;
}
module.exports.randomDerivationPath = function(hardenedDerivation) {
// TODO harden derivation path
var randomNumbers = [];
for(var i = 0; i < 3; i++) {
randomNumbers.push(parseInt(crypto.randomBytes(3).toString('hex'), 16))
}
return randomNumbers.join('/')
}
module.exports.verifyMessage = function(base58Key, derivationPath, message, signature) {
if(derivationPath.startsWith("a/")) {
derivationPath = derivationPath.split("a/")[1];
}
if(derivationPath.startsWith("c/")) {
derivationPath = derivationPath.split("c/")[1];
}
const verificationNode = bip32.fromBase58(base58Key).derivePath(derivationPath);
const hash = crypto.createHash('sha256').update(message, 'utf8').digest();
return verificationNode.verify(hash, Buffer.from(signature, 'hex'));
}
module.exports.signMessage = function(base58Key, derivationPath, message) {
let signingNode = bip32.fromBase58(base58Key).derivePath(derivationPath);
var hash = crypto.createHash('sha256').update(message, 'utf8').digest();
return signingNode.sign(hash);
}