acme.js/lib/pem.js

34 lines
591 B
JavaScript
Raw Normal View History

2019-10-04 23:35:59 +00:00
'use strict';
2019-10-08 10:33:14 +00:00
var Enc = require('@root/encoding/base64');
2019-10-04 23:35:59 +00:00
var PEM = module.exports;
PEM.packBlock = function(opts) {
// TODO allow for headers?
return (
'-----BEGIN ' +
opts.type +
'-----\n' +
Enc.bufToBase64(opts.bytes)
.match(/.{1,64}/g)
.join('\n') +
'\n' +
'-----END ' +
opts.type +
'-----'
);
};
// don't replace the full parseBlock, if it exists
PEM.parseBlock =
PEM.parseBlock ||
function(str) {
var der = str
.split(/\n/)
.filter(function(line) {
return !/-----/.test(line);
})
.join('');
return { bytes: Enc.base64ToBuf(der) };
};