Spaces:
Sleeping
Sleeping
Download backend/src/utils/encryption.ts from simhaa-hat/generated-bill: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/spaces/simhaa-hat/generated-bill/resolve/main/backend/src/utils/encryption.ts
- Command line
-
hf download hf://spaces/simhaa-hat/generated-bill/backend/src/utils/encryption.ts
-
curl -L -o encryption.ts https://huggingface.co/spaces/simhaa-hat/generated-bill/resolve/main/backend/src/utils/encryption.ts
2.12 kB
| import crypto from 'crypto'; | |
| const ALGORITHM = 'aes-256-gcm'; | |
| const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || 'default-32-char-key-change-me!!'; // Must be 32 characters | |
| // Ensure key is exactly 32 bytes | |
| const getKey = (): Buffer => { | |
| const key = ENCRYPTION_KEY.padEnd(32, '0').substring(0, 32); | |
| return Buffer.from(key, 'utf-8'); | |
| }; | |
| /** | |
| * Encrypt a token using AES-256-GCM | |
| */ | |
| export const encryptToken = (token: string): string => { | |
| try { | |
| const iv = crypto.randomBytes(16); | |
| const cipher = crypto.createCipheriv(ALGORITHM, getKey(), iv); | |
| let encrypted = cipher.update(token, 'utf8', 'hex'); | |
| encrypted += cipher.final('hex'); | |
| const authTag = cipher.getAuthTag(); | |
| // Return: iv:authTag:encrypted | |
| return `${iv.toString('hex')}:${authTag.toString('hex')}:${encrypted}`; | |
| } catch (error) { | |
| console.error('Encryption error:', error); | |
| throw new Error('Failed to encrypt token'); | |
| } | |
| }; | |
| /** | |
| * Decrypt a token encrypted with encryptToken | |
| */ | |
| export const decryptToken = (encryptedData: string): string => { | |
| try { | |
| const parts = encryptedData.split(':'); | |
| if (parts.length !== 3) { | |
| throw new Error('Invalid encrypted data format'); | |
| } | |
| const [ivHex, authTagHex, encrypted] = parts; | |
| const iv = Buffer.from(ivHex, 'hex'); | |
| const authTag = Buffer.from(authTagHex, 'hex'); | |
| const decipher = crypto.createDecipheriv(ALGORITHM, getKey(), iv); | |
| decipher.setAuthTag(authTag); | |
| let decrypted = decipher.update(encrypted, 'hex', 'utf8'); | |
| decrypted += decipher.final('utf8'); | |
| return decrypted; | |
| } catch (error) { | |
| console.error('Decryption error:', error); | |
| throw new Error('Failed to decrypt token'); | |
| } | |
| }; | |
| /** | |
| * Generate a random token | |
| */ | |
| export const generateRandomToken = (length: number = 32): string => { | |
| return crypto.randomBytes(length).toString('hex'); | |
| }; | |
| /** | |
| * Generate a secure state token for OAuth | |
| */ | |
| export const generateStateToken = (): string => { | |
| return generateRandomToken(16); | |
| }; | |