Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Last active March 5, 2021 01:42
Show Gist options
  • Save SparK-Cruz/fa2a769094da5ea59752aa259a101f2a to your computer and use it in GitHub Desktop.
Save SparK-Cruz/fa2a769094da5ea59752aa259a101f2a to your computer and use it in GitHub Desktop.
Lib/function for One Time Padding strings
(() => {
const OTP = (() => {
function xorStrings(a, b) {
let c = "";
for (let i = 0; i < a.length; i++)
c += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
return c;
}
function padkey(key, length) {
let padded = key;
while (padded.length < length)
padded += key;
return padded.substr(0, length);
}
function transform(message, key) {
return xorStrings(message, padkey(key, message.length));
}
return transform;
})();
if (typeof window !== 'undefined') {
window.OTP = OTP;
}
if (typeof module !== 'undefined') {
module.exports = OTP;
}
})();
@SparK-Cruz
Copy link
Author

const OTP = require('./otp.js');
const message = 'anything';
const key = 'a secret';

//encrypt
const encrypted = OTP(message, key);

//decrypt
const result = OTP(encrypted, key);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment