KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > mcrypt > McryptModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.mcrypt;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.env.ArrayValue;
34 import com.caucho.quercus.env.ArrayValueImpl;
35 import com.caucho.quercus.env.BooleanValue;
36 import com.caucho.quercus.env.Env;
37 import com.caucho.quercus.env.LongValue;
38 import com.caucho.quercus.env.Value;
39 import com.caucho.quercus.module.AbstractQuercusModule;
40 import com.caucho.util.L10N;
41 import com.caucho.util.RandomUtil;
42 import com.caucho.vfs.Path;
43
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * PHP encryption
48  */

49 public class McryptModule extends AbstractQuercusModule {
50   private static final L10N L = new L10N(McryptModule.class);
51
52   private static final Logger JavaDoc log =
53     Logger.getLogger(McryptModule.class.getName());
54
55   public static final int MCRYPT_DEV_RANDOM = 0;
56   public static final int MCRYPT_DEV_URANDOM = 1;
57   public static final int MCRYPT_RAND = 2;
58
59   public static final int MCRYPT_ENCRYPT = 0;
60   public static final int MCRYPT_DECRYPT = 1;
61
62   public static final String JavaDoc MCRYPT_MODE_ECB = "ecb";
63   public static final String JavaDoc MCRYPT_MODE_CBC = "cbc";
64   public static final String JavaDoc MCRYPT_MODE_CFB = "cfb";
65   public static final String JavaDoc MCRYPT_MODE_OFB = "ofb";
66   public static final String JavaDoc MCRYPT_MODE_NOFB = "nofb";
67   public static final String JavaDoc MCRYPT_MODE_STREAM = "stream";
68
69   public static final String JavaDoc MCRYPT_ARCFOUR = "arcfour";
70   public static final String JavaDoc MCRYPT_BLOWFISH = "blowfish";
71   public static final String JavaDoc MCRYPT_DES = "des";
72   public static final String JavaDoc MCRYPT_3DES = "tripledes";
73   public static final String JavaDoc MCRYPT_RC4 = "RC4";
74   public static final String JavaDoc MCRYPT_RIJNDAEL_128 = "rijndael-128";
75   public static final String JavaDoc MCRYPT_RIJNDAEL_192 = "rijndael-192";
76   public static final String JavaDoc MCRYPT_RIJNDAEL_256 = "rijndael-256";
77
78   public String JavaDoc []getLoadedExtensions()
79   {
80     return new String JavaDoc[] { "mcrypt" };
81   }
82
83   /**
84    * Encrypt with cbc
85    */

86   public static String JavaDoc mcrypt_cbc(Env env,
87                                   String JavaDoc cipher,
88                                   String JavaDoc key,
89                                   String JavaDoc data,
90                                   int mode,
91                                   @Optional String JavaDoc iv)
92   {
93     try {
94       Mcrypt mcrypt = new Mcrypt(env, cipher, "cbc");
95
96       mcrypt.init(key, iv);
97
98       if (mode == MCRYPT_ENCRYPT)
99         return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
100       else
101         return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
102     } catch (Exception JavaDoc e) {
103       throw new RuntimeException JavaDoc(e);
104     }
105   }
106
107   /**
108    * Encrypt with cfb
109    */

110   public static String JavaDoc mcrypt_cfb(Env env,
111                                   String JavaDoc cipher,
112                                   String JavaDoc key,
113                                   String JavaDoc data,
114                                   int mode,
115                                   @Optional String JavaDoc iv)
116   {
117     try {
118       Mcrypt mcrypt = new Mcrypt(env, cipher, "cfb");
119
120       mcrypt.init(key, iv);
121
122       if (mode == MCRYPT_ENCRYPT)
123         return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
124       else
125         return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
126     } catch (Exception JavaDoc e) {
127       throw new RuntimeException JavaDoc(e);
128     }
129   }
130
131   /**
132    * Creates the IV vector.
133    */

134   public static String JavaDoc mcrypt_create_iv(int size,
135                                         @Optional int randomMode)
136   {
137     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
138
139     for (int i = 0; i < size; i++)
140       sb.append((char) RandomUtil.nextInt(256));
141
142     return sb.toString();
143   }
144
145   /**
146    * Decrypt
147    */

148   public static String JavaDoc mcrypt_decrypt(Env env,
149                                       String JavaDoc cipher,
150                                       String JavaDoc key,
151                                       String JavaDoc data,
152                                       String JavaDoc mode,
153                                       @Optional String JavaDoc iv)
154   {
155     try {
156       Mcrypt mcrypt = new Mcrypt(env, cipher, mode);
157
158       mcrypt.init(key, iv);
159
160       return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
161     } catch (Exception JavaDoc e) {
162       throw new RuntimeException JavaDoc(e);
163     }
164   }
165
166   /**
167    * Encrypt with cfb
168    */

169   public static String JavaDoc mcrypt_ecb(Env env,
170                                   String JavaDoc cipher,
171                                   String JavaDoc key,
172                                   String JavaDoc data,
173                                   int mode,
174                                   @Optional String JavaDoc iv)
175   {
176     try {
177       Mcrypt mcrypt = new Mcrypt(env, cipher, "ecb");
178
179       mcrypt.init(key, iv);
180
181       if (mode == MCRYPT_ENCRYPT)
182         return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
183       else
184         return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
185     } catch (Exception JavaDoc e) {
186       throw new RuntimeException JavaDoc(e);
187     }
188   }
189
190   /**
191    * Returns the algorithms name
192    */

193   public static String JavaDoc mcrypt_enc_get_algorithms_name(Mcrypt mcrypt)
194   {
195     if (mcrypt != null)
196       return mcrypt.get_algorithms_name();
197     else
198       return "";
199   }
200
201   /**
202    * Returns the block size
203    */

204   public static int mcrypt_enc_get_block_size(Mcrypt mcrypt)
205   {
206     if (mcrypt != null)
207       return mcrypt.get_iv_size();
208     else
209       return 0;
210   }
211
212   /**
213    * Returns the IV size
214    */

215   public static int mcrypt_enc_get_iv_size(Mcrypt mcrypt)
216   {
217     if (mcrypt != null)
218       return mcrypt.get_iv_size();
219     else
220       return 0;
221   }
222
223   /**
224    * Returns the key size
225    */

226   public static int mcrypt_enc_get_key_size(Mcrypt mcrypt)
227   {
228     if (mcrypt != null)
229       return mcrypt.get_key_size();
230     else
231       return 0;
232   }
233
234   /**
235    * Returns the mode name
236    */

237   public static String JavaDoc mcrypt_enc_get_modes_name(Mcrypt mcrypt)
238   {
239     if (mcrypt != null)
240       return mcrypt.get_modes_name();
241     else
242       return null;
243   }
244
245   /**
246    * Returns the supported key sizes
247    */

248   public static Value mcrypt_enc_get_supported_key_sizes(Mcrypt mcrypt)
249   {
250     if (mcrypt != null)
251       return mcrypt.get_supported_key_sizes();
252     else
253       return BooleanValue.FALSE;
254   }
255
256   /**
257    * Returns true for block encoding modes
258    */

259   public static boolean mcrypt_enc_is_block_algorithm(Mcrypt mcrypt)
260   {
261     if (mcrypt != null)
262       return mcrypt.is_block_algorithm();
263     else
264       return false;
265   }
266
267   /**
268    * Returns true for block encoding modes
269    */

270   public static boolean mcrypt_enc_is_block_algorithm_mode(Mcrypt mcrypt)
271   {
272     if (mcrypt != null)
273       return mcrypt.is_block_algorithm_mode();
274     else
275       return false;
276   }
277
278   /**
279    * Returns true for block output modes
280    */

281   public static boolean mcrypt_enc_is_block_mode(Mcrypt mcrypt)
282   {
283     if (mcrypt != null)
284       return mcrypt.is_block_mode();
285     else
286       return false;
287   }
288
289   /**
290    * Returns true for block output modes
291    */

292   public static boolean mcrypt_enc_self_test(Mcrypt mcrypt)
293   {
294     if (mcrypt != null)
295       return true;
296     else
297       return false;
298   }
299
300   /**
301    * Encrypt
302    */

303   public static String JavaDoc mcrypt_encrypt(Env env,
304                                       String JavaDoc cipher,
305                                       String JavaDoc key,
306                                       String JavaDoc data,
307                                       String JavaDoc mode,
308                                       @Optional String JavaDoc iv)
309   {
310     try {
311       Mcrypt mcrypt = new Mcrypt(env, cipher, mode);
312
313       mcrypt.init(key, iv);
314
315       return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
316     } catch (Exception JavaDoc e) {
317       throw new RuntimeException JavaDoc(e);
318     }
319   }
320
321   /**
322    * Initialize encrption
323    */

324   public static String JavaDoc mcrypt_generic(Mcrypt mcrypt, String JavaDoc data)
325   {
326     if (mcrypt == null)
327       return null;
328     else
329       return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
330   }
331
332   /**
333    * Initialize encrption
334    */

335   public static boolean mcrypt_generic_deinit(Mcrypt mcrypt)
336   {
337     if (mcrypt == null)
338       return false;
339     else
340       return mcrypt.deinit();
341   }
342
343   /**
344    * Initialize encrption
345    */

346   public static Value mcrypt_generic_init(Mcrypt mcrypt, String JavaDoc key, String JavaDoc iv)
347   {
348     if (mcrypt == null)
349       return BooleanValue.FALSE;
350     else
351       return new LongValue(mcrypt.init(key, iv));
352   }
353
354   /**
355    * Closes the module
356    */

357   public static boolean mcrypt_generic_end(Mcrypt mcrypt)
358   {
359     if (mcrypt == null)
360       return false;
361     else {
362       mcrypt.close();
363
364       return true;
365     }
366   }
367
368   private static final String JavaDoc []ALGORITHMS = {
369     MCRYPT_ARCFOUR, MCRYPT_BLOWFISH, MCRYPT_DES, MCRYPT_3DES,
370     MCRYPT_RC4, MCRYPT_RIJNDAEL_128, MCRYPT_RIJNDAEL_192,
371     MCRYPT_RIJNDAEL_256
372   };
373
374   /**
375    * Lists the available algorithms
376    */

377   public static Value mcrypt_list_algorithms(Env env)
378   {
379     ArrayValue array = new ArrayValueImpl();
380
381     for (int i = 0; i < ALGORITHMS.length; i++) {
382       try {
383         Mcrypt mcrypt = new Mcrypt(env, ALGORITHMS[i], "cbc");
384
385         array.put(mcrypt.get_algorithms_name());
386       } catch (Throwable JavaDoc e) {
387       }
388     }
389
390     return array;
391   }
392
393   /**
394    * Lists the available modes.
395    */

396   public static Value mcrypt_list_modes(Env env)
397   {
398     ArrayValue array = new ArrayValueImpl();
399
400     array.put(MCRYPT_MODE_ECB);
401     array.put(MCRYPT_MODE_CBC);
402     array.put(MCRYPT_MODE_CFB);
403     array.put(MCRYPT_MODE_OFB);
404     array.put(MCRYPT_MODE_NOFB);
405
406     return array;
407   }
408
409
410   /**
411    * Closes the module
412    */

413   public static boolean mcrypt_module_close(Mcrypt mcrypt)
414   {
415     if (mcrypt == null)
416       return false;
417     else {
418       mcrypt.close();
419
420       return true;
421     }
422   }
423
424   /**
425    * Returns the block size for an algorithm.
426    */

427   public static int mcrypt_module_get_algo_block_size(Env env,
428                                                       String JavaDoc cipher,
429                                                       @Optional String JavaDoc libDir)
430   {
431     try {
432       Mcrypt mcrypt = new Mcrypt(env, cipher, "cbc");
433
434       return mcrypt.get_block_size();
435     } catch (Exception JavaDoc e) {
436       env.error(e);
437
438       return -1;
439     }
440   }
441
442   /**
443    * Returns the key size for an algorithm.
444    */

445   public static int mcrypt_module_get_algo_key_size(Env env,
446                                                     String JavaDoc cipher,
447                                                     @Optional String JavaDoc libDir)
448   {
449     try {
450       Mcrypt mcrypt = new Mcrypt(env, cipher, "cbc");
451
452       return mcrypt.get_key_size();
453     } catch (Exception JavaDoc e) {
454       env.error(e);
455
456       return -1;
457     }
458   }
459
460   /**
461    * Returns the key size for an algorithm.
462    */

463   public static Value mcrypt_module_get_supported_key_sizes(Env env,
464                                                             String JavaDoc cipher,
465                                                             @Optional String JavaDoc libDir)
466   {
467     try {
468       Mcrypt mcrypt = new Mcrypt(env, cipher, "cbc");
469
470       return mcrypt.get_supported_key_sizes();
471     } catch (Exception JavaDoc e) {
472       env.error(e);
473
474       return BooleanValue.FALSE;
475     }
476   }
477
478   /**
479    * Returns true for block algorithms
480    */

481   public static boolean mcrypt_module_is_block_algorithm(Env env,
482                                                          String JavaDoc cipher,
483                                                          @Optional String JavaDoc libDir)
484   {
485     try {
486       Mcrypt mcrypt = new Mcrypt(env, cipher, "cbc");
487
488       return mcrypt.is_block_algorithm();
489     } catch (Exception JavaDoc e) {
490       env.error(e);
491
492       return false;
493     }
494   }
495
496   /**
497    * Returns true for block modes
498    */

499   public static boolean mcrypt_module_is_block_algorithm_mode(Env env,
500                                                               String JavaDoc mode,
501                                                               @Optional String JavaDoc libDir)
502   {
503     try {
504       Mcrypt mcrypt = new Mcrypt(env, "des", mode);
505
506       return mcrypt.is_block_algorithm_mode();
507     } catch (Exception JavaDoc e) {
508       env.error(e);
509
510       return false;
511     }
512   }
513
514   /**
515    * Returns true for block modes
516    */

517   public static boolean mcrypt_module_is_block_mode(Env env,
518                                                     String JavaDoc mode,
519                                                     @Optional String JavaDoc libDir)
520   {
521     try {
522       Mcrypt mcrypt = new Mcrypt(env, "des", mode);
523
524       return mcrypt.is_block_mode();
525     } catch (Exception JavaDoc e) {
526       env.error(e);
527
528       return false;
529     }
530   }
531
532   /**
533    * Returns true for block modes
534    */

535   public static boolean mcrypt_module_self_test(Env env,
536                                                 String JavaDoc algorithm,
537                                                 Path libDir)
538   {
539     try {
540       Mcrypt mcrypt = new Mcrypt(env, algorithm, "cbc");
541
542       return true;
543     } catch (Exception JavaDoc e) {
544       env.error(e);
545
546       return false;
547     }
548   }
549
550   /**
551    * Open a new mcrypt object.
552    */

553   public static Value mcrypt_module_open(Env env,
554                                          String JavaDoc algorithm,
555                                          Path algorithm_directory,
556                                          String JavaDoc mode,
557                                          Path mode_directory)
558   {
559     try {
560       return env.wrapJava(new Mcrypt(env, algorithm, mode));
561     } catch (Exception JavaDoc e) {
562       env.error(e);
563
564       return BooleanValue.FALSE;
565     }
566   }
567
568   /**
569    * Encrypt with ofb
570    */

571   public static String JavaDoc mcrypt_ofb(Env env,
572                                   String JavaDoc cipher,
573                                   String JavaDoc key,
574                                   String JavaDoc data,
575                                   int mode,
576                                   @Optional String JavaDoc iv)
577   {
578     try {
579       Mcrypt mcrypt = new Mcrypt(env, cipher, "ofb");
580
581       mcrypt.init(key, iv);
582
583       if (mode == MCRYPT_ENCRYPT)
584         return new String JavaDoc(mcrypt.encrypt(data.getBytes()));
585       else
586         return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
587     } catch (Exception JavaDoc e) {
588       throw new RuntimeException JavaDoc(e);
589     }
590   }
591
592   /**
593    * Initialize encrption
594    */

595   public static String JavaDoc mdecrypt_generic(Mcrypt mcrypt, String JavaDoc data)
596   {
597     if (mcrypt == null)
598       return null;
599     else
600       return new String JavaDoc(mcrypt.decrypt(data.getBytes()));
601   }
602 }
603
Popular Tags