@hv-kit/hexpress
Version:
facilitates typescript backend development with express
189 lines • 7.09 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateToken = exports.AES = exports.MD5 = exports.SHA = void 0;
const crypto_js_1 = __importDefault(require("crypto-js"));
const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json"));
const date_1 = require("./date");
const string_1 = require("./string");
class SHA {
constructor(hashLength = 512, enc = 'base64') {
this.hashLength = 512;
this.enc = 'base64';
this.hashLength = hashLength;
this.enc = enc;
}
Hash(value, hashLength = 512, enc = 'base64') {
let res = '';
try {
this.hashLength = hashLength;
this.enc = enc;
let res2;
if (this.hashLength === 512) {
res2 = crypto_js_1.default.HmacSHA512(value, hexpress_config_json_1.default.crypto.key);
}
else if (this.hashLength === 384) {
res2 = crypto_js_1.default.HmacSHA384(value, hexpress_config_json_1.default.crypto.key);
}
else if (this.hashLength === 256) {
res2 = crypto_js_1.default.HmacSHA256(value, hexpress_config_json_1.default.crypto.key);
}
else if (this.hashLength === 224) {
res2 = crypto_js_1.default.HmacSHA224(value, hexpress_config_json_1.default.crypto.key);
}
else {
res2 = crypto_js_1.default.HmacSHA512(value, hexpress_config_json_1.default.crypto.key);
}
if (this.enc === 'base64') {
res = res2.toString(crypto_js_1.default.enc.Base64);
}
else if (this.enc === 'hex') {
res = res2.toString(crypto_js_1.default.enc.Hex);
}
else {
res = res2.toString(crypto_js_1.default.enc.Base64);
}
}
catch (error) {
if (hexpress_config_json_1.default.debug) {
console.log('-----> crypto.js > SHA - Hash - error:: ', error);
}
res = '';
}
return res;
}
}
exports.SHA = SHA;
class MD5 {
constructor(enc = 'base64') {
this.enc = 'base64';
this.enc = enc;
}
Hash(value, enc = 'base64') {
let res = '';
try {
this.enc = enc;
let res2 = crypto_js_1.default.HmacMD5(value, hexpress_config_json_1.default.crypto.key);
if (this.enc === 'base64') {
res = res2.toString(crypto_js_1.default.enc.Base64);
}
else if (this.enc === 'hex') {
res = res2.toString(crypto_js_1.default.enc.Hex);
}
else {
res = res2.toString(crypto_js_1.default.enc.Base64);
}
}
catch (error) {
if (hexpress_config_json_1.default.debug) {
console.log('-----> crypto.js > MD5 - Hash - error:: ', error);
}
res = '';
}
return res;
}
}
exports.MD5 = MD5;
class AES {
constructor(enc = 'utf8', mode = crypto_js_1.default.mode.CFB, padding = crypto_js_1.default.pad.Pkcs7) {
this.mode = crypto_js_1.default.mode.CFB;
this.padding = crypto_js_1.default.pad.Pkcs7;
this.key = crypto_js_1.default.enc.Hex.parse(hexpress_config_json_1.default.crypto.key);
this.iv = crypto_js_1.default.enc.Hex.parse(hexpress_config_json_1.default.crypto.iv);
this.enc = 'utf8';
this.mode = mode;
this.padding = padding;
this.enc = enc;
}
Encrypt(value, enc = 'utf8', mode = crypto_js_1.default.mode.CFB, padding = crypto_js_1.default.pad.Pkcs7) {
let res = '';
try {
this.mode = mode;
this.padding = padding;
this.enc = enc;
let res2 = crypto_js_1.default.AES.encrypt(value, this.key, {
iv: this.iv,
mode: this.mode,
padding: this.padding
});
res = res2;
if (['base64', 'utf8'].includes(this.enc)) {
res = res2.toString();
}
else if (this.enc === 'hex') {
res = res2.toString(crypto_js_1.default.format.Hex);
}
}
catch (error) {
if (hexpress_config_json_1.default.debug) {
console.log('-----> crypto.js > AES - Encrypt - error:: ', error);
}
res = '';
}
return res;
}
Decrypt(value, enc = 'utf8', mode = crypto_js_1.default.mode.CFB, padding = crypto_js_1.default.pad.Pkcs7) {
let res = '';
try {
this.mode = mode;
this.padding = padding;
this.enc = enc;
const res2 = crypto_js_1.default.AES.decrypt(value, this.key, {
iv: this.iv,
mode: this.mode,
padding: this.padding
});
res = res2;
if (this.enc === 'base64') {
res = res2.toString(crypto_js_1.default.enc.Base64);
}
else if (this.enc === 'utf8') {
res = res2.toString(crypto_js_1.default.enc.Utf8);
}
else if (this.enc === 'hex') {
res = res2.toString(crypto_js_1.default.enc.Hex);
}
}
catch (error) {
if (hexpress_config_json_1.default.debug) {
console.log('-----> crypto.js > AES - Decrypt - error:: ', error);
}
res = '';
}
return res;
}
}
exports.AES = AES;
function Base64url(source) {
let encodedSource = crypto_js_1.default.enc.Base64.stringify(source);
encodedSource = encodedSource.replace(/=+$/, '');
encodedSource = encodedSource.replace(/\+/g, '-');
encodedSource = encodedSource.replace(/\//g, '_');
return encodedSource;
}
function GenerateToken(data) {
if (typeof data === 'object' && Array.isArray(data) === false) {
data['_token_date'] = (0, date_1.GetCurrentDate)();
}
else {
data = {
value: data,
_token_date: (0, date_1.GetCurrentDate)(),
};
}
const stringifiedHeader = crypto_js_1.default.enc.Utf8.parse((0, string_1.RandomF)(5, 'alphabetical_s'));
const encodedHeader = Base64url(stringifiedHeader);
const stringifiedData = crypto_js_1.default.HmacSHA512(JSON.stringify(data), hexpress_config_json_1.default.crypto.key);
const encodedData = Base64url(stringifiedData);
return `${encodedHeader} ${encodedData}`;
}
exports.GenerateToken = GenerateToken;
exports.default = {
SHA,
MD5,
AES,
GenerateToken,
};
//# sourceMappingURL=crypto.js.map