Skip to content

Instantly share code, notes, and snippets.

@ril3y
Created January 31, 2018 03:32
Show Gist options
  • Save ril3y/501020a4e3923f3422253a5501eff6fe to your computer and use it in GitHub Desktop.
Save ril3y/501020a4e3923f3422253a5501eff6fe to your computer and use it in GitHub Desktop.
Simple Javascript String XOR utility
var xor_str = function(s,K)
{
c = '';
for(i=0; i<str.length; i++) {
for(ik=0; ik<K.length; ik++){
c += String.fromCharCode(str[i].charCodeAt(0).toString(10) ^ key.charCodeAt(0).toString(10)); // XORing with letter 'K'
}
}
return c;
}
@bergginu
Copy link

Awesome! Thanks!

@dartandrevinsky
Copy link

function xorString(init) {
  const initArr = Array.from(init);
  const initLength = initArr.length;
  return function * (input) {
    let idx = 0;
    for (const ch of input) {
      yield initLength === 0 ? ch : String.fromCharCode(ch.charCodeAt(0) ^ initArr[(idx ++) % initLength].charCodeAt(0));
    }
  }
}

@TimofeyOpal
Copy link

const xorStrings = (str1, str2) => {
str1 = str1.split("");
str2= str2.split("");
let betw=0;
for(let i = 0; i<str1.length; i++)
betw=str1[i].charCodeAt(0)^str2[i].charCodeAt(0)^betw;
return betw == 0 ? true : false;
};

@dartandrevinsky
Copy link

@TimofeyOpal please use markdown

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