KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > factories > PublicEncryptionFactory


1 /*
2  * Created on Sep 30, 2004
3  *
4  */

5 package com.dotmarketing.cms.factories;
6
7 import java.security.Key JavaDoc;
8 import java.util.Random JavaDoc;
9
10 import com.dotmarketing.exception.DotRuntimeException;
11 import com.dotmarketing.util.Logger;
12 import com.liferay.portal.model.Company;
13 import com.liferay.util.Encryptor;
14 import com.liferay.util.EncryptorException;
15
16 /**
17  * @author will
18  *
19  */

20 public class PublicEncryptionFactory {
21     
22     public static String JavaDoc getRandomPassword(){
23         // random number between 10000 AND 99999
24
Random JavaDoc r = new Random JavaDoc();
25         int passInt = Math.abs(r.nextInt() + 10000) % 100000;
26         return String.valueOf(passInt);
27     }
28     
29     public static String JavaDoc getRandomEncryptedPassword(){
30         return encryptString(getRandomPassword());
31     }
32     
33     public static String JavaDoc encryptString(String JavaDoc x){
34         try{
35             Company c = PublicCompanyFactory.getDefaultCompany();
36             Logger.debug(PublicEncryptionFactory.class, "c:"+c);
37             Key JavaDoc k = c.getKeyObj();
38             return Encryptor.encrypt(k, x);
39             
40         }
41         catch(EncryptorException e){
42             throw new DotRuntimeException("Encryption Failed");
43         }
44         
45     }
46     
47     public static String JavaDoc digestString(String JavaDoc x){
48         if(x == null) return null;
49         try{
50             
51             return Encryptor.digest(x);
52             
53         }
54         catch(Exception JavaDoc e){
55             Logger.error(PublicEncryptionFactory.class, "", e);
56             throw new DotRuntimeException("Encryption digest");
57         }
58     }
59     
60     public static String JavaDoc decryptString(String JavaDoc x){
61         try{
62             
63             Key JavaDoc k = PublicCompanyFactory.getDefaultCompany().getKeyObj();
64             return Encryptor.decrypt(k, x);
65             
66         }
67         catch(EncryptorException e){
68             Logger.error(PublicEncryptionFactory.class, "", e);
69             throw new DotRuntimeException("decryption Failed");
70         }
71     }
72     
73 }
74
Popular Tags