UNPKG

advanced-cipher

Version:

A sophisticated 256-bit encryption library with multi-layer key derivation and complex bit-level transformations

141 lines (131 loc) 4.9 kB
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", { writable: false }), e; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (undefined !== e) { var i = e.call(t, r); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return (String )(t); } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } /** * AdvancedCipher - A sophisticated encryption library * * @module AdvancedCipher * @description Implements a multi-layer encryption algorithm with configurable bit-level security */ /** * Advanced Symmetric Encryption Algorithm * * @class AdvancedCipher * @description Provides a robust encryption and decryption mechanism * @bitLevel 256 - Utilizes a 256-bit encryption strength for enhanced security */ var AdvancedCipher = /*#__PURE__*/function () { /** * Creates an instance of AdvancedCipher * * @param {string} secretKey - The secret key for encryption/decryption * @param {number} [iterations=1000] - Number of key derivation iterations */ function AdvancedCipher(secretKey) { var iterations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; _classCallCheck(this, AdvancedCipher); if (!secretKey || secretKey.length < 12) { throw new Error('Secret key must be at least 12 characters long'); } this._key = this._deriveKey(secretKey, iterations); } /** * Derives a cryptographically secure key using a more memory-efficient approach * * @private * @param {string} secretKey - Original secret key * @param {number} iterations - Number of key derivation iterations * @returns {Uint8Array} Derived cryptographic key */ return _createClass(AdvancedCipher, [{ key: "_deriveKey", value: function _deriveKey(secretKey, iterations) { var encoder = new TextEncoder(); new TextDecoder(); var baseKey = encoder.encode(secretKey); // Limit key size to prevent exponential growth var MAX_KEY_LENGTH = 1024; var derivedKey = new Uint8Array(Math.min(baseKey.length, MAX_KEY_LENGTH)); derivedKey.set(baseKey.slice(0, derivedKey.length)); // More efficient key derivation for (var i = 0; i < iterations; i++) { // Use a fixed-size temporary buffer var tempKey = new Uint8Array(derivedKey.length); for (var j = 0; j < derivedKey.length; j++) { // Complex but controlled bit manipulation tempKey[j] = derivedKey[j] ^ (i + j) % 256 ^ (derivedKey[(j + 1) % derivedKey.length] << 3 | derivedKey[(j + 1) % derivedKey.length] >> 5); } derivedKey = tempKey; } return derivedKey; } /** * Encrypts the input text * * @param {string} plaintext - Text to encrypt * @returns {string} Base64 encoded encrypted text */ }, { key: "encrypt", value: function encrypt(plaintext) { if (!plaintext) return ''; var encoder = new TextEncoder(); var data = encoder.encode(plaintext); var encryptedBuffer = new Uint8Array(data.length); for (var i = 0; i < data.length; i++) { var keyByte = this._key[i % this._key.length]; // Streamlined encryption with controlled bit manipulation encryptedBuffer[i] = data[i] ^ keyByte ^ i * 17 % 256 ^ (keyByte << 4 | keyByte >> 4); } return btoa(String.fromCharCode.apply(null, encryptedBuffer)); } /** * Decrypts the input text * * @param {string} ciphertext - Base64 encoded encrypted text * @returns {string} Decrypted plaintext */ }, { key: "decrypt", value: function decrypt(ciphertext) { if (!ciphertext) return ''; var encryptedBuffer = new Uint8Array(atob(ciphertext).split('').map(function (_char) { return _char.charCodeAt(0); })); var decryptedBuffer = new Uint8Array(encryptedBuffer.length); for (var i = 0; i < encryptedBuffer.length; i++) { var keyByte = this._key[i % this._key.length]; // Reverse the encryption process precisely decryptedBuffer[i] = encryptedBuffer[i] ^ keyByte ^ i * 17 % 256 ^ (keyByte << 4 | keyByte >> 4); } var decoder = new TextDecoder(); return decoder.decode(decryptedBuffer); } }]); }(); export { AdvancedCipher as default };