1 25 package org.ofbiz.entity.util; 26 27 import java.security.NoSuchAlgorithmException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Random ; 31 32 import javax.crypto.SecretKey; 33 import javax.transaction.Transaction ; 34 35 import org.ofbiz.base.crypto.DesCrypt; 36 import org.ofbiz.base.crypto.HashCrypt; 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.GeneralException; 39 import org.ofbiz.base.util.StringUtil; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.base.util.UtilObject; 42 import org.ofbiz.entity.EntityCryptoException; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.transaction.GenericTransactionException; 47 import org.ofbiz.entity.transaction.TransactionUtil; 48 49 56 public class EntityCrypto { 57 58 public static final String module = EntityCrypto.class.getName(); 59 60 protected GenericDelegator delegator = null; 61 protected Map keyMap = null; 62 63 protected EntityCrypto() { } 64 public EntityCrypto(GenericDelegator delegator) { 65 this.delegator = delegator; 66 this.keyMap = new HashMap (); 67 68 synchronized(EntityCrypto.class) { 71 try { 72 long size = delegator.findCountByAnd("EntityKeyStore", null); 73 if (size == 0) { 74 for (int i = 0; i < 20; i++) { 75 String randomName = this.getRandomString(); 76 this.getKeyFromStore(randomName); 77 } 78 } 79 } catch (GenericEntityException e) { 80 Debug.logError(e, module); 81 } 82 } 83 } 84 85 86 public String encrypt(String keyName, Object obj) throws EntityCryptoException { 87 try { 88 return StringUtil.toHexString(DesCrypt.encrypt(this.getKey(keyName), UtilObject.getBytes(obj))); 89 } catch (GeneralException e) { 90 throw new EntityCryptoException(e); 91 } 92 } 93 94 95 public Object decrypt(String keyName, String str) throws EntityCryptoException { 96 try { 97 return UtilObject.getObject(DesCrypt.decrypt(this.getKey(keyName), StringUtil.fromHexString(str))); 98 } catch (GeneralException e) { 99 throw new EntityCryptoException(e); 100 } 101 } 102 103 protected SecretKey getKey(String name) throws EntityCryptoException { 104 SecretKey key = (SecretKey) keyMap.get(name); 105 if (key == null) { 106 synchronized(this) { 107 String keyName = HashCrypt.getDigestHash(name); 108 key = this.getKeyFromStore(keyName); 109 keyMap.put(name, key); 110 } 111 } 112 return key; 113 } 114 115 protected SecretKey getKeyFromStore(String keyName) throws EntityCryptoException { 116 GenericValue keyValue = null; 117 try { 118 keyValue = delegator.findByPrimaryKey("EntityKeyStore", UtilMisc.toMap("keyName", keyName)); 119 } catch (GenericEntityException e) { 120 throw new EntityCryptoException(e); 121 } 122 if (keyValue == null || keyValue.get("keyText") == null) { 123 SecretKey key = null; 124 try { 125 key = DesCrypt.generateKey(); 126 } catch (NoSuchAlgorithmException e) { 127 throw new EntityCryptoException(e); 128 } 129 GenericValue newValue = delegator.makeValue("EntityKeyStore", null); 130 newValue.set("keyText", StringUtil.toHexString(key.getEncoded())); 131 newValue.set("keyName", keyName); 132 133 Transaction parentTransaction = null; 134 boolean beganTrans = false; 135 try { 136 beganTrans = TransactionUtil.begin(); 137 } catch (GenericTransactionException e) { 138 throw new EntityCryptoException(e); 139 } 140 141 if (!beganTrans) { 142 try { 143 parentTransaction = TransactionUtil.suspend(); 144 } catch (GenericTransactionException e) { 145 throw new EntityCryptoException(e); 146 } 147 148 try { 150 beganTrans = TransactionUtil.begin(); 151 } catch (GenericTransactionException e) { 152 throw new EntityCryptoException(e); 153 } 154 } 155 156 try { 157 delegator.create(newValue); 158 } catch (GenericEntityException e) { 159 try { 160 TransactionUtil.rollback(beganTrans, "Error creating encrypted value", e); 161 } catch (GenericTransactionException e1) { 162 Debug.logError(e1, "Could not rollback transaction", module); 163 } 164 throw new EntityCryptoException(e); 165 } finally { 166 try { 167 TransactionUtil.commit(beganTrans); 168 } catch (GenericTransactionException e) { 169 throw new EntityCryptoException(e); 170 } 171 if (parentTransaction != null) { 173 try { 174 TransactionUtil.resume(parentTransaction); 175 } catch (GenericTransactionException e) { 176 throw new EntityCryptoException(e); 177 } 178 } 179 } 180 181 182 return key; 183 } else { 184 byte[] keyBytes = StringUtil.fromHexString(keyValue.getString("keyText")); 185 try { 186 return DesCrypt.getDesKey(keyBytes); 187 } catch (GeneralException e) { 188 throw new EntityCryptoException(e); 189 } 190 } 191 } 192 193 protected String getRandomString() { 194 Random rand = new Random (); 195 byte[] randomBytes = new byte[24]; 196 rand.nextBytes(randomBytes); 197 return StringUtil.toHexString(randomBytes); 198 } 199 } 200 | Popular Tags |