This commit is contained in:
Benjamin Toby
2025-01-14 14:19:50 +01:00
parent 0731e8cb6c
commit 20754d5cae
23 changed files with 382 additions and 74 deletions
+12 -13
View File
@@ -2,6 +2,7 @@
import { scryptSync, createDecipheriv } from "crypto";
import { Buffer } from "buffer";
import grabKeys from "../../utils/grab-keys";
type Param = {
encryptedString: string;
@@ -22,28 +23,26 @@ export default function decrypt({
return encryptedString;
}
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const finalEncryptionSalt =
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
: 24;
const {
key: encrptKey,
salt,
keyLen,
algorithm,
bufferAllocSize,
} = grabKeys({ encryptionKey });
if (!finalEncryptionKey?.match(/.{8,}/)) {
if (!encrptKey?.match(/.{8,}/)) {
console.log("Decrption key is invalid");
return encryptedString;
}
if (!finalEncryptionSalt?.match(/.{8,}/)) {
if (!salt?.match(/.{8,}/)) {
console.log("Decrption salt is invalid");
return encryptedString;
}
const algorithm = "aes-192-cbc";
let key = scryptSync(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
let iv = Buffer.alloc(16, 0);
let key = scryptSync(encrptKey, salt, keyLen);
let iv = Buffer.alloc(bufferAllocSize, 0);
const decipher = createDecipheriv(algorithm, key, iv);
+15 -15
View File
@@ -2,6 +2,7 @@
import { scryptSync, createCipheriv } from "crypto";
import { Buffer } from "buffer";
import grabKeys from "../../utils/grab-keys";
type Param = {
data: string;
@@ -22,36 +23,35 @@ export default function encrypt({
return data;
}
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const finalEncryptionSalt =
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
: 24;
const {
key: encrptKey,
salt,
keyLen,
algorithm,
bufferAllocSize,
} = grabKeys({ encryptionKey });
if (!finalEncryptionKey?.match(/.{8,}/)) {
if (!encrptKey?.match(/.{8,}/)) {
console.log("Encryption key is invalid");
return data;
}
if (!finalEncryptionSalt?.match(/.{8,}/)) {
if (!salt?.match(/.{8,}/)) {
console.log("Encryption salt is invalid");
return data;
}
const algorithm = "aes-192-cbc";
const password = finalEncryptionKey;
const password = encrptKey;
let key = scryptSync(password, salt, keyLen);
let iv = Buffer.alloc(bufferAllocSize, 0);
let key = scryptSync(password, finalEncryptionSalt, finalKeyLen);
let iv = Buffer.alloc(16, 0);
// @ts-ignore
const cipher = createCipheriv(algorithm, key, iv);
try {
let encrypted = cipher.update(data, "utf8", "hex");
encrypted += cipher.final("hex");
return encrypted;
} catch (/** @type {*} */ error: any) {
} catch (error: any) {
console.log("Error in encrypting =>", error.message);
return data;
}
@@ -1,4 +1,5 @@
import { createHmac } from "crypto";
import grabKeys from "../../utils/grab-keys";
type Param = {
password: string;
@@ -12,14 +13,13 @@ export default function hashPassword({
password,
encryptionKey,
}: Param): string {
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const { key } = grabKeys({ encryptionKey });
if (!finalEncryptionKey?.match(/.{8,}/)) {
if (!key?.match(/.{8,}/)) {
throw new Error("Encryption key is invalid");
}
const hmac = createHmac("sha512", finalEncryptionKey);
const hmac = createHmac("sha512", key);
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;