PageRenderTime 328ms CodeModel.GetById 268ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/crypto.js

http://pyjamas.googlecode.com/
JavaScript | 81 lines | 80 code | 1 blank | 0 comment | 14 complexity | 76caff02f0d9b4af91fab1164fdde221 MD5 | raw file
 1 
 2Module("crypto", "0.1.2", function(mod){
 3mod.listEncrypters=function(){
 4var c=[];
 5for(var attr in String.prototype){
 6if(attr.slice(0, 8) == "encrypt_"){
 7c.push(attr.slice(8));
 8}
 9}
10return c;
11}
12mod.listDecrypters=function(){
13var c=[];
14for(var attr in String.prototype){
15if(attr.slice(0, 8) == "decrypt_"){
16c.push(attr.slice(8));
17}
18}
19return c;
20}
21String.prototype.encrypt=function(crydec){
22var n = "encrypt_" + crydec;
23if(String.prototype[n]){
24var args=[];
25for(var i=1;i<arguments.length;i++){
26args[i-1] = arguments[i];
27}
28return String.prototype[n].apply(this, args);
29}else{
30throw new mod.Exception("Decrypter '%s' not found.".format(crydec));
31}
32}
33String.prototype.decrypt=function(crydec){
34var n = "decrypt_" + crydec;
35if(String.prototype[n]){
36var args=[];
37for(var i=1;i<arguments.length;i++){
38args[i-1] = arguments[i];
39}
40return String.prototype[n].apply(this, args);
41}else{
42throw new mod.Exception("Encrypter '%s' not found.".format(crydec));
43}
44}
45String.prototype.encrypt_xor=function(key){
46var e=new Array(this.length);
47var l=key.length;
48for(var i=0;i<this.length;i++){
49e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l));
50}
51return e.join("");
52}
53String.prototype.decrypt_xor=String.prototype.encrypt_xor;
54String.prototype.encrypt_rc4=function(key){
55var sbox = new Array (256);
56for (var i=0; i<256; i++){
57sbox[i]=i;
58}
59var j=0;
60for (var i=0; i < 256; i++) {
61j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256;
62var tmp = sbox[i];
63sbox[i] = sbox[j];
64sbox[j] = tmp;
65}
66var i=256;    
67var j=256;
68var rslt=new Array(this.length);
69for (var k=0; k < this.length; k++) {
70i = (i + 1) % 256;
71j = (j + sbox[i]) % 256;
72var tmp = sbox[i];
73sbox[i] = sbox[j];
74sbox[j] = tmp;
75t = (sbox[i] + sbox[j]) % 256;
76rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]);
77}
78return rslt.join("");
79}
80String.prototype.decrypt_rc4=String.prototype.encrypt_rc4;
81})