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
+9 -10
View File
@@ -1,9 +1,13 @@
"use strict";
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = decrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Decrypt Function
*/
@@ -12,22 +16,17 @@ function decrypt({ encryptedString, encryptionKey, encryptionSalt, }) {
console.log("Encrypted string is invalid");
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;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = (0, grab_keys_1.default)({ encryptionKey });
if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
console.log("Decrption key is invalid");
return encryptedString;
}
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
console.log("Decrption salt is invalid");
return encryptedString;
}
const algorithm = "aes-192-cbc";
let key = (0, crypto_1.scryptSync)(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
let iv = buffer_1.Buffer.alloc(16, 0);
let key = (0, crypto_1.scryptSync)(encrptKey, salt, keyLen);
let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
const decipher = (0, crypto_1.createDecipheriv)(algorithm, key, iv);
try {
let decrypted = decipher.update(encryptedString, "hex", "utf8");
+11 -13
View File
@@ -1,9 +1,13 @@
"use strict";
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = encrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Encrypt String
*/
@@ -12,31 +16,25 @@ function encrypt({ data, encryptionKey, encryptionSalt, }) {
console.log("Encryption string is invalid");
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;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = (0, grab_keys_1.default)({ encryptionKey });
if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
console.log("Encryption key is invalid");
return data;
}
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
console.log("Encryption salt is invalid");
return data;
}
const algorithm = "aes-192-cbc";
const password = finalEncryptionKey;
let key = (0, crypto_1.scryptSync)(password, finalEncryptionSalt, finalKeyLen);
let iv = buffer_1.Buffer.alloc(16, 0);
// @ts-ignore
const password = encrptKey;
let key = (0, crypto_1.scryptSync)(password, salt, keyLen);
let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
const cipher = (0, crypto_1.createCipheriv)(algorithm, key, iv);
try {
let encrypted = cipher.update(data, "utf8", "hex");
encrypted += cipher.final("hex");
return encrypted;
}
catch ( /** @type {*} */error) {
catch (error) {
console.log("Error in encrypting =>", error.message);
return data;
}
+7 -3
View File
@@ -1,16 +1,20 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = hashPassword;
const crypto_1 = require("crypto");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Hash password Function
*/
function hashPassword({ password, encryptionKey, }) {
const finalEncryptionKey = encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key } = (0, grab_keys_1.default)({ encryptionKey });
if (!(key === null || key === void 0 ? void 0 : key.match(/.{8,}/))) {
throw new Error("Encryption key is invalid");
}
const hmac = (0, crypto_1.createHmac)("sha512", finalEncryptionKey);
const hmac = (0, crypto_1.createHmac)("sha512", key);
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;