Sha256: dc54ccea1f3e6a303c50b59f40ded1b3ecca1b884f1e7766ddf682026ca596cf

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

var inherits = require('inherits');
var Buffer = require('buffer').Buffer;

var asn1 = require('../../asn1');
var DERDecoder = require('./der');

function PEMDecoder(entity) {
  DERDecoder.call(this, entity);
  this.enc = 'pem';
};
inherits(PEMDecoder, DERDecoder);
module.exports = PEMDecoder;

PEMDecoder.prototype.decode = function decode(data, options) {
  var lines = data.toString().split(/[\r\n]+/g);

  var label = options.label.toUpperCase();

  var re = /^-----(BEGIN|END) ([^-]+)-----$/;
  var start = -1;
  var end = -1;
  for (var i = 0; i < lines.length; i++) {
    var match = lines[i].match(re);
    if (match === null)
      continue;

    if (match[2] !== label)
      continue;

    if (start === -1) {
      if (match[1] !== 'BEGIN')
        break;
      start = i;
    } else {
      if (match[1] !== 'END')
        break;
      end = i;
      break;
    }
  }
  if (start === -1 || end === -1)
    throw new Error('PEM section not found for: ' + label);

  var base64 = lines.slice(start + 1, end).join('');
  // Remove excessive symbols
  base64.replace(/[^a-z0-9\+\/=]+/gi, '');

  var input = new Buffer(base64, 'base64');
  return DERDecoder.prototype.decode.call(this, input, options);
};

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
brwy_rails-0.0.6 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js
brwy_rails-0.0.5 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js
brwy_rails-0.0.4 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js
brwy_rails-0.0.3 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js
brwy_rails-0.0.2 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js
brwy_rails-0.0.1 test/dummy/node_modules/asn1.js/lib/asn1/decoders/pem.js