import __base64 from './base64.js'; /** * @name object * @namespace shared.crypto * @type Object * @platform js * @platform node * @status stable * * Expose two function named "encrypt" and "decrypt" that you can use to process your content using the object algorithm * * @snippet __object.encrypt($1) * * @example js * import { __object } from '@lotsof/sugar/crypto'; * __object.encrypt({ * hello: 'world' * }); * * @since 1.0.0 * @author Olivier Bossel (https://lotsof.io) */ export default { /** * @name encrypt * @type Function * * Encrypt * * @param {Object} object The object to encrypt * @param {String} [salt="lotsof.sugar.crypto.object"] The salt to encode the object. Needed to decode correctly the object * @return {String} The encrypted object * * @author Olivier Bossel (https://lotsof.io) */ encrypt: function (object, salt = 'lotsof.sugar.crypto.object') { return __base64.encrypt(`${JSON.stringify(object)}-${salt}`); }, /** * @name decrypt * @type Function * * Decrypt * * @param {String} encodedObject The object to decrypt * @param {String} [salt='lotsof.sugar.crypto.object'] The salt to decode the object * @return {Object} The decrypted object * * @author Olivier Bossel (https://lotsof.io) */ decrypt: function (encodedObject, salt = 'lotsof.sugar.crypto.object') { return JSON.parse(__base64.decrypt(encodedObject).replace(`-${salt}`, '')); }, } as { encrypt: (object: any, salt?: string) => string; decrypt: (encodedObject: string, salt?: string) => any; };