Skip to content

Commit 10516ff

Browse files
committed
refactor: use browser-native crypto.subtle to hash
1 parent 08c8a21 commit 10516ff

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

lib/apollo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { HttpLink } from "@apollo/client";
22
import { ApolloClient, InMemoryCache } from "@apollo/client-integration-nextjs";
33
import { PersistedQueryLink } from "@apollo/client/link/persisted-queries";
4-
import { sha256 } from "crypto-hash";
54
import buildUri from "./build-uri";
5+
import { sha256 } from "./sha256";
66

77
/**
88
* Create an Apollo Client instance that uses the upstream GraphQL API.

lib/sha256.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Ported from <https://github.com/sindresorhus/crypto-hash/blob/main/browser.js>
3+
*/
4+
5+
export async function sha256(data: string): Promise<string> {
6+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(data));
7+
return bufferToHex(digest);
8+
}
9+
10+
// Pre-computed hex lookup table for fast conversion
11+
const HEX_CHARS = "0123456789abcdef";
12+
const HEX_LUT: string[] = [];
13+
for (let index = 0; index < 256; index++) {
14+
HEX_LUT[index] = HEX_CHARS[(index >>> 4) & 0xF] + HEX_CHARS[index & 0xF];
15+
}
16+
17+
export function bufferToHex(buffer: ArrayBuffer): string {
18+
const bytes = new Uint8Array(buffer);
19+
let hex = "";
20+
for (let index = 0; index < bytes.length; index++) {
21+
hex += HEX_LUT[bytes[index]];
22+
}
23+
24+
return hex;
25+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"babel-plugin-react-compiler": "19.1.0-rc.3",
4343
"class-variance-authority": "^0.7.1",
4444
"clsx": "^2.1.1",
45-
"crypto-hash": "^4.0.0",
4645
"foxact": "^0.2.49",
4746
"graphql": "^16.11.0",
4847
"lucide-react": "^0.544.0",

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)