Spaces:
Build error
Build error
File size: 5,453 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import {
randomBytes,
createCipheriv,
createDecipheriv,
createHash,
pbkdf2Sync,
randomUUID,
} from 'crypto';
import { genSalt, hash, compare } from 'bcrypt';
import { deflateSync, inflateSync } from 'zlib';
import { Env } from './env';
import { createLogger } from './logger';
const logger = createLogger('crypto');
const saltRounds = 10;
function base64UrlSafe(data: string): string {
return Buffer.from(data)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
function fromUrlSafeBase64(data: string): string {
// Add padding if needed
const padding = data.length % 4;
const paddedData = padding ? data + '='.repeat(4 - padding) : data;
return Buffer.from(
paddedData.replace(/-/g, '+').replace(/_/g, '/'),
'base64'
).toString('utf-8');
}
const compressData = (data: string): Buffer => {
return deflateSync(Buffer.from(data, 'utf-8'), {
level: 9,
});
};
const decompressData = (data: Buffer): string => {
return inflateSync(data).toString('utf-8');
};
const encryptData = (
secretKey: Buffer,
data: Buffer
): { iv: string; data: string } => {
// Then encrypt the compressed data
const iv = randomBytes(16);
const cipher = createCipheriv('aes-256-cbc', secretKey, iv);
const encryptedData = Buffer.concat([cipher.update(data), cipher.final()]);
return {
iv: iv.toString('base64'),
data: encryptedData.toString('base64'),
};
};
const decryptData = (
secretKey: Buffer,
encryptedData: Buffer,
iv: Buffer
): Buffer => {
const decipher = createDecipheriv('aes-256-cbc', secretKey, iv);
// Decrypt the data
const decryptedData = Buffer.concat([
decipher.update(encryptedData),
decipher.final(),
]);
return decryptedData;
};
type SuccessResponse = {
success: true;
data: string;
error: null;
};
type ErrorResponse = {
success: false;
error: string;
data: null;
};
export type Response = SuccessResponse | ErrorResponse;
export function isEncrypted(data: string): boolean {
try {
// parse the data as json
const json = JSON.parse(fromUrlSafeBase64(data));
return json.type === 'aioEncrypt';
} catch (error) {
return false;
}
}
/**
* Encrypts a string using AES-256-CBC encryption, returns a string in the format "iv:encrypted" where
* iv and encrypted are url encoded.
* @param data Data to encrypt
* @param secretKey Secret key used for encryption
* @returns Encrypted data or error message
*/
export function encryptString(data: string, secretKey?: Buffer): Response {
if (!secretKey) {
secretKey = Buffer.from(Env.SECRET_KEY, 'hex');
}
try {
const compressed = compressData(data);
const { iv, data: encrypted } = encryptData(secretKey, compressed);
return {
success: true,
data: base64UrlSafe(
JSON.stringify({ iv, encrypted, type: 'aioEncrypt' })
),
error: null,
};
} catch (error: any) {
logger.error(`Failed to encrypt data: ${error.message}`);
return {
success: false,
error: error.message,
data: null,
};
}
}
/**
* Decrypts a string using AES-256-CBC encryption
* @param data Encrypted data to decrypt
* @param secretKey Secret key used for encryption
* @returns Decrypted data or error message
*/
export function decryptString(data: string, secretKey?: Buffer): Response {
if (!secretKey) {
secretKey = Buffer.from(Env.SECRET_KEY, 'hex');
}
try {
if (!isEncrypted(data)) {
throw new Error('The data was not in an expected encrypted format');
}
const json = JSON.parse(fromUrlSafeBase64(data));
const iv = Buffer.from(json.iv, 'base64');
const encrypted = Buffer.from(json.encrypted, 'base64');
const decrypted = decryptData(secretKey, encrypted, iv);
const decompressed = decompressData(decrypted);
return {
success: true,
data: decompressed,
error: null,
};
} catch (error: any) {
logger.error(`Failed to decrypt data: ${error.message}`);
return {
success: false,
error: error.message,
data: null,
};
}
}
export function getSimpleTextHash(text: string): string {
return createHash('sha256').update(text).digest('hex');
}
/**
* Creates a secure hash of text using PBKDF2
* @param text Text to hash
* @returns Object containing the hash and salt used
*/
export async function getTextHash(text: string): Promise<string> {
return await hash(text, await genSalt(saltRounds));
}
/**
* Verifies if the provided text matches a previously generated hash
* @param text Text to verify
* @param storedHash Previously generated hash
* @returns Boolean indicating if the text matches the hash
*/
export async function verifyHash(
text: string,
storedHash: string
): Promise<boolean> {
return compare(text, storedHash);
}
/**
* Derives a 64 character hex string from a password using PBKDF2
* @param password Password to derive key from
* @param salt Optional salt, will be generated if not provided
* @returns Object containing the key and salt used
*/
export async function deriveKey(
password: string,
salt?: string
): Promise<{ key: Buffer; salt: string }> {
salt = salt || (await genSalt(saltRounds));
const key = pbkdf2Sync(
Buffer.from(password, 'utf-8'),
Buffer.from(salt, 'hex'),
100000,
32,
'sha512'
);
return { key, salt };
}
export function generateUUID(): string {
return randomUUID();
}
|