Sha256: 201e8c344d4da628ddb73bb12bc0c8b63dc53d76b1ba671d2ab708ef770a7006
Contents?: true
Size: 872 Bytes
Versions: 185
Compression:
Stored size: 872 Bytes
Contents
const ALPHA = 'abcdefghijklmnopqrstuvwxyz'; function generateKey() { return Array(...Array(100)) .map(() => ALPHA[Math.floor(Math.random() * ALPHA.length)]) .join(''); } function xCode(key, inText, sign) { return [...inText] .reduce((outText, letter, ii) => { const offset = sign * ALPHA.indexOf(key[mod(ii, key.length)]); outText += ALPHA[mod(ALPHA.indexOf(letter) + offset, ALPHA.length)]; return outText; }, ''); } const mod = (n, m) => (n % m + m) % m; export default function (key) { if (typeof key === 'undefined') { key = generateKey(); } else if (key.length === 0 || key.match(/[^a-z]/, 'g')) { throw new Error('Bad key'); } return { key, encode(plainText) { return xCode(this.key, plainText, 1); }, decode(encodedText) { return xCode(this.key, encodedText, -1); }, }; }
Version data entries
185 entries across 185 versions & 1 rubygems