JSFiddle - React, Tailwind, and code Playground
by Alexander
JavaScript
console.clear();
const trace = (tag) => x => (console.log(tag, x), x);
const encrypt = (str, key) => str
.split('')
.map(s=>(s.charCodeAt()^key).toString(16))
.join('g')
;
const decrypt = (str, key) => str
.split('g')
.filter(Boolean)
//.map(trace('beforeMap'))
.map(s=> String.fromCharCode(parseInt(s,16)^key) )
.join('')
;
let hash;
{
let text = 'Текст для шифрования with english symbols',
key = 889360895;
hash = encrypt(text, key);
console.group('Test 1: Строка с данными');
console.log(hash);
hash = decrypt(hash, key);
console.assert(text === hash, `Not equal string ${hash}`);
console.log(text);
console.groupEnd();
}
{
let text = '',
key = 889360895;
hash = encrypt(text, key);
console.group('Test 2: Пустая строка');
console.log(hash);
hash = decrypt(hash, key);
console.assert(text === hash, `Not equal string ${hash}`);
console.log(text);
console.groupEnd();
}