Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fnv1a instead of SHA256 to create images signatures #2353

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@sentry/nextjs": "^7.94.1",
"@sindresorhus/fnv1a": "^3.1.0",
"@tailwindcss/container-queries": "^0.1.1",
"@tailwindcss/typography": "^0.5.10",
"@upstash/redis": "^1.27.1",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/images.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'server-only';

import fnv1a from '@sindresorhus/fnv1a';

import { noCacheFetchOptions } from '@/lib/cache/http';

import { rootUrl } from './links';
Expand Down Expand Up @@ -201,12 +203,10 @@ function stringifyOptions(options: CloudflareImageOptions): string {
}, '');
}

// Reused buffer for FNV-1a hashing
const fnv1aUtf8Buffer = new Uint8Array(100);
jpreynat marked this conversation as resolved.
Show resolved Hide resolved

async function generateSignature(input: string): Promise<string> {
jpreynat marked this conversation as resolved.
Show resolved Hide resolved
const all = [input, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all));

// Convert ArrayBuffer to hex string
const hashArray = Array.from(new Uint8Array(hash));
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
return hashHex;
return fnv1a(all, { utf8Buffer: fnv1aUtf8Buffer }).toString(16);
}
Loading