Skip to content

Commit f8db275

Browse files
authored
fix: base64url convention (#179)
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
1 parent fec88f0 commit f8db275

File tree

10 files changed

+37
-37
lines changed

10 files changed

+37
-37
lines changed

packages/core/src/decoy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { HasherAndAlg, SaltGenerator } from '@sd-jwt/types';
2-
import { Uint8ArrayToBase64Url } from '@sd-jwt/utils';
2+
import { uint8ArrayToBase64Url } from '@sd-jwt/utils';
33

44
// This function creates a decoy value that can be used to obscure SD JWT payload.
55
// The value is basically a hash of a random salt. So the value is not predictable.
@@ -11,5 +11,5 @@ export const createDecoy = async (
1111
const { hasher, alg } = hash;
1212
const salt = await saltGenerator(16);
1313
const decoy = await hasher(salt, alg);
14-
return Uint8ArrayToBase64Url(decoy);
14+
return uint8ArrayToBase64Url(decoy);
1515
};

packages/core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SDJWTException, Uint8ArrayToBase64Url } from '@sd-jwt/utils';
1+
import { SDJWTException, uint8ArrayToBase64Url } from '@sd-jwt/utils';
22
import { Jwt } from './jwt';
33
import { KBJwt } from './kbjwt';
44
import { SDJwt, pack } from './sdjwt';
@@ -250,7 +250,7 @@ export class SDJwtInstance<ExtendedPayload extends SdJwtPayload> {
250250
}
251251
const { _sd_alg } = getSDAlgAndPayload(sdjwt.jwt.payload);
252252
const sdHash = await hasher(presentSdJwtWithoutKb, _sd_alg);
253-
const sdHashStr = Uint8ArrayToBase64Url(sdHash);
253+
const sdHashStr = uint8ArrayToBase64Url(sdHash);
254254
return sdHashStr;
255255
}
256256

packages/core/src/jwt.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils';
1+
import { base64urlEncode, SDJWTException } from '@sd-jwt/utils';
22
import type { Base64urlString, Signer, Verifier } from '@sd-jwt/types';
33
import { decodeJwt } from '@sd-jwt/decode';
44

@@ -83,8 +83,8 @@ export class Jwt<
8383
return unsignedToken;
8484
}
8585

86-
const header = Base64urlEncode(JSON.stringify(this.header));
87-
const payload = Base64urlEncode(JSON.stringify(this.payload));
86+
const header = base64urlEncode(JSON.stringify(this.header));
87+
const payload = base64urlEncode(JSON.stringify(this.payload));
8888
return `${header}.${payload}`;
8989
}
9090

@@ -104,8 +104,8 @@ export class Jwt<
104104
throw new SDJWTException('Serialize Error: Invalid JWT');
105105
}
106106

107-
const header = Base64urlEncode(JSON.stringify(this.header));
108-
const payload = Base64urlEncode(JSON.stringify(this.payload));
107+
const header = base64urlEncode(JSON.stringify(this.header));
108+
const payload = base64urlEncode(JSON.stringify(this.payload));
109109
const signature = this.signature;
110110
const compact = `${header}.${payload}.${signature}`;
111111
this.encoded = compact;

packages/core/src/kbjwt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils';
1+
import { SDJWTException } from '@sd-jwt/utils';
22
import { Jwt } from './jwt';
33
import {
44
type JwtPayload,

packages/core/src/test/decoy.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createDecoy } from '../decoy';
22
import { describe, expect, test } from 'vitest';
3-
import { Base64urlEncode } from '@sd-jwt/utils';
3+
import { base64urlEncode } from '@sd-jwt/utils';
44
import { digest, generateSalt } from '@sd-jwt/crypto-nodejs';
55

