# Code coverage report for parseASN1/fixProc.js

## Statements: 100% (28 / 28) Branches: 100% (4 / 4) Functions: 100% (2 / 2) Lines: 100% (28 / 28) Ignored: none

All files » parseASN1/ » fixProc.js

    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
    361
    1
    1
    1
    7
    7
    7
    4

    3
    3
    3
    3
    3
    3
    3
    3
    3
    3
    3

    1
    3
    3
    48
    3
    3

    45
    45

    3
     var findProc = /Proc-Type: 4,ENCRYPTED\n\r?DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\n\r?\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?/m;
    var startRegex = /^-----BEGIN (.*)-----\n/;
    var evp = require('./EVP_BytesToKey');
    module.exports = function (okey, password, crypto) {
      var key = okey.toString();
      var match = key.match(findProc);
      if (!match) {
        return okey;
      }
      var suite = 'aes' + match[1];
      var iv = new Buffer(match[2], 'hex');
      var cipherText = new Buffer(match[3].replace(/\n\r?/g, ''), 'base64');
      var cipherKey = evp(crypto, password, iv.slice(0,8), parseInt(match[1]));
      var out = [];
      var cipher = crypto.createDecipheriv(suite, cipherKey, iv);
      out.push(cipher.update(cipherText));
      out.push(cipher.final());
      var decrypted = Buffer.concat(out).toString('base64');
      var tag = key.match(startRegex)[1];
      return '-----BEGIN ' + tag + "-----\n" + wrap(decrypted) + "\n" + '-----END ' + tag + "-----\n";
    };
    // http://stackoverflow.com/a/7033705
    function wrap(str) {
      var chunks = [];
      while (str) {
        if (str.length < 64) {
          chunks.push(str);
          break;
        }
        else {
          chunks.push(str.slice(0, 64));
          str = str.slice(64);
        }
      }
      return chunks.join("\n");
    }