|
| 1 | +// based on |
| 2 | +// https://github.com/vercel/next.js/blob/a0993d90c280690e83a2a1bc7c292e1187429fe8/packages/next/src/server/app-render/encryption-utils.ts |
| 3 | + |
| 4 | +function arrayBufferToString(buffer: ArrayBuffer | Uint8Array): string { |
| 5 | + const bytes = new Uint8Array(buffer); |
| 6 | + const len = bytes.byteLength; |
| 7 | + if (len < 65535) { |
| 8 | + return String.fromCharCode.apply(null, bytes as unknown as number[]); |
| 9 | + } |
| 10 | + let binary = ""; |
| 11 | + for (let i = 0; i < len; i++) { |
| 12 | + binary += String.fromCharCode(bytes[i]!); |
| 13 | + } |
| 14 | + return binary; |
| 15 | +} |
| 16 | + |
| 17 | +function stringToUint8Array(binary: string): Uint8Array { |
| 18 | + const len = binary.length; |
| 19 | + const arr = new Uint8Array(len); |
| 20 | + for (let i = 0; i < len; i++) { |
| 21 | + arr[i] = binary.charCodeAt(i); |
| 22 | + } |
| 23 | + return arr; |
| 24 | +} |
| 25 | + |
| 26 | +function concatArray(chunks: Uint8Array[]): Uint8Array { |
| 27 | + let total = 0; |
| 28 | + for (const chunk of chunks) { |
| 29 | + total += chunk.length; |
| 30 | + } |
| 31 | + const result = new Uint8Array(total); |
| 32 | + let offset = 0; |
| 33 | + for (const chunk of chunks) { |
| 34 | + result.set(chunk, offset); |
| 35 | + offset += chunk.length; |
| 36 | + } |
| 37 | + return result; |
| 38 | +} |
| 39 | + |
| 40 | +export async function concatArrayStream( |
| 41 | + stream: ReadableStream<Uint8Array>, |
| 42 | +): Promise<Uint8Array> { |
| 43 | + const chunks: Uint8Array[] = []; |
| 44 | + await stream.pipeTo( |
| 45 | + new WritableStream({ |
| 46 | + write(chunk) { |
| 47 | + chunks.push(chunk); |
| 48 | + }, |
| 49 | + }), |
| 50 | + ); |
| 51 | + return concatArray(chunks); |
| 52 | +} |
| 53 | + |
| 54 | +export function arrayToStream(data: Uint8Array): ReadableStream<Uint8Array> { |
| 55 | + return new ReadableStream({ |
| 56 | + start(controller) { |
| 57 | + controller.enqueue(data); |
| 58 | + controller.close(); |
| 59 | + }, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +export function toBase64(buffer: Uint8Array): string { |
| 64 | + return btoa(arrayBufferToString(buffer)); |
| 65 | +} |
| 66 | + |
| 67 | +export function fromBase64(data: string): Uint8Array { |
| 68 | + return stringToUint8Array(atob(data)); |
| 69 | +} |
| 70 | + |
| 71 | +export async function generateEncryptionKey(): Promise<Uint8Array> { |
| 72 | + const key = await crypto.subtle.generateKey( |
| 73 | + { |
| 74 | + name: "AES-GCM", |
| 75 | + length: 256, |
| 76 | + }, |
| 77 | + true, |
| 78 | + ["encrypt", "decrypt"], |
| 79 | + ); |
| 80 | + const exported = await crypto.subtle.exportKey("raw", key); |
| 81 | + return new Uint8Array(exported); |
| 82 | +} |
| 83 | + |
| 84 | +export async function encryptBuffer( |
| 85 | + data: BufferSource, |
| 86 | + key: CryptoKey, |
| 87 | +): Promise<string> { |
| 88 | + const iv = crypto.getRandomValues(new Uint8Array(16)); |
| 89 | + const encrypted = await crypto.subtle.encrypt( |
| 90 | + { |
| 91 | + name: "AES-GCM", |
| 92 | + iv, |
| 93 | + }, |
| 94 | + key, |
| 95 | + data, |
| 96 | + ); |
| 97 | + return toBase64(concatArray([iv, new Uint8Array(encrypted)])); |
| 98 | +} |
| 99 | + |
| 100 | +export async function decryptBuffer( |
| 101 | + encryptedString: string, |
| 102 | + key: CryptoKey, |
| 103 | +): Promise<ArrayBuffer> { |
| 104 | + const concatenated = fromBase64(encryptedString); |
| 105 | + const iv = concatenated.slice(0, 16); |
| 106 | + const encrypted = concatenated.slice(16); |
| 107 | + return crypto.subtle.decrypt( |
| 108 | + { |
| 109 | + name: "AES-GCM", |
| 110 | + iv, |
| 111 | + }, |
| 112 | + key, |
| 113 | + encrypted, |
| 114 | + ); |
| 115 | +} |
0 commit comments