pem.js 614 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. const inherits = require('inherits');
  3. const DEREncoder = require('./der');
  4. function PEMEncoder(entity) {
  5. DEREncoder.call(this, entity);
  6. this.enc = 'pem';
  7. }
  8. inherits(PEMEncoder, DEREncoder);
  9. module.exports = PEMEncoder;
  10. PEMEncoder.prototype.encode = function encode(data, options) {
  11. const buf = DEREncoder.prototype.encode.call(this, data);
  12. const p = buf.toString('base64');
  13. const out = [ '-----BEGIN ' + options.label + '-----' ];
  14. for (let i = 0; i < p.length; i += 64)
  15. out.push(p.slice(i, i + 64));
  16. out.push('-----END ' + options.label + '-----');
  17. return out.join('\n');
  18. };