Created
July 13, 2022 14:05
-
-
Save abhi3700/7ec54e623aafff329ad1b2d42249733e to your computer and use it in GitHub Desktop.
Conversion of inline-assembly to JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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