UNPKG

@szegedsw/lib-node

Version:

A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.

123 lines 5.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Crypt = exports.SecretToUse = void 0; const crypto_1 = __importDefault(require("crypto")); const logger_1 = require("../logger/logger"); // eslint-disable-next-line no-shadow var SecretToUse; (function (SecretToUse) { SecretToUse[SecretToUse["AES"] = 0] = "AES"; SecretToUse[SecretToUse["HMAC"] = 1] = "HMAC"; })(SecretToUse = exports.SecretToUse || (exports.SecretToUse = {})); let Crypt = /** @class */ (() => { class Crypt { static init(secrets, initvector) { Crypt.secrets = secrets; // tslint:disable-next-line: no-magic-numbers if (Crypt.secrets[SecretToUse.AES].length !== 32) { logger_1.Logger.error("An AES 256-bit key length should be 32 characters."); } /* const hmacKeyLength = Buffer.byteLength(Crypt.secrets[SecretToUse.HMAC],'utf8'); if( hmacKeyLength !== 128) { Logger.warning('HMAC-512 key length should be 128 bytes. Current length: ' + hmacKeyLength + ' bytes.'); } */ const len = Buffer.byteLength(initvector, "utf8"); logger_1.Logger.trace(`Initvector length: ${len}`); // tslint:disable-next-line: no-magic-numbers if (len < 7 || len > 13) { throw new Error(`initvector length should be between 7 and 13 bytes (inclusive). Current length: ${len} bytes.`); } else { Crypt.iv = initvector; // tslint:disable-next-line: no-magic-numbers Crypt.maxlen = 2 ** ((15 - len) * 8); logger_1.Logger.info(`Crypt functions can accept max length of plain text : ${Crypt.maxlen} bytes.`); logger_1.Logger.info("Crypt module was initialized...", false); } } static encryptAES256(text, secretId) { Crypt.checkLength(text); if (secretId < 0 || !Crypt.secrets || Crypt.secrets.length === 0 || secretId >= Crypt.secrets.length) { throw Error("secrets_not_provided"); } const cipher = crypto_1.default.createCipheriv("aes-256-gcm", Crypt.secrets[secretId], Crypt.iv); return cipher.update(text, "utf8", "hex").toString(); } static decryptAES256(text, secretId) { Crypt.checkLength(text); if (secretId < 0 || !Crypt.secrets || Crypt.secrets.length === 0 || secretId >= Crypt.secrets.length) { throw Error("secrets_not_provided"); } const cipher = crypto_1.default.createDecipheriv("aes-256-gcm", Crypt.secrets[secretId], Crypt.iv); return cipher.update(text, "hex").toString(); } static hmacSHA512(text, secretId) { Crypt.checkLength(text); if (secretId < 0 || !Crypt.secrets || Crypt.secrets.length === 0 || secretId >= Crypt.secrets.length) { throw Error("secrets_not_provided"); } const cipher = crypto_1.default.createHmac("sha512", Crypt.secrets[secretId]); return cipher.update(text, "utf8").digest("base64"); } static checkLength(text) { if (Buffer.byteLength(text, "utf8") > Crypt.maxlen) { throw new Error("crypt_max_length_exceeded"); } } static sign(obj, skipProperties = []) { const keys = Object.keys(obj).sort(); let all = ""; keys.forEach((key) => { if (!skipProperties.includes(key)) { all += Crypt.toString(obj[key]); } }); logger_1.Logger.trace(`sign string -> ${all}`); const newObj = Object.assign(obj, { signature: Crypt.hmacSHA512(all, SecretToUse.HMAC) }); return newObj; } static checkSignature(obj, skipProperties = []) { const sign = obj.signature; const objcpy = { ...obj }; Crypt.sign(objcpy, skipProperties); if (!sign || !objcpy.signature) { return false; } return sign === objcpy.signature; } static toString(input) { switch (typeof input) { case "string": return input; case "boolean": return input ? "true" : "false"; case "number": return input.toString(); case "object": if (Object.prototype.toString.call(input) === "[object Date]" && !Number.isNaN(input)) { return input.toFormattedString("YYYY/MM/DD hh24:mm:ss"); } return JSON.stringify(input); case "bigint": return input.toString(); case "function": return ""; default: throw new Error("Can not be converted to String"); } } } Object.defineProperty(Crypt, "iv", { enumerable: true, configurable: true, writable: true, value: "" }); return Crypt; })(); exports.Crypt = Crypt; //# sourceMappingURL=crypt.js.map