All files / crypto/src/Encryption/Pascal PrivateKey.js

100% Statements 23/23
0% Branches 0/2
100% Functions 2/2
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x 1x 1x   1x 1x 1x                         51x 51x 51x 51x     51x   51x   51x                     12x 12x   12x 12x     12x   12x   12x   12x       1x  
const Abstract = require('./../Abstract');
const KDF = require('./KDF');
const BC = require('@pascalcoin-sbx/common').BC;
const CBCPKCS7 = require('./../AES/CBCPKCS7');
 
const Random = require('mipher/dist/random');
const PrivateKeyCoder = require('@pascalcoin-sbx/common').Coding.Pascal.Keys.PrivateKey;
const privKeyCoder = new PrivateKeyCoder();
 
class PrivateKey extends Abstract {
 
  /**
   * Creates a new keypair from the given private key.
   *
   * @param {Buffer|Uint8Array|BC|String} encryptedPrivateKey
   * @param {Buffer|Uint8Array|BC|String} password
   * @returns {KeyPair}
   */
  static decrypt(value, options = {password: ''}) {
 
    value = BC.from(value);
    let password = BC.from(options.password, 'string');
    let salt = value.slice(8, 16);
    let key = KDF.PascalCoin(password, salt);
 
    // decrypt
    const encData = value.slice(16);
 
    const privateKeyDecryptedAndEncoded = CBCPKCS7.decrypt(encData, key);
 
    return privKeyCoder.decodeFromBytes(privateKeyDecryptedAndEncoded);
  }
 
  /**
   * Creates a new keypair from the given private key.
   *
   * @param {PrivateKey} value
   * @param {Buffer|Uint8Array|BC|String} password
   * @returns {BC}
   */
  static encrypt(value, options = {password: ''}) {
    let password = BC.from(options.password, 'string');
    const privateKeyEncoded = privKeyCoder.encodeToBytes(value);
 
    const randomGenerator = new Random.Random();
    const salt = new BC(Buffer.from(randomGenerator.get(8)));
 
    // mocha sees an open setinterval and won't exit without this change
    randomGenerator.stop();
 
    const keyInfo = KDF.PascalCoin(password, salt);
 
    const privateKeyEncrypted = CBCPKCS7.encrypt(privateKeyEncoded, keyInfo);
 
    return BC.concat(BC.fromString('Salted__'), salt, privateKeyEncrypted);
  }
}
 
module.exports = PrivateKey;