UNPKG

@open-rights-exchange/orejs

Version:

Orejs is a Javascript helper library to provide simple high-level access to the ore-protocol. Orejs uses eosJS as a wrapper to the EOS blockchain.

41 lines 1.5 kB
"use strict"; var sjcl = require('sjcl'); // PRIVATE // Encrypts the EOS private key with the derived key function encryptWithKey(unencrypted, key) { var encrypted = JSON.parse(sjcl.encrypt(key, unencrypted, { mode: 'gcm' })); return JSON.stringify(encrypted); } // PUBLIC // Derive the key used for encryption/decryption function deriveKey(password, salt) { // NOTE Passing in at least an empty string for the salt, will prevent cached keys, which can lead to false positives in the test suite var key = sjcl.misc.cachedPbkdf2(password, { iter: 1000, salt: salt || '' }).key; return key; } // Decrypts the encrypted EOS private key with the derived key function decryptWithKey(encrypted, key) { try { var encryptedData = JSON.stringify(Object.assign(JSON.parse(encrypted), { mode: 'gcm' })); return sjcl.decrypt(key, encryptedData); } catch (err) { // console.error('Decryption Error:', err); return ''; } } // Decrypts the encrypted EOS private key with wallet password, and salt function decrypt(encrypted, password, salt) { return decryptWithKey(encrypted, deriveKey(password, salt)); } // Encrypts the EOS private key with wallet password, and salt function encrypt(unencrypted, password, salt) { return encryptWithKey(unencrypted, deriveKey(password, salt)); } module.exports = { decrypt: decrypt, decryptWithKey: decryptWithKey, deriveKey: deriveKey, encrypt: encrypt }; //# sourceMappingURL=crypto.js.map