Created
September 29, 2012 17:06
-
-
Save brianteachman/3804601 to your computer and use it in GitHub Desktop.
ROT-13 letter substitution cipher Constructor. Provides ROT-13 encoding and decoding.
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
function Rot13() { | |
var result = ""; | |
var decode = false; | |
var lookup = function(char, i) { | |
var lt1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "; | |
var lt2 = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm "; | |
var orig = (decode) ? lt2: lt1; | |
var enc = (decode) ? lt1: lt2; | |
// base case | |
if (i >= orig.length) { | |
return; | |
} else if (char === orig[i]) { | |
result += enc[i]; | |
} | |
lookup(char, i+1); | |
}; | |
var cypher = function(name, i) { | |
if (i > name.length) { return; } | |
lookup(name[i], 0); | |
cypher(name, i+1); | |
return result; | |
}; | |
this.encode = function(text) { | |
result = ""; | |
return cypher(text, 0); | |
}; | |
this.decode = function(text) { | |
decode = true; | |
result = ""; | |
return cypher(text, 0); | |
}; | |
} | |
var text = new Rot13(); | |
console.log(text); | |
var name = text.encode("Brian Teachman"); // "Oevna Grnpuzna" | |
var token = text.decode(name); // "Brian Teachman" | |
console.log(name+" => "+token); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is my take on it so you do not have to figure out if decode or encode (figures it out) & added more characters so you can even encode JavaScript source code ;)