Add GET /i/:id/:secret hotlink image decrypt stream.

This commit is contained in:
2026-10-26 21:47:30 +01:00
parent 4517fe906a
commit 5db90f02c5
14 changed files with 696 additions and 4 deletions
+27
View File
@@ -0,0 +1,27 @@
import { createDecipheriv } from 'node:crypto';
import { AES_GCM_AUTH_TAG_BYTE_LENGTH } from '../consts/crypto/aesGcmAuthTagByteLength';
import { AES_GCM_IV_BYTE_LENGTH } from '../consts/crypto/aesGcmIvByteLength';
import { AES_GCM_KEY_BYTE_LENGTH } from '../consts/crypto/aesGcmKeyByteLength';
export const decryptAesGcm = (ciphertext: Buffer, key: Buffer, iv: Buffer): Buffer => {
if (iv.length !== AES_GCM_IV_BYTE_LENGTH) {
throw new Error(`AES-GCM IV must be ${AES_GCM_IV_BYTE_LENGTH} bytes`);
}
if (key.length !== AES_GCM_KEY_BYTE_LENGTH) {
throw new Error(`AES-GCM key must be ${AES_GCM_KEY_BYTE_LENGTH} bytes`);
}
if (ciphertext.length < AES_GCM_AUTH_TAG_BYTE_LENGTH) {
throw new Error('AES-GCM ciphertext is too short');
}
const authTag = ciphertext.subarray(ciphertext.length - AES_GCM_AUTH_TAG_BYTE_LENGTH);
const encrypted = ciphertext.subarray(0, ciphertext.length - AES_GCM_AUTH_TAG_BYTE_LENGTH);
const decipher = createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(authTag);
return Buffer.concat([decipher.update(encrypted), decipher.final()]);
};