This repository has been archived by the owner on Feb 25, 2019. It is now read-only.
/
RSA.js
175 lines (140 loc) · 3.76 KB
/
RSA.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// https://github.com/dannycoates/pem-jwk/blob/master/index.js
// https://tools.ietf.org/html/rfc3447#page-44
/**
* Package dependencies
*/
const asn = require('asn1.js')
const fs = require('fs')
const public_test = fs.readFileSync('./public.pem').toString("ascii")
const private_test = fs.readFileSync('./private.pem').toString("ascii")
function pad(hex) {
return (hex.length % 2 === 1) ? '0' + hex : hex
}
function urlize(base64) {
return base64.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
}
function hex2b64url(str) {
return urlize(Buffer(str, 'hex').toString('base64'))
}
function bn2base64url(bn) {
return hex2b64url(pad(bn.toString(16)))
}
function base64url2bn(str) {
return new asn.bignum(Buffer(str, 'base64'))
}
function string2bn(str) {
if (/^[0-9]+$/.test(str)) {
return new asn.bignum(str, 10)
}
return base64url2bn(str)
}
var Version = asn.define('Version', function () {
this.int({
0: 'two-prime',
1: 'multi'
})
})
var OtherPrimeInfos = asn.define('OtherPrimeInfos', function () {
this.seq().obj(
this.key('ri').int(),
this.key('di').int(),
this.key('ti').int()
)
})
let RSAPublicKey = asn.define('RSAPublicKey', function () {
this.seq().obj(
this.key('n').int(),
this.key('e').int()
)
})
var AlgorithmIdentifier = asn.define('AlgorithmIdentifier', function () {
this.seq().obj(
this.key('algorithm').objid(),
this.key('parameters').optional().any()
)
})
var RSAPrivateKey = asn.define('RSAPrivateKey', function () {
this.seq().obj(
this.key('version').use(Version),
this.key('n').int(),
this.key('e').int(),
this.key('d').int(),
this.key('p').int(),
this.key('q').int(),
this.key('dp').int(),
this.key('dq').int(),
this.key('qi').int(),
this.key('other').optional().use(OtherPrimeInfos)
)
})
var PublicKeyInfo = asn.define('PublicKeyInfo', function () {
this.seq().obj(
this.key('algorithm').use(AlgorithmIdentifier),
this.key('publicKey').bitstr()
)
})
var PrivateKeyInfo = asn.define('PrivateKeyInfo', function () {
this.seq().obj(
this.key('version').use(Version),
this.key('algorithm').use(AlgorithmIdentifier),
this.key('privateKey').bitstr()
)
})
function decodeRsaPublic(buffer,extras)
{
var key = RSAPublicKey.decode(buffer, 'der')
var e = pad(key.e.toString(16))
var jwk = {
kty: 'RSA',
n: bn2base64url(key.n),
e: hex2b64url(e)
}
return jwk
}
function decodeRsaPrivate(buffer, extras) {
console.log("decodeRsaPrivate")
var key = RSAPrivateKey.decode(buffer, 'der')
var e = pad(key.e.toString(16))
var jwk = {
kty: 'RSA',
n: bn2base64url(key.n),
e: hex2b64url(e),
d: bn2base64url(key.d),
p: bn2base64url(key.p),
q: bn2base64url(key.q),
dp: bn2base64url(key.dp),
dq: bn2base64url(key.dq),
qi: bn2base64url(key.qi)
}
return jwk
}
function decodePublic(buffer, extras) {
var info = PublicKeyInfo.decode(buffer, 'der')
return decodeRsaPublic(info.publicKey.data,extras)
}
function decodePrivate(buffer, extras) {
var info = PrivateKeyInfo.decode(buffer, 'der')
return decodeRsaPrivate(info.privateKey.data, extras)
}
var pem = private_test
console.log(pem)
var text = pem.split(/(\r\n|\r|\n)+/g)
text = text.filter(function(line) {
return line.trim().length !== 0
});
text = text.slice(1, -1).join('')
let bufferInput = text.replace(/[^\w\d\+\/=]+/g, '')
let buf = Buffer.from( bufferInput, 'base64' )
console.log(buf.toString("base64"))
let returnVal = decodeRsaPrivate(buf,'der')
console.log(returnVal)
/*
let output = Test.encode({ a: 'foo' }, 'der')
console.log(output.toString('base64'))
let decoded = Test.decode(output, 'der')
Object.keys(decoded).forEach(key => {
decoded[key] = decoded[key].toString('utf8')
})
*/