// @ts-ignore import sha512 from 'crypto-js/sha512.js'; import parse from '../../shared/string/parse.js'; import toString from '../../shared/string/toString.js'; const __encryptedMessages = {}; /** * @name sha512 * @namespace shared.crypto * @type Object * @platform node * @status stable * * Expose two function named "encrypt" and "decrypt" that you can use to process your content using the sha512 algorithm * * @snippet __sha512.encrypt($1) * * @example js * import { __sha512 } from '@lotsof/sugar/crypto'; * __sha512.encrypt('hello world'); * * @todo tests * * @since 1.0.0 * @author Olivier Bossel (https://lotsof.dev) */ export default { /** * @name encrypt * @type Function * * Encrypt * * @param {String} message The message to encrypt * @return {String} The encrypted string * * @author Olivier Bossel (https://lotsof.dev) */ encrypt: function (message) { if (typeof message !== 'string') message = toString(message); const string = sha512(message).toString(); __encryptedMessages[string] = message; return string; }, /** * @name decrypt * @type Function * * Decrypt * * @param {String} message The message to decrypt * @return {String} The decrypted message * * @author Olivier Bossel (https://lotsof.dev) */ decrypt: function (message) { if (!__encryptedMessages[message]) { console.warn(`The message "${message}" cannot be decrypted...`); return; } const string = __encryptedMessages[message]; delete __encryptedMessages[message]; return parse(string); }, };