Skip to content

Commit 7f35213

Browse files
rjgtavRicardo Tavares
andauthored
fix: make input/output encoding explicit in auth/crypto's encrypt/decrypt (#13710)
### What? Passes the input/output encoding format explicitly when ciphering/deciphering in the auth/crypto utility. ### Why? To make it clearer to read and improve compatibility with other Javascript runtimes Co-authored-by: Ricardo Tavares <rtavares@cloudflare.com>
1 parent 662bab2 commit 7f35213

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

packages/payload/src/auth/crypto.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ export function encrypt(text: string): string {
88
const secret = this.secret
99
const cipher = crypto.createCipheriv(algorithm, secret, iv)
1010

11-
const encrypted = Buffer.concat([cipher.update(text), cipher.final()])
12-
11+
const encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex')
1312
const ivString = iv.toString('hex')
14-
const encryptedString = encrypted.toString('hex')
1513

16-
return `${ivString}${encryptedString}`
14+
return `${ivString}${encrypted}`
1715
}
1816

1917
export function decrypt(hash: string): string {
@@ -24,7 +22,5 @@ export function decrypt(hash: string): string {
2422
const secret = this.secret
2523
const decipher = crypto.createDecipheriv(algorithm, secret, Buffer.from(iv, 'hex'))
2624

27-
const decrypted = Buffer.concat([decipher.update(Buffer.from(content, 'hex')), decipher.final()])
28-
29-
return decrypted.toString()
25+
return decipher.update(content, 'hex', 'utf8') + decipher.final('utf8')
3026
}

0 commit comments

Comments
 (0)