@@ -21,27 +21,26 @@ export const enc = async function (
2121 str : string = '' ,
2222 password : string = process . env . JWT_SECRET as string ,
2323 algorithm : string = algorithmDefault ,
24- iv : Buffer = crypto . randomBytes ( 16 )
25- ) : Promise < { encrypted : string , iv : string } > {
26- const key = await scryptAsync ( password , ' salt' , 24 )
27- // Use `crypto.randomBytes` to generate a random iv instead of the static iv
24+ iv : Buffer = crypto . randomBytes ( 16 ) ,
25+ salt : string = crypto . randomBytes ( 16 ) . toString ( 'hex' )
26+ ) : Promise < { encrypted : string , iv : string , salt : string } > {
27+ const key = await scryptAsync ( password , salt , 24 )
2828 const cipher = crypto . createCipheriv ( algorithm , key , iv )
2929 let encrypted = cipher . update ( str , 'utf8' , 'hex' )
3030 encrypted += cipher . final ( 'hex' )
31- return { encrypted, iv : iv . toString ( 'hex' ) }
31+ return { encrypted, iv : iv . toString ( 'hex' ) , salt }
3232}
3333
3434export const dec = async function (
3535 encrypted : string = '' ,
3636 ivStr : string ,
37+ salt : string ,
3738 password : string = process . env . JWT_SECRET as string ,
3839 algorithm : string = algorithmDefault
3940) : Promise < string > {
4041 const iv = Buffer . from ( ivStr , 'hex' )
41- // Use the async `crypto.scrypt()` instead.
42- const key = await scryptAsync ( password , 'salt' , 24 )
42+ const key = await scryptAsync ( password , salt , 24 )
4343 const decipher = crypto . createDecipheriv ( algorithm , key , iv )
44- // Encrypted using same algorithm, key and iv.
4544 let decrypted = decipher . update ( encrypted , 'hex' , 'utf8' )
4645 decrypted += decipher . final ( 'utf8' )
4746 return decrypted
0 commit comments