mirror of
https://git.coolaj86.com/coolaj86/old-keypairs.js
synced 2025-03-14 12:20:42 +00:00
add PEM parser / packer
This commit is contained in:
parent
bf86aa8964
commit
6b3f1cb6a1
13
lib/pem-packer.js
Normal file
13
lib/pem-packer.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var PEM = module.exports;
|
||||||
|
var Enc = require('./encoding.js');
|
||||||
|
|
||||||
|
PEM.packBlock = function (opts) {
|
||||||
|
var n = opts.lineLength || 64;
|
||||||
|
var re = new RegExp('.{1,' + n + '}', 'g');
|
||||||
|
return '-----BEGIN ' + opts.type + '-----\n'
|
||||||
|
+ Enc.bufToBase64(opts.bytes).match(re).join('\n') + '\n'
|
||||||
|
+ '-----END ' + opts.type + '-----'
|
||||||
|
;
|
||||||
|
};
|
21
lib/pem-parser.js
Normal file
21
lib/pem-parser.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
'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('')) };
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user