1 5 package com.dotmarketing.cms.factories; 6 7 import java.security.Key ; 8 import java.util.Random ; 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 20 public class PublicEncryptionFactory { 21 22 public static String getRandomPassword(){ 23 Random r = new Random (); 25 int passInt = Math.abs(r.nextInt() + 10000) % 100000; 26 return String.valueOf(passInt); 27 } 28 29 public static String getRandomEncryptedPassword(){ 30 return encryptString(getRandomPassword()); 31 } 32 33 public static String encryptString(String x){ 34 try{ 35 Company c = PublicCompanyFactory.getDefaultCompany(); 36 Logger.debug(PublicEncryptionFactory.class, "c:"+c); 37 Key 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 digestString(String x){ 48 if(x == null) return null; 49 try{ 50 51 return Encryptor.digest(x); 52 53 } 54 catch(Exception e){ 55 Logger.error(PublicEncryptionFactory.class, "", e); 56 throw new DotRuntimeException("Encryption digest"); 57 } 58 } 59 60 public static String decryptString(String x){ 61 try{ 62 63 Key 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 |