@@ -17,6 +17,13 @@ const ENCRYPTION_SALT = "oidc_token_salt";
1717const KEY_LENGTH = 32 ;
1818const IV_LENGTH = 12 ;
1919
20+ // Derive encryption key once at module initialization to avoid blocking event loop
21+ const DERIVED_KEY = crypto . scryptSync (
22+ ENCRYPTION_KEY ,
23+ ENCRYPTION_SALT ,
24+ KEY_LENGTH ,
25+ ) ;
26+
2027// Token expiration constants
2128const TOKEN_ONE_HOUR_MS = 60 * 60 * 1000 ; // milliseconds
2229const TOKEN_SEVEN_DAYS_SECONDS = 7 * 24 * 60 * 60 ; // seconds
@@ -66,9 +73,8 @@ function isOidcTokenData(data: unknown): data is OidcTokenData {
6673 * Returns encrypted data in format: iv:authTag:encrypted (all hex-encoded).
6774 */
6875function encrypt ( text : string ) : string {
69- const key = crypto . scryptSync ( ENCRYPTION_KEY , ENCRYPTION_SALT , KEY_LENGTH ) ;
7076 const iv = crypto . randomBytes ( IV_LENGTH ) ;
71- const cipher = crypto . createCipheriv ( "aes-256-gcm" , key , iv ) ;
77+ const cipher = crypto . createCipheriv ( "aes-256-gcm" , DERIVED_KEY , iv ) ;
7278
7379 const encrypted = Buffer . concat ( [
7480 cipher . update ( text , "utf8" ) ,
@@ -90,7 +96,6 @@ function encrypt(text: string): string {
9096 * Expects data in format: iv:authTag:encrypted (all hex-encoded).
9197 */
9298function decrypt ( payload : string ) : string {
93- const key = crypto . scryptSync ( ENCRYPTION_KEY , ENCRYPTION_SALT , KEY_LENGTH ) ;
9499 const [ ivHex , tagHex , encryptedHex ] = payload . split ( ":" ) ;
95100
96101 if ( ! ivHex || ! tagHex || ! encryptedHex ) {
@@ -101,7 +106,7 @@ function decrypt(payload: string): string {
101106 const authTag = Buffer . from ( tagHex , "hex" ) ;
102107 const encrypted = Buffer . from ( encryptedHex , "hex" ) ;
103108
104- const decipher = crypto . createDecipheriv ( "aes-256-gcm" , key , iv ) ;
109+ const decipher = crypto . createDecipheriv ( "aes-256-gcm" , DERIVED_KEY , iv ) ;
105110 decipher . setAuthTag ( authTag ) ;
106111
107112 const decrypted = Buffer . concat ( [
0 commit comments