@@ -24,14 +24,14 @@ export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
24
24
const values : asn1js . Integer [ ] = result . valueBlock . value
25
25
26
26
const key = {
27
- n : uint8ArrayToString ( bnToBuf ( values [ 1 ] . toBigInt ( ) ) , 'base64url' ) ,
28
- e : uint8ArrayToString ( bnToBuf ( values [ 2 ] . toBigInt ( ) ) , 'base64url' ) ,
29
- d : uint8ArrayToString ( bnToBuf ( values [ 3 ] . toBigInt ( ) ) , 'base64url' ) ,
30
- p : uint8ArrayToString ( bnToBuf ( values [ 4 ] . toBigInt ( ) ) , 'base64url' ) ,
31
- q : uint8ArrayToString ( bnToBuf ( values [ 5 ] . toBigInt ( ) ) , 'base64url' ) ,
32
- dp : uint8ArrayToString ( bnToBuf ( values [ 6 ] . toBigInt ( ) ) , 'base64url' ) ,
33
- dq : uint8ArrayToString ( bnToBuf ( values [ 7 ] . toBigInt ( ) ) , 'base64url' ) ,
34
- qi : uint8ArrayToString ( bnToBuf ( values [ 8 ] . toBigInt ( ) ) , 'base64url' ) ,
27
+ n : asn1jsIntegerToBase64 ( values [ 1 ] ) ,
28
+ e : asn1jsIntegerToBase64 ( values [ 2 ] ) ,
29
+ d : asn1jsIntegerToBase64 ( values [ 3 ] ) ,
30
+ p : asn1jsIntegerToBase64 ( values [ 4 ] ) ,
31
+ q : asn1jsIntegerToBase64 ( values [ 5 ] ) ,
32
+ dp : asn1jsIntegerToBase64 ( values [ 6 ] ) ,
33
+ dq : asn1jsIntegerToBase64 ( values [ 7 ] ) ,
34
+ qi : asn1jsIntegerToBase64 ( values [ 8 ] ) ,
35
35
kty : 'RSA' ,
36
36
alg : 'RS256'
37
37
}
@@ -78,8 +78,8 @@ export function pkixToJwk (bytes: Uint8Array): JsonWebKey {
78
78
79
79
return {
80
80
kty : 'RSA' ,
81
- n : uint8ArrayToString ( bnToBuf ( values [ 0 ] . toBigInt ( ) ) , 'base64url' ) ,
82
- e : uint8ArrayToString ( bnToBuf ( values [ 1 ] . toBigInt ( ) ) , 'base64url' )
81
+ n : asn1jsIntegerToBase64 ( values [ 0 ] ) ,
82
+ e : asn1jsIntegerToBase64 ( values [ 1 ] )
83
83
}
84
84
}
85
85
@@ -120,26 +120,15 @@ export function jwkToPkix (jwk: JsonWebKey): Uint8Array {
120
120
return new Uint8Array ( der , 0 , der . byteLength )
121
121
}
122
122
123
- function bnToBuf ( bn : bigint ) : Uint8Array {
124
- let hex = bn . toString ( 16 )
123
+ function asn1jsIntegerToBase64 ( int : asn1js . Integer ) : string {
124
+ let buf = int . valueBlock . valueHexView
125
125
126
- if ( hex . length % 2 > 0 ) {
127
- hex = `0${ hex } `
126
+ // chrome rejects values with leading 0s
127
+ while ( buf [ 0 ] === 0 ) {
128
+ buf = buf . subarray ( 1 )
128
129
}
129
130
130
- const len = hex . length / 2
131
- const u8 = new Uint8Array ( len )
132
-
133
- let i = 0
134
- let j = 0
135
-
136
- while ( i < len ) {
137
- u8 [ i ] = parseInt ( hex . slice ( j , j + 2 ) , 16 )
138
- i += 1
139
- j += 2
140
- }
141
-
142
- return u8
131
+ return uint8ArrayToString ( buf , 'base64url' )
143
132
}
144
133
145
134
function bufToBn ( u8 : Uint8Array ) : bigint {
0 commit comments