hd-auth-wallet/lib/core/cryptoUtil.js
Kgothatso de7bd68872 Adding the cryptography stuff
This is probably the wrong way to use the cryptography.
2019-11-24 23:03:27 +02:00

60 lines
2.0 KiB
JavaScript

const crypto = require('crypto');
const algorithm = 'aes-256-ecb';
const bip32 = require('bip32');
// NOTE I'm not a cryptographer... so don't be reusing any code from below as I don't have no idea what I'm doing
const iv = null; // If I knew what I was doing I would have a value assigned here...
module.exports.encrypt = (plainText, password) => {
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
const cipher = crypto.createCipheriv(algorithm, key, iv);
var cipherText = cipher.update(plainText, 'utf8', 'hex');
cipherText += cipher.final('hex');
return cipherText;
}
module.exports.decrypt = (cipherText, password) => {
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
const decipher = crypto.createDecipheriv(algorithm, key, iv);
var plainText = decipher.update(cipherText, 'hex', 'utf8');
plainText += decipher.final('utf8');
return plainText;
}
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);
}