[READ-ONLY] Mirror of https://github.com/flo-bit/skywatched. review movies and tv shows, based on at proto skywatched.app
0

Configure Feed

Select the types of activity you want to include in your feed.

skywatched / src / lib / server / crypts.ts
1.5 kB 49 lines
1// Code by @pilcrowonpaper on GitHub: https://gist.github.com/pilcrowonpaper/353318556029221c8e25f451b91e5f76 2// AES128 with the Web Crypto API. 3async function encrypt(key: Uint8Array, data: Uint8Array): Promise<Uint8Array> { 4 const iv = new Uint8Array(16); 5 crypto.getRandomValues(iv); 6 const cryptoKey = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['encrypt']); 7 const cipher = await crypto.subtle.encrypt( 8 { 9 name: 'AES-GCM', 10 iv, 11 tagLength: 128 12 }, 13 cryptoKey, 14 data 15 ); 16 const encrypted = new Uint8Array(iv.byteLength + cipher.byteLength); 17 encrypted.set(iv); 18 encrypted.set(new Uint8Array(cipher), iv.byteLength); 19 return encrypted; 20} 21 22export async function encryptString(key: Uint8Array, data: string): Promise<Uint8Array> { 23 const encoded = new TextEncoder().encode(data); 24 const encrypted = await encrypt(key, encoded); 25 return encrypted; 26} 27 28async function decrypt(key: Uint8Array, encrypted: Uint8Array): Promise<Uint8Array> { 29 if (encrypted.length < 16) { 30 throw new Error('Invalid data'); 31 } 32 const cryptoKey = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['decrypt']); 33 const decrypted = await crypto.subtle.decrypt( 34 { 35 name: 'AES-GCM', 36 iv: encrypted.slice(0, 16), 37 tagLength: 128 38 }, 39 cryptoKey, 40 encrypted.slice(16) 41 ); 42 return new Uint8Array(decrypted); 43} 44 45export async function decryptToString(key: Uint8Array, data: Uint8Array): Promise<string> { 46 const decrypted = await decrypt(key, data); 47 const decoded = new TextDecoder().decode(decrypted); 48 return decoded; 49}