diff --git a/packages/core/src/decoy.ts b/packages/core/src/decoy.ts index 8f3f3ee..8ead821 100644 --- a/packages/core/src/decoy.ts +++ b/packages/core/src/decoy.ts @@ -1,5 +1,5 @@ import type { HasherAndAlg, SaltGenerator } from '@sd-jwt/types'; -import { Uint8ArrayToBase64Url } from '@sd-jwt/utils'; +import { uint8ArrayToBase64Url } from '@sd-jwt/utils'; // This function creates a decoy value that can be used to obscure SD JWT payload. // The value is basically a hash of a random salt. So the value is not predictable. @@ -11,5 +11,5 @@ export const createDecoy = async ( const { hasher, alg } = hash; const salt = await saltGenerator(16); const decoy = await hasher(salt, alg); - return Uint8ArrayToBase64Url(decoy); + return uint8ArrayToBase64Url(decoy); }; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index ed90f03..610347a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,4 @@ -import { SDJWTException, Uint8ArrayToBase64Url } from '@sd-jwt/utils'; +import { SDJWTException, uint8ArrayToBase64Url } from '@sd-jwt/utils'; import { Jwt } from './jwt'; import { KBJwt } from './kbjwt'; import { SDJwt, pack } from './sdjwt'; @@ -250,7 +250,7 @@ export class SDJwtInstance { } const { _sd_alg } = getSDAlgAndPayload(sdjwt.jwt.payload); const sdHash = await hasher(presentSdJwtWithoutKb, _sd_alg); - const sdHashStr = Uint8ArrayToBase64Url(sdHash); + const sdHashStr = uint8ArrayToBase64Url(sdHash); return sdHashStr; } diff --git a/packages/core/src/jwt.ts b/packages/core/src/jwt.ts index 1ecf9d0..a53362c 100644 --- a/packages/core/src/jwt.ts +++ b/packages/core/src/jwt.ts @@ -1,4 +1,4 @@ -import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils'; +import { base64urlEncode, SDJWTException } from '@sd-jwt/utils'; import type { Base64urlString, Signer, Verifier } from '@sd-jwt/types'; import { decodeJwt } from '@sd-jwt/decode'; @@ -83,8 +83,8 @@ export class Jwt< return unsignedToken; } - const header = Base64urlEncode(JSON.stringify(this.header)); - const payload = Base64urlEncode(JSON.stringify(this.payload)); + const header = base64urlEncode(JSON.stringify(this.header)); + const payload = base64urlEncode(JSON.stringify(this.payload)); return `${header}.${payload}`; } @@ -104,8 +104,8 @@ export class Jwt< throw new SDJWTException('Serialize Error: Invalid JWT'); } - const header = Base64urlEncode(JSON.stringify(this.header)); - const payload = Base64urlEncode(JSON.stringify(this.payload)); + const header = base64urlEncode(JSON.stringify(this.header)); + const payload = base64urlEncode(JSON.stringify(this.payload)); const signature = this.signature; const compact = `${header}.${payload}.${signature}`; this.encoded = compact; diff --git a/packages/core/src/kbjwt.ts b/packages/core/src/kbjwt.ts index 4266a4b..550cc6a 100644 --- a/packages/core/src/kbjwt.ts +++ b/packages/core/src/kbjwt.ts @@ -1,4 +1,4 @@ -import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils'; +import { SDJWTException } from '@sd-jwt/utils'; import { Jwt } from './jwt'; import { type JwtPayload, diff --git a/packages/core/src/test/decoy.spec.ts b/packages/core/src/test/decoy.spec.ts index 3e4e382..ceec48c 100644 --- a/packages/core/src/test/decoy.spec.ts +++ b/packages/core/src/test/decoy.spec.ts @@ -1,6 +1,6 @@ import { createDecoy } from '../decoy'; import { describe, expect, test } from 'vitest'; -import { Base64urlEncode } from '@sd-jwt/utils'; +import { base64urlEncode } from '@sd-jwt/utils'; import { digest, generateSalt } from '@sd-jwt/crypto-nodejs'; const hash = { @@ -21,7 +21,7 @@ describe('Decoy', () => { // * Contents: ["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"] test('apply hasher and saltGenerator', async () => { const decoyValue = await createDecoy(hash, () => - Base64urlEncode( + base64urlEncode( '["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"]', ), ); diff --git a/packages/decode/src/decode.ts b/packages/decode/src/decode.ts index 551cbbf..fa73929 100644 --- a/packages/decode/src/decode.ts +++ b/packages/decode/src/decode.ts @@ -1,4 +1,4 @@ -import { Base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils'; +import { base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils'; import { type Hasher, type HasherAndAlg, @@ -20,8 +20,8 @@ export const decodeJwt = < } return { - header: JSON.parse(Base64urlDecode(header)), - payload: JSON.parse(Base64urlDecode(payload)), + header: JSON.parse(base64urlDecode(header)), + payload: JSON.parse(base64urlDecode(payload)), signature: signature, }; }; diff --git a/packages/utils/src/base64url.ts b/packages/utils/src/base64url.ts index 75c2ec6..02d7dde 100644 --- a/packages/utils/src/base64url.ts +++ b/packages/utils/src/base64url.ts @@ -1,8 +1,8 @@ import { Base64 } from 'js-base64'; -export const Base64urlEncode = Base64.encodeURI; +export const base64urlEncode = Base64.encodeURI; -export const Base64urlDecode = Base64.decode; +export const base64urlDecode = Base64.decode; -export const Uint8ArrayToBase64Url = (input: Uint8Array): string => +export const uint8ArrayToBase64Url = (input: Uint8Array): string => Base64.fromUint8Array(input, true); diff --git a/packages/utils/src/disclosure.ts b/packages/utils/src/disclosure.ts index b8eb97b..b05a207 100644 --- a/packages/utils/src/disclosure.ts +++ b/packages/utils/src/disclosure.ts @@ -1,7 +1,7 @@ import { - Uint8ArrayToBase64Url, - Base64urlDecode, - Base64urlEncode, + uint8ArrayToBase64Url, + base64urlDecode, + base64urlEncode, } from './base64url'; import { SDJWTException } from './error'; import type { @@ -45,16 +45,16 @@ export class Disclosure { public static async fromEncode(s: string, hash: HasherAndAlg) { const { hasher, alg } = hash; const digest = await hasher(s, alg); - const digestStr = Uint8ArrayToBase64Url(digest); - const item = JSON.parse(Base64urlDecode(s)) as DisclosureData; + const digestStr = uint8ArrayToBase64Url(digest); + const item = JSON.parse(base64urlDecode(s)) as DisclosureData; return Disclosure.fromArray(item, { digest: digestStr, encoded: s }); } public static fromEncodeSync(s: string, hash: HasherAndAlgSync) { const { hasher, alg } = hash; const digest = hasher(s, alg); - const digestStr = Uint8ArrayToBase64Url(digest); - const item = JSON.parse(Base64urlDecode(s)) as DisclosureData; + const digestStr = uint8ArrayToBase64Url(digest); + const item = JSON.parse(base64urlDecode(s)) as DisclosureData; return Disclosure.fromArray(item, { digest: digestStr, encoded: s }); } @@ -69,7 +69,7 @@ export class Disclosure { if (!this._encoded) { // we use JSON.stringify to encode the data // It's the most reliable and universal way to encode JSON object - this._encoded = Base64urlEncode(JSON.stringify(this.decode())); + this._encoded = base64urlEncode(JSON.stringify(this.decode())); } return this._encoded; } @@ -84,7 +84,7 @@ export class Disclosure { const { hasher, alg } = hash; if (!this._digest) { const hash = await hasher(this.encode(), alg); - this._digest = Uint8ArrayToBase64Url(hash); + this._digest = uint8ArrayToBase64Url(hash); } return this._digest; @@ -94,7 +94,7 @@ export class Disclosure { const { hasher, alg } = hash; if (!this._digest) { const hash = hasher(this.encode(), alg); - this._digest = Uint8ArrayToBase64Url(hash); + this._digest = uint8ArrayToBase64Url(hash); } return this._digest; diff --git a/packages/utils/src/test/base64url.spec.ts b/packages/utils/src/test/base64url.spec.ts index 9b87ad6..b79cd6e 100644 --- a/packages/utils/src/test/base64url.spec.ts +++ b/packages/utils/src/test/base64url.spec.ts @@ -1,26 +1,26 @@ import { describe, expect, test } from 'vitest'; import { - Base64urlDecode, - Base64urlEncode, - Uint8ArrayToBase64Url, + base64urlDecode, + base64urlEncode, + uint8ArrayToBase64Url, } from '../base64url'; describe('Base64url', () => { const raw = 'abcdefghijklmnopqrstuvwxyz'; const encoded = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo'; test('Encode', () => { - expect(Base64urlEncode(raw)).toStrictEqual(encoded); + expect(base64urlEncode(raw)).toStrictEqual(encoded); }); test('Decode', () => { - expect(Base64urlDecode(encoded)).toStrictEqual(raw); + expect(base64urlDecode(encoded)).toStrictEqual(raw); }); test('Encode and decode', () => { const str = 'hello world'; - expect(Base64urlDecode(Base64urlEncode(str))).toStrictEqual(str); + expect(base64urlDecode(base64urlEncode(str))).toStrictEqual(str); }); test('Uint8Array', () => { const str = 'hello world'; const uint8 = new TextEncoder().encode(str); - expect(Uint8ArrayToBase64Url(uint8)).toStrictEqual(Base64urlEncode(str)); + expect(uint8ArrayToBase64Url(uint8)).toStrictEqual(base64urlEncode(str)); }); }); diff --git a/packages/utils/src/test/disclosure.spec.ts b/packages/utils/src/test/disclosure.spec.ts index 3b45663..fba00a3 100644 --- a/packages/utils/src/test/disclosure.spec.ts +++ b/packages/utils/src/test/disclosure.spec.ts @@ -1,7 +1,7 @@ import { generateSalt, digest as hasher } from '@sd-jwt/crypto-nodejs'; import { Disclosure } from '../disclosure'; import { describe, expect, test } from 'vitest'; -import { Base64urlEncode, type SDJWTException } from '../index'; +import { base64urlEncode, type SDJWTException } from '../index'; const hash = { alg: 'SHA256', hasher }; @@ -144,7 +144,7 @@ describe('Disclosure', () => { }); test('digest disclosure #3', async () => { - const encoded = Base64urlEncode(TestDataDraft7.claimTests[0].contents); + const encoded = base64urlEncode(TestDataDraft7.claimTests[0].contents); expect(encoded).toStrictEqual(TestDataDraft7.claimTests[0].disclosure); const disclosure = await Disclosure.fromEncode(