-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
64 lines (52 loc) · 1.72 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
55
56
57
58
59
60
61
62
63
64
module.exports = rsaPublicKeyPem;
//http://stackoverflow.com/questions/18835132/xml-to-pem-in-node-js
function rsaPublicKeyPem(modulus_b64, exponent_b64) {
var modulus = Buffer.from(modulus_b64, 'base64');
var exponent = Buffer.from(exponent_b64, 'base64');
var modulus_hex = modulus.toString('hex')
var exponent_hex = exponent.toString('hex')
modulus_hex = prepadSigned(modulus_hex)
exponent_hex = prepadSigned(exponent_hex)
var modlen = modulus_hex.length/2
var explen = exponent_hex.length/2
var encoded_modlen = encodeLengthHex(modlen)
var encoded_explen = encodeLengthHex(explen)
var encoded_pubkey = '30' +
encodeLengthHex(
modlen +
explen +
encoded_modlen.length/2 +
encoded_explen.length/2 + 2
) +
'02' + encoded_modlen + modulus_hex +
'02' + encoded_explen + exponent_hex;
var der_b64 = Buffer.from(encoded_pubkey, 'hex').toString('base64');
var pem = '-----BEGIN RSA PUBLIC KEY-----\n'
+ der_b64.match(/.{1,64}/g).join('\n')
+ '\n-----END RSA PUBLIC KEY-----\n';
return pem
}
function prepadSigned(hexStr) {
var msb = hexStr[0]
if (msb < '0' || msb > '7') {
return '00'+hexStr;
} else {
return hexStr;
}
}
function toHex(number) {
var nstr = number.toString(16);
if (nstr.length%2) return '0'+nstr;
return nstr;
}
// encode ASN.1 DER length field
// if <=127, short form
// if >=128, long form
function encodeLengthHex(n) {
if (n<=127) return toHex(n)
else {
var n_hex = toHex(n)
var length_of_length_byte = 128 + n_hex.length/2 // 0x80+numbytes
return toHex(length_of_length_byte)+n_hex
}
}