Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created December 14, 2011 17:45
Show Gist options
  • Select an option

  • Save crisu83/1477634 to your computer and use it in GitHub Desktop.

Select an option

Save crisu83/1477634 to your computer and use it in GitHub Desktop.
Rot13 static JavaScript class
/**
* Rot13 class.
* @author Christoffer Niska <[email protected]>
* @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;
}
};
@crisu83
Copy link
Author

crisu83 commented Dec 14, 2011

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.

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