react-caches
Version:
React Caches is a lightweight and easy-to-use package that provide you a simple way to use Local Storage, Session Storage, Caches Storage & Also provide Encryption & Decryption data alongside with outers package
74 lines • 3.38 kB
JavaScript
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());
});
};
// Encrypt and Decrypt Imports
import CryptoJS from "crypto-js";
/**
* The function encrypts a given string using AES encryption algorithm and returns the encrypted data
* as a promise.
* @param {Str} Data - The `Data` parameter is the string that you want to encrypt. It is the data that
* you want to keep secure and confidential.
* @param [Key=YourKey] - The `Key` parameter is an optional parameter that represents the encryption
* key used to encrypt the data. If no key is provided, the default value is set to 'YourKey'.
* @returns The encrypted data is being returned.
*/
export function encrypt(Data, Key) {
return __awaiter(this, void 0, void 0, function* () {
// Check if User Provided Key or not
if (!Key) {
throw new Error("You must provide a key");
}
// Encrypt data
const encryptedData = CryptoJS.AES.encrypt(Data, Key).toString(); // Encrypt data
return encryptedData; // Return encrypted data
});
}
/**
* The function decrypts a given string using the AES encryption algorithm with a specified key.
* @param {str} Data - The `Data` parameter is the encrypted data that you want to decrypt. It should
* be a string representing the encrypted data.
* @param [Key=YourKey] - The `Key` parameter is an optional parameter that represents the encryption
* key used to decrypt the data. If no key is provided, the default value is set to 'YourKey'.
* @returns The decrypted data is being returned.
*/
export function decrypt(Data, Key) {
return __awaiter(this, void 0, void 0, function* () {
// Check if User Provided Key or not
if (!Key) {
throw new Error("You must provide a key");
}
// Decrypt data
const bytes = CryptoJS.AES.decrypt(Data, Key);
const decryptedText = bytes.toString(CryptoJS.enc.Utf8);
// Return decrypted data
return decryptedText; // Return decrypted data
});
}
// Sync Functions
export function encryptSync(Data, Key) {
// Check if User Provided Key or not
if (!Key) {
throw new Error("You must provide a key");
}
// Encrypt data
const encryptedData = CryptoJS.AES.encrypt(Data, Key).toString(); // Encrypt data
return encryptedData; // Return encrypted data
}
export function decryptSync(Data, Key) {
// Check if User Provided Key or not
if (!Key) {
throw new Error("You must provide a key");
}
// Decrypt data
const bytes = CryptoJS.AES.decrypt(Data, Key);
const decryptedText = bytes.toString(CryptoJS.enc.Utf8);
// Return decrypted data
return decryptedText; // Return decrypted data
}
//# sourceMappingURL=Crypto.config.js.map