crypto: replace base64 with base64url

This commit is contained in:
wukko 2024-03-05 21:14:54 +06:00
parent 48ac75b135
commit 73b63e5487

View file

@ -4,14 +4,14 @@ const algorithm = "aes256"
const keyLength = 32; const keyLength = 32;
export function generateHmac(str, salt) { export function generateHmac(str, salt) {
return createHmac("sha256", salt).update(str).digest("base64"); return createHmac("sha256", salt).update(str).digest("base64url");
} }
export function encryptStream(str, iv, secret) { export function encryptStream(str, iv, secret) {
const buff = Buffer.from(JSON.stringify(str), "utf-8"); const buff = Buffer.from(JSON.stringify(str), "utf-8");
const key = scryptSync(Buffer.from(secret, "base64"), "salt", keyLength); const key = scryptSync(Buffer.from(secret, "base64url"), "salt", keyLength);
const cipher = createCipheriv(algorithm, key, Buffer.from(iv, "base64")); const cipher = createCipheriv(algorithm, key, Buffer.from(iv, "base64url"));
return Buffer.concat([ cipher.update(buff), cipher.final() ]) return Buffer.concat([ cipher.update(buff), cipher.final() ])
} }
@ -19,8 +19,8 @@ export function encryptStream(str, iv, secret) {
export function decryptStream(buf, iv, secret) { export function decryptStream(buf, iv, secret) {
const buff = Buffer.from(buf, "binary"); const buff = Buffer.from(buf, "binary");
const key = scryptSync(Buffer.from(secret, "base64"), "salt", keyLength); const key = scryptSync(Buffer.from(secret, "base64url"), "salt", keyLength);
const decipher = createDecipheriv(algorithm, key, Buffer.from(iv, "base64")); const decipher = createDecipheriv(algorithm, key, Buffer.from(iv, "base64url"));
return Buffer.concat([ decipher.update(buff), decipher.final() ]) return Buffer.concat([ decipher.update(buff), decipher.final() ])
} }