etkframework
Version:
First test release of Etk over colored coins SDK
93 lines (74 loc) • 3.83 kB
JavaScript
/**
* Etk Helper library.
* Encryption and decryption of HD Wallet seed over server without knowledge of seed.
*
* Copyright (C) 2015 Akul Mathur
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Parts of the software are provided under separate licenses, as follows:
"colu-nodejs" SDK is under the MIT License
"pbkdf2-sha256" is under the BSD License
"bip38" is under the MIT License
"scryptsy" is under the MIT License
"coinstring" is under the MIT License
* Core Developer(s): @codecakes Akul Mathur
* Maintainer(s):
* @codecakes Akul Mathur
*/
;
const
compoundedSalt = require("./passCrypt").compoundedSalt,
hashCipherText = require("./kdfCrypt").hashCipherText,
hashedLen = require("./kdfCrypt").hashedLen,
encrypt = require("./kdfCrypt").encrypt,
decrypt = require("./kdfCrypt").decrypt,
encryptSeed = function encryptSeed(masterpassword, seed, ivInputs, bufInputs, storeCB) {
ivInputs.unshift(masterpassword);
bufInputs.unshift(masterpassword);
const
ivSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,ivInputs) ) ) ),
bufSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) ) );
encrypt(masterpassword, seed, ivSalt, bufSalt, function cb(err, cipherText, iv) {
if (err) throw Error(err);
// this stores the transformed seed
storeCB(cipherText, iv);
});
},
decryptSeed = function decryptSeed(masterpassword, cipherText, iv, bufInputs, getCB) {
bufInputs.unshift(masterpassword);
let
bufSalt = hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) );
decrypt( masterpassword, bufSalt, cipherText, iv,
function cbDecrypt( err, decipherText ) {
if (err) throw Error(err);
getCB(decipherText.toString('utf8'));
});
};
exports.encryptSeed = encryptSeed;
exports.decryptSeed = decryptSeed;
// for testing and debugging purpose only
// const
// // 256 char long
// seed = 'c82425d18a88ea6c5a92d06bc4ee26bfaf1b8782e7c0537544ae6dd40bb5083bxprv9s21ZrQH143K4EdfsuwWHdxaGgcwGkTB1f71fbzJNfJma1Xga5XE3LqYxXKkwJJLevsp16iDRyk35MwvmKEEyyqLkHQVziTNs6VtPr1xGM81',
// masterpassword = "HaiIsPass";
// let
// ivInputs = ["band", "ho", "gaya", "Nee"],
// bufInputs = ["jhona", "launa"],
// ivSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,ivInputs) ) ) ),
// bufSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) ) );
// encryptSeed(masterpassword, seed, ivInputs, bufInputs, function storeCB(cipherText, iv) {
// console.log("Cipher text is: "+ cipherText);
// ivInputs = ["band", "ho", "gaya", "Nee"],
// bufInputs = ["jhona", "launa"],
// decryptSeed(masterpassword, cipherText, iv, bufInputs, function(DecipheredText) {
// console.log("DecipheredText: "+ DecipheredText);
// });
// });