66
const hash = {
@@ -21,7 +21,7 @@ describe('Decoy', () => {
2121
// * Contents: ["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"]
2222
test('apply hasher and saltGenerator', async () => {
2323
const decoyValue = await createDecoy(hash, () =>
24-
Base64urlEncode(
24+
base64urlEncode(
2525
'["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"]',
2626
),
2727
);

packages/decode/src/decode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils';
1+
import { base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils';
22
import {
33
type Hasher,
44
type HasherAndAlg,
@@ -20,8 +20,8 @@ export const decodeJwt = <
2020
}
2121

2222
return {
23-
header: JSON.parse(Base64urlDecode(header)),
24-
payload: JSON.parse(Base64urlDecode(payload)),
23+
header: JSON.parse(base64urlDecode(header)),
24+
payload: JSON.parse(base64urlDecode(payload)),
2525
signature: signature,
2626
};
2727
};

packages/utils/src/base64url.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Base64 } from 'js-base64';
22

3-
export const Base64urlEncode = Base64.encodeURI;
3+
export const base64urlEncode = Base64.encodeURI;
44

5-
export const Base64urlDecode = Base64.decode;
5+
export const base64urlDecode = Base64.decode;
66

7-
export const Uint8ArrayToBase64Url = (input: Uint8Array): string =>
7+
export const uint8ArrayToBase64Url = (input: Uint8Array): string =>
88
Base64.fromUint8Array(input, true);

packages/utils/src/disclosure.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2-
Uint8ArrayToBase64Url,
3-
Base64urlDecode,
4-
Base64urlEncode,
2+
uint8ArrayToBase64Url,
3+
base64urlDecode,
4+
base64urlEncode,
55
} from './base64url';
66
import { SDJWTException } from './error';
77
import type {
@@ -45,16 +45,16 @@ export class Disclosure<T = unknown> {
4545
public static async fromEncode<T>(s: string, hash: HasherAndAlg) {
4646
const { hasher, alg } = hash;
4747
const digest = await hasher(s, alg);
48-
const digestStr = Uint8ArrayToBase64Url(digest);
49-
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
48+
const digestStr = uint8ArrayToBase64Url(digest);
49+
const item = JSON.parse(base64urlDecode(s)) as DisclosureData<T>;
5050
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
5151
}
5252

5353
public static fromEncodeSync<T>(s: string, hash: HasherAndAlgSync) {
5454
const { hasher, alg } = hash;
5555
const digest = hasher(s, alg);
56-
const digestStr = Uint8ArrayToBase64Url(digest);
57-
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
56+
const digestStr = uint8ArrayToBase64Url(digest);
57+
const item = JSON.parse(base64urlDecode(s)) as DisclosureData<T>;
5858
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
5959
}
6060

@@ -69,7 +69,7 @@ export class Disclosure<T = unknown> {
6969
if (!this._encoded) {
7070
// we use JSON.stringify to encode the data
7171
// It's the most reliable and universal way to encode JSON object
72-
this._encoded = Base64urlEncode(JSON.stringify(this.decode()));
72+
this._encoded = base64urlEncode(JSON.stringify(this.decode()));
7373
}
7474
return this._encoded;
7575
}
@@ -84,7 +84,7 @@ export class Disclosure<T = unknown> {
8484
const { hasher, alg } = hash;
8585
if (!this._digest) {
8686
const hash = await hasher(this.encode(), alg);
87-
this._digest = Uint8ArrayToBase64Url(hash);
87+
this._digest = uint8ArrayToBase64Url(hash);
8888
}
8989

9090
return this._digest;
@@ -94,7 +94,7 @@ export class Disclosure<T = unknown> {
9494
const { hasher, alg } = hash;
9595
if (!this._digest) {
9696
const hash = hasher(this.encode(), alg);
97-
this._digest = Uint8ArrayToBase64Url(hash);
97+
this._digest = uint8ArrayToBase64Url(hash);
9898
}
9999

100100
return this._digest;
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { describe, expect, test } from 'vitest';
22
import {
3-
Base64urlDecode,
4-
Base64urlEncode,
5-
Uint8ArrayToBase64Url,
3+
base64urlDecode,
4+
base64urlEncode,
5+
uint8ArrayToBase64Url,
66
} from '../base64url';
77

88
describe('Base64url', () => {
99
const raw = 'abcdefghijklmnopqrstuvwxyz';
1010
const encoded = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo';
1111
test('Encode', () => {
12-
expect(Base64urlEncode(raw)).toStrictEqual(encoded);
12+
expect(base64urlEncode(raw)).toStrictEqual(encoded);
1313
});
1414
test('Decode', () => {
15-
expect(Base64urlDecode(encoded)).toStrictEqual(raw);
15+
expect(base64urlDecode(encoded)).toStrictEqual(raw);
1616
});
1717
test('Encode and decode', () => {
1818
const str = 'hello world';
19-
expect(Base64urlDecode(Base64urlEncode(str))).toStrictEqual(str);
19+
expect(base64urlDecode(base64urlEncode(str))).toStrictEqual(str);
2020
});
2121
test('Uint8Array', () => {
2222
const str = 'hello world';
2323
const uint8 = new TextEncoder().encode(str);
24-
expect(Uint8ArrayToBase64Url(uint8)).toStrictEqual(Base64urlEncode(str));
24+
expect(uint8ArrayToBase64Url(uint8)).toStrictEqual(base64urlEncode(str));
2525
});
2626
});

packages/utils/src/test/disclosure.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { generateSalt, digest as hasher } from '@sd-jwt/crypto-nodejs';
22
import { Disclosure } from '../disclosure';
33
import { describe, expect, test } from 'vitest';
4-
import { Base64urlEncode, type SDJWTException } from '../index';
4+
import { base64urlEncode, type SDJWTException } from '../index';
55

66
const hash = { alg: 'SHA256', hasher };
77

@@ -144,7 +144,7 @@ describe('Disclosure', () => {
144144
});
145145

146146
test('digest disclosure #3', async () => {
147-
const encoded = Base64urlEncode(TestDataDraft7.claimTests[0].contents);
147+
const encoded = base64urlEncode(TestDataDraft7.claimTests[0].contents);
148148
expect(encoded).toStrictEqual(TestDataDraft7.claimTests[0].disclosure);
149149

150150
const disclosure = await Disclosure.fromEncode(

0 commit comments

Comments
 (0)