Skip to content

Instantly share code, notes, and snippets.

@abhi3700
Created July 13, 2022 14:05
Show Gist options
  • Save abhi3700/7ec54e623aafff329ad1b2d42249733e to your computer and use it in GitHub Desktop.
Save abhi3700/7ec54e623aafff329ad1b2d42249733e to your computer and use it in GitHub Desktop.
Conversion of inline-assembly to JS
const keccak256 = require("keccak256");
// `encryptDecrypt` function
function encryptDecrypt(data, key) {
let length = data.length;
// let result.length = length;
for (let i = 0; i < length; i += 32) {
const hash = keccak256(key.concat(i.toString())).toString("hex");
let chunk;
chunk = data.substr(i, 32);
chunk = getXOR(chunk, hash);
result += chunk;
}
return result;
}
// utility function
function getXOR(s, K) {
c = "";
for (i = 0; i < s.length; i++) {
for (ik = 0; ik < K.length; ik++) {
c += String.fromCharCode(
s[i].charCodeAt(0).toString(10) ^ K.charCodeAt(0).toString(10)
); // XORing with letter 'K'
}
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment