Created
December 14, 2011 17:45
-
-
Save crisu83/1477634 to your computer and use it in GitHub Desktop.
Rot13 static JavaScript class
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
/** | |
* Rot13 class. | |
* @author Christoffer Niska <christoffer.niska@nordsoftware.com> | |
* @copyright Copyright (c) 2011, Christoffer Niska | |
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License | |
*/ | |
var Rot13 = { | |
map: null, | |
length: 0, | |
init: function() { | |
if (this.map) | |
return; | |
var i, map = [], | |
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
length = chars.length; | |
for (i = 0; i < length; ++i) { | |
map.push(chars.charAt((i + 13) % length)); | |
} | |
this.map = map; | |
this.length = length; | |
}, | |
decode: function(text) { | |
this.init(); | |
var i, c, idx, result = ''; | |
for (i = 0; i < text.length; ++i) { | |
c = text.charAt(i); | |
idx = (this.map.indexOf(c) - 13) % this.length; | |
if (idx < 13 && idx >= 0 ) { | |
idx += this.length / 2; | |
} | |
result += this.map[idx] ? this.map[idx] : c; | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this class to decode email addresses client side that are encoded using Rot13 on the server side to prevent them from being collected by spambots.