[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 /* 2 Copyright (c) 2003 Jan-Klaas Kollhof 3 4 This file is part of the JavaScript o lait library(jsolait). 5 6 jsolait is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published by 8 the Free Software Foundation; either version 2.1 of the License, or 9 (at your option) any later version. 10 11 This software is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with this software; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 /** 22 Cryptography module. 23 Provides String encryption/decryption and hashing. 24 */ 25 Module("crypto", "0.1.2", function(mod){ 26 /** 27 Returns all all available encrypters. 28 @return An array of encrypters names. 29 */ 30 mod.listEncrypters=function(){ 31 var c=[]; 32 for(var attr in String.prototype){ 33 if(attr.slice(0, 8) == "encrypt_"){ 34 c.push(attr.slice(8)); 35 } 36 } 37 return c; 38 } 39 /** 40 Returns all all available decrypters. 41 @return An array of decrypters names. 42 */ 43 mod.listDecrypters=function(){ 44 var c=[]; 45 for(var attr in String.prototype){ 46 if(attr.slice(0, 8) == "decrypt_"){ 47 c.push(attr.slice(8)); 48 } 49 } 50 return c; 51 } 52 53 /** 54 Encrypts a string. 55 Parameters but the crypdec parameter are forwardet to the crypdec. 56 @param codec The codec to use. 57 */ 58 String.prototype.encrypt=function(crydec){ 59 var n = "encrypt_" + crydec; 60 if(String.prototype[n]){ 61 var args=[]; 62 for(var i=1;i<arguments.length;i++){ 63 args[i-1] = arguments[i]; 64 } 65 return String.prototype[n].apply(this, args); 66 }else{ 67 throw new mod.Exception("Decrypter '%s' not found.".format(crydec)); 68 } 69 } 70 /** 71 Decrypts a string. 72 Parameters but the crypdec parameter are forwardet to the crypdec. 73 @param codec The codec to use. 74 */ 75 String.prototype.decrypt=function(crydec){ 76 var n = "decrypt_" + crydec; 77 if(String.prototype[n]){ 78 var args=[]; 79 for(var i=1;i<arguments.length;i++){ 80 args[i-1] = arguments[i]; 81 } 82 return String.prototype[n].apply(this, args); 83 }else{ 84 throw new mod.Exception("Encrypter '%s' not found.".format(crydec)); 85 } 86 } 87 88 /** 89 Encrypts a string using XOR. 90 The whole String will be XORed with the key. 91 If the key is shorter than the String then it will be multiplied to fit the length of the String. 92 @param key The key to use for encryption. 93 */ 94 String.prototype.encrypt_xor=function(key){ 95 var e=new Array(this.length); 96 var l=key.length; 97 for(var i=0;i<this.length;i++){ 98 e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l)); 99 } 100 return e.join(""); 101 } 102 /** 103 Decrypts a string using XOR. 104 Since XORing is symetric it is the same as the encrypter. 105 @param key The key to use for decryption. 106 */ 107 String.prototype.decrypt_xor=String.prototype.encrypt_xor; 108 /** 109 Encrypts a string using the ARC4 algorithm. 110 @param key The key to use for encryption. 111 */ 112 String.prototype.encrypt_rc4=function(key){ 113 //generate substitution box 114 var sbox = new Array (256); 115 for (var i=0; i<256; i++){ 116 sbox[i]=i; 117 } 118 119 //swap things around 120 var j=0; 121 for (var i=0; i < 256; i++) { 122 j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256; 123 var tmp = sbox[i]; 124 sbox[i] = sbox[j]; 125 sbox[j] = tmp; 126 } 127 128 //calculate the result 129 var i=256; 130 var j=256; 131 var rslt=new Array(this.length); 132 for (var k=0; k < this.length; k++) { 133 i = (i + 1) % 256; 134 j = (j + sbox[i]) % 256; 135 var tmp = sbox[i]; 136 sbox[i] = sbox[j]; 137 sbox[j] = tmp; 138 t = (sbox[i] + sbox[j]) % 256; 139 rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]); 140 } 141 return rslt.join(""); 142 } 143 /** 144 Decrypts a string using the ARC4 algorithm. 145 Since it is symetric it is the same as the encrypter. 146 @param key The key to use for decryption. 147 */ 148 String.prototype.decrypt_rc4=String.prototype.encrypt_rc4; 149 })
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |