UNPKG

nitlix-crypto

Version:

😍 Another starting collection of hashing, salting and other encryption tools 🚀 for the back-end side of the web.

79 lines (78 loc) 5.14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const saltStringNative_1 = __importDefault(require("../salt/saltStringNative")); const saltCompareNative_1 = __importDefault(require("../salt/saltCompareNative")); const saltString_1 = __importDefault(require("../salt/saltString")); const saltCompare_1 = __importDefault(require("../salt/saltCompare")); function main() { return __awaiter(this, void 0, void 0, function* () { const testString = "string"; console.log("Original string:", testString); // Test Native Functions console.log("\n=== Native Functions ==="); // Salt the string with timing (Native) const nativeSaltStartTime = performance.now(); const nativeSaltedString = yield (0, saltStringNative_1.default)(testString); const nativeSaltEndTime = performance.now(); const nativeSaltDuration = nativeSaltEndTime - nativeSaltStartTime; console.log("Native salted string:", nativeSaltedString); console.log(`Native salting took ${nativeSaltDuration.toFixed(2)}ms`); // Compare the original string with the salted string with timing (Native) const nativeCompareStartTime = performance.now(); const nativeIsMatch = yield (0, saltCompareNative_1.default)(testString, nativeSaltedString); const nativeCompareEndTime = performance.now(); const nativeCompareDuration = nativeCompareEndTime - nativeCompareStartTime; console.log("Native comparison result:", nativeIsMatch); console.log(`Native comparison took ${nativeCompareDuration.toFixed(2)}ms`); // Test with wrong string with timing (Native) const wrongString = "wrong"; const nativeWrongStartTime = performance.now(); const nativeWrongMatch = yield (0, saltCompareNative_1.default)(wrongString, nativeSaltedString); const nativeWrongEndTime = performance.now(); const nativeWrongDuration = nativeWrongEndTime - nativeWrongStartTime; console.log("Native wrong string comparison:", nativeWrongMatch); console.log(`Native wrong string comparison took ${nativeWrongDuration.toFixed(2)}ms`); // Test Crypto Module Functions console.log("\n=== Crypto Module Functions ==="); // Salt the string with timing (Crypto) const cryptoSaltStartTime = performance.now(); const cryptoSaltedString = yield (0, saltString_1.default)(testString); const cryptoSaltEndTime = performance.now(); const cryptoSaltDuration = cryptoSaltEndTime - cryptoSaltStartTime; console.log("Crypto salted string:", cryptoSaltedString); console.log(`Crypto salting took ${cryptoSaltDuration.toFixed(2)}ms`); // Compare the original string with the salted string with timing (Crypto) const cryptoCompareStartTime = performance.now(); const cryptoIsMatch = yield (0, saltCompare_1.default)(testString, cryptoSaltedString); const cryptoCompareEndTime = performance.now(); const cryptoCompareDuration = cryptoCompareEndTime - cryptoCompareStartTime; console.log("Crypto comparison result:", cryptoIsMatch); console.log(`Crypto comparison took ${cryptoCompareDuration.toFixed(2)}ms`); // Test with wrong string with timing (Crypto) const cryptoWrongStartTime = performance.now(); const cryptoWrongMatch = yield (0, saltCompare_1.default)(wrongString, cryptoSaltedString); const cryptoWrongEndTime = performance.now(); const cryptoWrongDuration = cryptoWrongEndTime - cryptoWrongStartTime; console.log("Crypto wrong string comparison:", cryptoWrongMatch); console.log(`Crypto wrong string comparison took ${cryptoWrongDuration.toFixed(2)}ms`); // Performance Comparison Summary console.log("\n=== Performance Comparison ==="); console.log(`Salting - Native: ${nativeSaltDuration.toFixed(2)}ms, Crypto: ${cryptoSaltDuration.toFixed(2)}ms`); console.log(`Comparison - Native: ${nativeCompareDuration.toFixed(2)}ms, Crypto: ${cryptoCompareDuration.toFixed(2)}ms`); console.log(`Wrong String - Native: ${nativeWrongDuration.toFixed(2)}ms, Crypto: ${cryptoWrongDuration.toFixed(2)}ms`); }); } // Run the main function main().catch(console.error);