Skip to content

Commit

Permalink
removed the need for @lukeed/csprng
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar committed Feb 25, 2024
1 parent d8c70f4 commit 4a5dc88
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
19 changes: 0 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand Down Expand Up @@ -156,7 +155,7 @@ export function decodeJwt<T = unknown>(jwt: string) {
}

export async function randomBytes(num: number) {
return random(num);
return Buffer.from(crypto.getRandomValues(new Uint8Array(num)));
}

export function formatTimestamp(date: Date) {
Expand All @@ -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);
}

0 comments on commit 4a5dc88

Please sign in to comment.