pem.all.js
889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;(function () {
'use strict';
var PEM = window.PEM = {};
var Enc = {};
// "A little copying is better than a little dependency" - Rob Pike
Enc.bufToBase64 = function(u8) {
var bin = '';
// map is not part of u8
u8.forEach(function(i) {
bin += String.fromCharCode(i);
});
return btoa(bin);
};
Enc.base64ToBuf = function(b64) {
return Uint8Array.from(
atob(b64)
.split('')
.map(function(ch) {
return ch.charCodeAt(0);
})
);
};
PEM.parseBlock = function(str) {
var der = str
.split(/\n/)
.filter(function(line) {
return !/-----/.test(line);
})
.join('');
return { bytes: Enc.base64ToBuf(der) };
};
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 +
'-----'
);
};
}());