Skip to content

Commit e1350ef

Browse files
committed
refactor: move base64url around
1 parent 03d72c8 commit e1350ef

20 files changed

+124
-125
lines changed

src/jwe/flattened/decrypt.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import type * as types from '../../types.d.ts'
8-
import { decode as base64url } from '../../lib/base64url.js'
8+
import { decode as b64u } from '../../util/base64url.js'
99
import decrypt from '../../lib/decrypt.js'
1010
import { JOSEAlgNotAllowed, JOSENotSupported, JWEInvalid } from '../../util/errors.js'
1111
import isDisjoint from '../../lib/is_disjoint.js'
@@ -122,7 +122,7 @@ export async function flattenedDecrypt(
122122
let parsedProt!: types.JWEHeaderParameters
123123
if (jwe.protected) {
124124
try {
125-
const protectedHeader = base64url(jwe.protected)
125+
const protectedHeader = b64u(jwe.protected)
126126
parsedProt = JSON.parse(decoder.decode(protectedHeader))
127127
} catch {
128128
throw new JWEInvalid('JWE Protected Header is invalid')
@@ -178,7 +178,7 @@ export async function flattenedDecrypt(
178178
let encryptedKey!: Uint8Array
179179
if (jwe.encrypted_key !== undefined) {
180180
try {
181-
encryptedKey = base64url(jwe.encrypted_key!)
181+
encryptedKey = b64u(jwe.encrypted_key!)
182182
} catch {
183183
throw new JWEInvalid('Failed to base64url decode the encrypted_key')
184184
}
@@ -213,14 +213,14 @@ export async function flattenedDecrypt(
213213
let tag: Uint8Array | undefined
214214
if (jwe.iv !== undefined) {
215215
try {
216-
iv = base64url(jwe.iv)
216+
iv = b64u(jwe.iv)
217217
} catch {
218218
throw new JWEInvalid('Failed to base64url decode the iv')
219219
}
220220
}
221221
if (jwe.tag !== undefined) {
222222
try {
223-
tag = base64url(jwe.tag)
223+
tag = b64u(jwe.tag)
224224
} catch {
225225
throw new JWEInvalid('Failed to base64url decode the tag')
226226
}
@@ -237,7 +237,7 @@ export async function flattenedDecrypt(
237237

238238
let ciphertext: Uint8Array
239239
try {
240-
ciphertext = base64url(jwe.ciphertext)
240+
ciphertext = b64u(jwe.ciphertext)
241241
} catch {
242242
throw new JWEInvalid('Failed to base64url decode the ciphertext')
243243
}
@@ -251,7 +251,7 @@ export async function flattenedDecrypt(
251251

252252
if (jwe.aad !== undefined) {
253253
try {
254-
result.additionalAuthenticatedData = base64url(jwe.aad!)
254+
result.additionalAuthenticatedData = b64u(jwe.aad!)
255255
} catch {
256256
throw new JWEInvalid('Failed to base64url decode the aad')
257257
}

src/jwe/flattened/encrypt.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @module
55
*/
66

7-
import { encode as base64url } from '../../lib/base64url.js'
7+
import { encode as b64u } from '../../util/base64url.js'
88
import { unprotected } from '../../lib/private_symbols.js'
99
import encrypt from '../../lib/encrypt.js'
1010
import type * as types from '../../types.d.ts'
@@ -249,13 +249,13 @@ export class FlattenedEncrypt {
249249
let protectedHeader: Uint8Array
250250
let aadMember: string | undefined
251251
if (this._protectedHeader) {
252-
protectedHeader = encoder.encode(base64url(JSON.stringify(this._protectedHeader)))
252+
protectedHeader = encoder.encode(b64u(JSON.stringify(this._protectedHeader)))
253253
} else {
254254
protectedHeader = encoder.encode('')
255255
}
256256

257257
if (this._aad) {
258-
aadMember = base64url(this._aad)
258+
aadMember = b64u(this._aad)
259259
additionalData = concat(protectedHeader, encoder.encode('.'), encoder.encode(aadMember))
260260
} else {
261261
additionalData = protectedHeader
@@ -270,19 +270,19 @@ export class FlattenedEncrypt {
270270
)
271271

272272
const jwe: types.FlattenedJWE = {
273-
ciphertext: base64url(ciphertext),
273+
ciphertext: b64u(ciphertext),
274274
}
275275

276276
if (iv) {
277-
jwe.iv = base64url(iv)
277+
jwe.iv = b64u(iv)
278278
}
279279

280280
if (tag) {
281-
jwe.tag = base64url(tag)
281+
jwe.tag = b64u(tag)
282282
}
283283

284284
if (encryptedKey) {
285-
jwe.encrypted_key = base64url(encryptedKey)
285+
jwe.encrypted_key = b64u(encryptedKey)
286286
}
287287

288288
if (aadMember) {

src/jwe/general/encrypt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { JOSENotSupported, JWEInvalid } from '../../util/errors.js'
1111
import generateCek from '../../lib/cek.js'
1212
import isDisjoint from '../../lib/is_disjoint.js'
1313
import encryptKeyManagement from '../../lib/encrypt_key_management.js'
14-
import { encode as base64url } from '../../lib/base64url.js'
14+
import { encode as b64u } from '../../util/base64url.js'
1515
import validateCrit from '../../lib/validate_crit.js'
1616
import normalizeKey from '../../lib/normalize_key.js'
1717
import checkKeyType from '../../lib/check_key_type.js'
@@ -301,7 +301,7 @@ export class GeneralEncrypt {
301301

302302
const k = await normalizeKey(recipient.key, alg)
303303
const { encryptedKey, parameters } = await encryptKeyManagement(alg, enc, k, cek, { p2c })
304-
target.encrypted_key = base64url(encryptedKey!)
304+
target.encrypted_key = b64u(encryptedKey!)
305305
if (recipient.unprotectedHeader || parameters)
306306
target.header = { ...recipient.unprotectedHeader, ...parameters }
307307
}

src/jwk/thumbprint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import digest from '../lib/digest.js'
8-
import { encode as base64url } from '../lib/base64url.js'
8+
import { encode as b64u } from '../util/base64url.js'
99

1010
import { JOSENotSupported, JWKInvalid } from '../util/errors.js'
1111
import { encoder } from '../lib/buffer_utils.js'
@@ -97,7 +97,7 @@ export async function calculateJwkThumbprint(
9797
}
9898

9999
const data = encoder.encode(JSON.stringify(components))
100-
return base64url(await digest(digestAlgorithm, data))
100+
return b64u(await digest(digestAlgorithm, data))
101101
}
102102

103103
/**

src/jws/flattened/sign.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import type * as types from '../../types.d.ts'
8-
import { encode as base64url } from '../../lib/base64url.js'
8+
import { encode as b64u } from '../../util/base64url.js'
99
import sign from '../../lib/sign.js'
1010

1111
import isDisjoint from '../../lib/is_disjoint.js'
@@ -130,12 +130,12 @@ export class FlattenedSign {
130130

131131
let payload = this._payload
132132
if (b64) {
133-
payload = encoder.encode(base64url(payload))
133+
payload = encoder.encode(b64u(payload))
134134
}
135135

136136
let protectedHeader: Uint8Array
137137
if (this._protectedHeader) {
138-
protectedHeader = encoder.encode(base64url(JSON.stringify(this._protectedHeader)))
138+
protectedHeader = encoder.encode(b64u(JSON.stringify(this._protectedHeader)))
139139
} else {
140140
protectedHeader = encoder.encode('')
141141
}
@@ -146,7 +146,7 @@ export class FlattenedSign {
146146
const signature = await sign(alg, k, data)
147147

148148
const jws: types.FlattenedJWS = {
149-
signature: base64url(signature),
149+
signature: b64u(signature),
150150
payload: '',
151151
}
152152

src/jws/flattened/verify.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import type * as types from '../../types.d.ts'
8-
import { decode as base64url } from '../../lib/base64url.js'
8+
import { decode as b64u } from '../../util/base64url.js'
99
import verify from '../../lib/verify.js'
1010

1111
import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js'
@@ -106,7 +106,7 @@ export async function flattenedVerify(
106106
let parsedProt: types.JWSHeaderParameters = {}
107107
if (jws.protected) {
108108
try {
109-
const protectedHeader = base64url(jws.protected)
109+
const protectedHeader = b64u(jws.protected)
110110
parsedProt = JSON.parse(decoder.decode(protectedHeader))
111111
} catch {
112112
throw new JWSInvalid('JWS Protected Header is invalid')
@@ -176,7 +176,7 @@ export async function flattenedVerify(
176176
)
177177
let signature: Uint8Array
178178
try {
179-
signature = base64url(jws.signature)
179+
signature = b64u(jws.signature)
180180
} catch {
181181
throw new JWSInvalid('Failed to base64url decode the signature')
182182
}
@@ -191,7 +191,7 @@ export async function flattenedVerify(
191191
let payload: Uint8Array
192192
if (b64) {
193193
try {
194-
payload = base64url(jws.payload)
194+
payload = b64u(jws.payload)
195195
} catch {
196196
throw new JWSInvalid('Failed to base64url decode the payload')
197197
}

src/jwt/unsecured.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @module
55
*/
66

7-
import * as base64url from '../lib/base64url.js'
7+
import * as b64u from '../util/base64url.js'
88

99
import type * as types from '../types.d.ts'
1010
import { decoder } from '../lib/buffer_utils.js'
@@ -55,8 +55,8 @@ export interface UnsecuredResult<PayloadType = types.JWTPayload> {
5555
export class UnsecuredJWT extends ProduceJWT {
5656
/** Encodes the Unsecured JWT. */
5757
encode(): string {
58-
const header = base64url.encode(JSON.stringify({ alg: 'none' }))
59-
const payload = base64url.encode(JSON.stringify(this._payload))
58+
const header = b64u.encode(JSON.stringify({ alg: 'none' }))
59+
const payload = b64u.encode(JSON.stringify(this._payload))
6060

6161
return `${header}.${payload}.`
6262
}
@@ -82,15 +82,15 @@ export class UnsecuredJWT extends ProduceJWT {
8282

8383
let header: types.JWSHeaderParameters
8484
try {
85-
header = JSON.parse(decoder.decode(base64url.decode(encodedHeader)))
85+
header = JSON.parse(decoder.decode(b64u.decode(encodedHeader)))
8686
if (header.alg !== 'none') throw new Error()
8787
} catch {
8888
throw new JWTInvalid('Invalid Unsecured JWT')
8989
}
9090

9191
const payload = jwtPayload(
9292
header,
93-
base64url.decode(encodedPayload),
93+
b64u.decode(encodedPayload),
9494
options,
9595
) as UnsecuredResult<PayloadType>['payload']
9696

src/key/import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @module
55
*/
66

7-
import { decode as decodeBase64URL } from '../lib/base64url.js'
7+
import { decode as decodeBase64URL } from '../util/base64url.js'
88
import { fromSPKI, fromPKCS8, fromX509 } from '../lib/asn1.js'
99
import toCryptoKey from '../lib/jwk_to_key.js'
1010

src/lib/aesgcmkw.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import encrypt from './encrypt.js'
22
import decrypt from './decrypt.js'
3-
import { encode as base64url } from '../lib/base64url.js'
3+
import { encode as b64u } from '../util/base64url.js'
44

55
export async function wrap(alg: string, key: unknown, cek: Uint8Array, iv?: Uint8Array) {
66
const jweAlgorithm = alg.slice(0, 7)
@@ -9,8 +9,8 @@ export async function wrap(alg: string, key: unknown, cek: Uint8Array, iv?: Uint
99

1010
return {
1111
encryptedKey: wrapped.ciphertext,
12-
iv: base64url(wrapped.iv!),
13-
tag: base64url(wrapped.tag!),
12+
iv: b64u(wrapped.iv!),
13+
tag: b64u(wrapped.tag!),
1414
}
1515
}
1616

src/lib/asn1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as types from '../types.d.ts'
22
import invalidKeyInput from './invalid_key_input.js'
3-
import { encodeBase64, decodeBase64 } from './base64url.js'
3+
import { encodeBase64, decodeBase64 } from '../lib/base64.js'
44
import { JOSENotSupported } from '../util/errors.js'
55
import { isCryptoKey, isKeyObject } from './is_key_like.js'
66

src/lib/base64.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function encodeBase64(input: Uint8Array): string {
2+
// @ts-ignore
3+
if (Uint8Array.prototype.toBase64) {
4+
// @ts-ignore
5+
return input.toBase64()
6+
}
7+
8+
const CHUNK_SIZE = 0x8000
9+
const arr = []
10+
for (let i = 0; i < input.length; i += CHUNK_SIZE) {
11+
// @ts-expect-error
12+
arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)))
13+
}
14+
return btoa(arr.join(''))
15+
}
16+
17+
export function decodeBase64(encoded: string): Uint8Array {
18+
// @ts-ignore
19+
if (Uint8Array.fromBase64) {
20+
// @ts-ignore
21+
return Uint8Array.fromBase64(encoded)
22+
}
23+
24+
const binary = atob(encoded)
25+
const bytes = new Uint8Array(binary.length)
26+
for (let i = 0; i < binary.length; i++) {
27+
bytes[i] = binary.charCodeAt(i)
28+
}
29+
return bytes
30+
}

src/lib/base64url.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)