diff --git a/packages/crypto/src/environment.ts b/packages/crypto/src/environment.ts index c1f72210..8efe296d 100644 --- a/packages/crypto/src/environment.ts +++ b/packages/crypto/src/environment.ts @@ -1,10 +1,6 @@ import dotenv from 'dotenv'; -export const getEnv = (env: string | undefined, defaultValue?: string, required?: boolean) => { - if (required && !env && !defaultValue) { - throw new Error(`Missing required environment variable`); - } - +export const getEnv = (env: string | undefined, defaultValue?: string) => { return env ?? defaultValue; } @@ -14,4 +10,4 @@ dotenv.config({ }); // @note: You can use https://generate-random.org/encryption-key-generator to create a new 32 byte key -export const SOURCEBOT_ENCRYPTION_KEY = getEnv(process.env.SOURCEBOT_ENCRYPTION_KEY, undefined, true)!; \ No newline at end of file +export const SOURCEBOT_ENCRYPTION_KEY = getEnv(process.env.SOURCEBOT_ENCRYPTION_KEY); \ No newline at end of file diff --git a/packages/crypto/src/index.ts b/packages/crypto/src/index.ts index fc63f764..f0128297 100644 --- a/packages/crypto/src/index.ts +++ b/packages/crypto/src/index.ts @@ -9,6 +9,10 @@ const generateIV = (): Buffer => { }; export function encrypt(text: string): { iv: string; encryptedData: string } { + if (!SOURCEBOT_ENCRYPTION_KEY) { + throw new Error('Encryption key is not set'); + } + const encryptionKey = Buffer.from(SOURCEBOT_ENCRYPTION_KEY, 'ascii'); const iv = generateIV(); @@ -21,6 +25,10 @@ export function encrypt(text: string): { iv: string; encryptedData: string } { } export function decrypt(iv: string, encryptedText: string): string { + if (!SOURCEBOT_ENCRYPTION_KEY) { + throw new Error('Encryption key is not set'); + } + const encryptionKey = Buffer.from(SOURCEBOT_ENCRYPTION_KEY, 'ascii'); const ivBuffer = Buffer.from(iv, 'hex');