]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/jsolait/lib/crypto.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / jsolait / lib / crypto.js
1 /*
2
3 Modification information for LGPL compliance
4
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
6
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
8
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
10
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
12
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
14
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
16
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
18
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
20
21
22 */
23
24 /*
25   Copyright (c) 2003 Jan-Klaas Kollhof
26   
27   This file is part of the JavaScript o lait library(jsolait).
28   
29   jsolait is free software; you can redistribute it and/or modify
30   it under the terms of the GNU Lesser General Public License as published by
31   the Free Software Foundation; either version 2.1 of the License, or
32   (at your option) any later version.
33  
34   This software is distributed in the hope that it will be useful,
35   but WITHOUT ANY WARRANTY; without even the implied warranty of
36   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   GNU Lesser General Public License for more details.
38  
39   You should have received a copy of the GNU Lesser General Public License
40   along with this software; if not, write to the Free Software
41   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42 */ 
43
44 /**
45     Cryptography module.
46     Provides String encryption/decryption and hashing. 
47 */
48 Module("crypto", "0.1.2", function(mod){
49     /**
50         Returns all all available encrypters.
51         @return  An array of encrypters names.
52     */
53     mod.listEncrypters=function(){
54         var c=[];
55         for(var attr in String.prototype){
56             if(attr.slice(0, 8) == "encrypt_"){
57                 c.push(attr.slice(8));
58             }
59         }
60         return c;
61     }
62     /**
63         Returns all all available decrypters.
64         @return  An array of decrypters names.
65     */
66     mod.listDecrypters=function(){
67         var c=[];
68         for(var attr in String.prototype){
69             if(attr.slice(0, 8) == "decrypt_"){
70                 c.push(attr.slice(8));
71             }
72         }
73         return c;
74     }
75     
76     /**
77         Encrypts a string.
78         Parameters but the crypdec parameter are forwardet to the crypdec.
79         @param codec  The codec to use.
80     */
81     String.prototype.encrypt=function(crydec){
82         var n = "encrypt_" + crydec;
83         if(String.prototype[n]){
84             var args=[];
85             for(var i=1;i<arguments.length;i++){
86                 args[i-1] = arguments[i];
87             }
88             return String.prototype[n].apply(this, args);
89         }else{
90             throw new mod.Exception("Decrypter '%s' not found.".format(crydec));
91         }
92     }
93     /**
94         Decrypts a string.
95         Parameters but the crypdec parameter are forwardet to the crypdec.
96         @param codec  The codec to use.
97     */
98     String.prototype.decrypt=function(crydec){
99         var n = "decrypt_" + crydec;
100         if(String.prototype[n]){
101             var args=[];
102             for(var i=1;i<arguments.length;i++){
103                 args[i-1] = arguments[i];
104             }
105             return String.prototype[n].apply(this, args);
106         }else{
107             throw new mod.Exception("Encrypter '%s' not found.".format(crydec));
108         }
109     }
110     
111     /**
112         Encrypts a string using XOR.
113         The whole String will be XORed with the key. 
114         If the key is shorter than the String then it will be multiplied to fit the length of the String.
115         @param key  The key to use for encryption.
116     */
117     String.prototype.encrypt_xor=function(key){
118         var e=new Array(this.length);
119         var l=key.length;
120         for(var i=0;i<this.length;i++){
121             e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l));
122         }
123         return e.join("");
124     }
125     /**
126         Decrypts a string using XOR.
127         Since XORing is symetric it is the same as the encrypter.
128         @param key  The key to use for decryption.
129     */
130     String.prototype.decrypt_xor=String.prototype.encrypt_xor;
131     /**
132         Encrypts a string using the ARC4 algorithm.
133         @param key  The key to use for encryption.
134     */
135     String.prototype.encrypt_rc4=function(key){
136         //generate substitution box
137         var sbox = new Array (256);
138         for (var i=0; i<256; i++){
139              sbox[i]=i;
140         }
141         
142         //swap things around 
143         var j=0;
144         for (var i=0; i < 256; i++) {
145              j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256;
146              var tmp = sbox[i];
147              sbox[i] = sbox[j];
148              sbox[j] = tmp;
149         }
150         
151         //calculate the result
152         var i=256;    
153         var j=256;
154         var rslt=new Array(this.length);
155         for (var k=0; k < this.length; k++) {
156             i = (i + 1) % 256;
157             j = (j + sbox[i]) % 256;
158             var tmp = sbox[i];
159             sbox[i] = sbox[j];
160             sbox[j] = tmp;
161             t = (sbox[i] + sbox[j]) % 256;
162             rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]);
163         }
164         return rslt.join("");
165     }
166     /**
167         Decrypts a string using the ARC4 algorithm.
168         Since it is symetric it is the same as the encrypter.
169         @param key  The key to use for decryption.
170     */
171     String.prototype.decrypt_rc4=String.prototype.encrypt_rc4;
172 })