2
0
mirror of https://git.coolaj86.com/coolaj86/old-keypairs.js synced 2025-03-14 12:20:42 +00:00
old-keypairs.js/lib/pem-parser.js

22 lines
528 B
JavaScript
Raw Normal View History

2018-12-17 21:08:46 -07:00
'use strict';
var PEM = module.exports;
var Enc = require('./encoding.js');
PEM.parseBlock = function pemToDer(pem) {
var lines = pem.trim().split(/\n/);
var end = lines.length - 1;
var head = lines[0].match(/-----BEGIN (.*)-----/);
var foot = lines[end].match(/-----END (.*)-----/);
if (head) {
lines = lines.slice(1, end);
head = head[1];
if (head !== foot[1]) {
throw new Error("headers and footers do not match");
}
}
return { type: head, bytes: Enc.base64ToBuf(lines.join('')) };
};