From 4a5dc88e2da69b56cc5bdb0f0fde2f20a58c4d2d Mon Sep 17 00:00:00 2001 From: Sahin Vardar Date: Sun, 25 Feb 2024 12:19:54 +0100 Subject: [PATCH] removed the need for @lukeed/csprng --- package.json | 2 -- pnpm-lock.yaml | 19 ------------------- src/utils.ts | 27 +++++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index c0ac5c1..49d67bf 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "watch": "tsc --build --watch" }, "dependencies": { - "@lukeed/csprng": "^1.1.0", "buffer": "^6.0.3", "date-fns-tz": "^2.0.0", "hash.js": "^1.1.7", @@ -32,7 +31,6 @@ "@aws-sdk/client-cognito-identity-provider": "^3.465.0", "@types/jsbn": "^1.2.33", "@types/jsdom": "^21.1.5", - "@types/randombytes": "^2.0.3", "@typescript-eslint/eslint-plugin": "^6.11.0", "@typescript-eslint/parser": "^6.11.0", "eslint": "^8.54.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db50f4e..ef2a261 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,9 +7,6 @@ settings: importers: .: dependencies: - '@lukeed/csprng': - specifier: ^1.1.0 - version: 1.1.0 buffer: specifier: ^6.0.3 version: 6.0.3 @@ -32,9 +29,6 @@ importers: '@types/jsdom': specifier: ^21.1.5 version: 21.1.5 - '@types/randombytes': - specifier: ^2.0.3 - version: 2.0.3 '@typescript-eslint/eslint-plugin': specifier: ^6.11.0 version: 6.11.0(@typescript-eslint/parser@6.11.0)(eslint@8.54.0)(typescript@5.2.2) @@ -1615,12 +1609,6 @@ packages: '@jridgewell/sourcemap-codec': 1.4.15 dev: true - /@lukeed/csprng@1.1.0: - resolution: - { integrity: sha512-Z7C/xXCiGWsg0KuKsHTKJxbWhpI3Vs5GwLfOean7MGyVFGqdRgBbAjOCh6u4bbjPc/8MJ2pZmK/0DLdCbivLDA== } - engines: { node: '>=8' } - dev: false - /@nodelib/fs.scandir@2.1.5: resolution: { integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g== } @@ -2548,13 +2536,6 @@ packages: { integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw== } dev: true - /@types/randombytes@2.0.3: - resolution: - { integrity: sha512-+NRgihTfuURllWCiIAhm1wsJqzsocnqXM77V/CalsdJIYSRGEHMnritxh+6EsBklshC+clo1KgnN14qgSGeQdw== } - dependencies: - '@types/node': 20.9.1 - dev: true - /@types/semver@7.5.5: resolution: { integrity: sha512-+d+WYC1BxJ6yVOgUgzK8gWvp5qF8ssV5r4nsDcZWKRWcDQLQ619tvWAxJQYGgBrO1MnLJC7a5GtiYsAoQ47dJg== } diff --git a/src/utils.ts b/src/utils.ts index 2895b4a..ad8d8bb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,6 @@ import hashJs from 'hash.js'; import { BigInteger } from 'jsbn'; import { Buffer } from 'buffer'; import formatInTimeZone from 'date-fns-tz/formatInTimeZone'; -import { random } from '@lukeed/csprng'; const initN = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + @@ -156,7 +155,7 @@ export function decodeJwt(jwt: string) { } export async function randomBytes(num: number) { - return random(num); + return Buffer.from(crypto.getRandomValues(new Uint8Array(num))); } export function formatTimestamp(date: Date) { @@ -174,3 +173,27 @@ export function calculateSecretHash(clientSecret: string, userPoolClientId: stri return hash; } + +export async function digest(algorithm: AlgorithmIdentifier, str: string) { + const buffer = new TextEncoder().encode(str); + const hashBuffer = await crypto.subtle.digest(algorithm, buffer); + return Array.from(new Uint8Array(hashBuffer)) + .map(b => b.toString(16).padStart(2, '0')) + .join(''); +} + +export async function hmac(algorithm: AlgorithmIdentifier, key: string, data: string) { + const enc = new TextEncoder(); + const cryptoKey = await crypto.subtle.importKey( + 'raw', + enc.encode(key), + { + name: 'HMAC', + hash: algorithm + }, + false, + ['sign'] + ); + const signature = await crypto.subtle.sign(algorithm, cryptoKey, enc.encode(data)); + return new Uint8Array(signature); +}