-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
54 lines (44 loc) · 1.46 KB
/
index.js
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
module.exports = function (pem) {
if (typeof pem !== 'string') pem = String(pem);
var m = /^-----BEGIN RSA (PRIVATE|PUBLIC) KEY-----/.exec(pem);
if (!m) return undefined;
var type = m[1].toLowerCase();
if (pem.split('\n').slice(-2)[0] !== '-----END RSA ' + m[1] + ' KEY-----') {
return undefined;
}
var buf = Buffer(pem.split('\n').slice(1,-2).join(''), 'base64');
var field = {};
var size = {};
var offset = {
private : buf[1] & 0x80 ? buf[1] - 0x80 + 5 : 7,
public : buf[1] & 0x80 ? buf[1] - 0x80 + 2 : 2,
}[type];
function read () {
var s = buf.readUInt8(offset + 1);
if (s & 0x80) {
var n = s - 0x80;
s = buf[[
'readUInt8', 'readUInt16BE'
][n - 1]](offset + 2);
offset += n;
}
offset += 2;
var b = buf.slice(offset, offset + s);
offset += s;
return b;
}
field.modulus = read();
field.bits = (field.modulus.length - 1) * 8 + Math.ceil(
Math.log(field.modulus[0] + 1) / Math.log(2)
);
field.publicExponent = parseInt(read().toString('hex'), 16);
if (type === 'private') {
field.privateExponent = read();
field.prime1 = read();
field.prime2 = read();
field.exponent1 = read();
field.exponent2 = read();
field.coefficient = read();
}
return field;
};