Author: doogie
Date: Fri May 4 20:42:45 2012 New Revision: 1334172 URL: http://svn.apache.org/viewvc?rev=1334172&view=rev Log: FIX/DEPRECATION: Fix all the HashCrypt deprecated uses. All proper methods in HashCrypt now follow the same form: (String hashType, ..., String valueToHash). Some methods also follow a polish notation suffix. Modified: ofbiz/trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java ofbiz/trunk/framework/datafile/src/org/ofbiz/datafile/Record.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java Modified: ofbiz/trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java?rev=1334172&r1=1334171&r2=1334172&view=diff ============================================================================== --- ofbiz/trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java (original) +++ ofbiz/trunk/applications/party/src/org/ofbiz/party/contact/ContactMechServices.java Fri May 4 20:42:45 2012 @@ -1053,7 +1053,7 @@ public class ContactMechServices { synchronized(ContactMechServices.class) { while (true) { Long random = secureRandom.nextLong(); - verifyHash = HashCrypt.getDigestHash(Long.toString(random), "MD5"); + verifyHash = HashCrypt.digestHash("MD5", Long.toString(random).getBytes()); List<GenericValue> emailAddVerifications = null; try { emailAddVerifications = delegator.findByAnd("EmailAddressVerification", UtilMisc.toMap("verifyHash", verifyHash)); Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java?rev=1334172&r1=1334171&r2=1334172&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java Fri May 4 20:42:45 2012 @@ -145,57 +145,53 @@ public class HashCrypt { } /** - * @deprecated use cryptPassword + * @deprecated use digestHash("SHA", null, str) */ @Deprecated public static String getDigestHash(String str) { - return getDigestHash(str, "SHA"); + return digestHash("SHA", null, str); } /** - * @deprecated use cryptPassword + * @deprecated use digestHash(hashType, null, str)) */ @Deprecated public static String getDigestHash(String str, String hashType) { - if (str == null) return null; - try { - MessageDigest messagedigest = MessageDigest.getInstance(hashType); - byte[] strBytes = str.getBytes(); - - messagedigest.update(strBytes); - byte[] digestBytes = messagedigest.digest(); - char[] digestChars = Hex.encodeHex(digestBytes); - - StringBuilder sb = new StringBuilder(); - sb.append("{").append(hashType).append("}"); - sb.append(digestChars, 0, digestChars.length); - return sb.toString(); - } catch (Exception e) { - throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e); - } + return digestHash(hashType, null, str); } /** - * @deprecated use cryptPassword + * @deprecated use digestHash(hashType, code, str); */ @Deprecated public static String getDigestHash(String str, String code, String hashType) { + return digestHash(hashType, code, str); + } + + public static String digestHash(String hashType, String code, String str) { if (str == null) return null; + byte[] codeBytes; try { - byte codeBytes[] = null; - if (code == null) codeBytes = str.getBytes(); else codeBytes = str.getBytes(code); - MessageDigest messagedigest = MessageDigest.getInstance(hashType); + } catch (UnsupportedEncodingException e) { + throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e); + } + return digestHash(hashType, codeBytes); + } - messagedigest.update(codeBytes); - byte digestBytes[] = messagedigest.digest(); + public static String digestHash(String hashType, byte[] bytes) { + try { + MessageDigest messagedigest = MessageDigest.getInstance(hashType); + messagedigest.update(bytes); + byte[] digestBytes = messagedigest.digest(); char[] digestChars = Hex.encodeHex(digestBytes); + StringBuilder sb = new StringBuilder(); sb.append("{").append(hashType).append("}"); sb.append(digestChars, 0, digestChars.length); return sb.toString(); - } catch (Exception e) { + } catch (NoSuchAlgorithmException e) { throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e); } } @@ -243,15 +239,19 @@ public class HashCrypt { } /** - * @deprecated no replacement, logic moved into comparePassword + * @deprecated use digestHashOldFunnyHex(hashType, str) */ @Deprecated public static String getDigestHashOldFunnyHexEncode(String str, String hashType) { + return digestHashOldFunnyHex(hashType, str); + } + + public static String digestHashOldFunnyHex(String hashType, String str) { if (UtilValidate.isEmpty(hashType)) hashType = "SHA"; if (str == null) return null; try { MessageDigest messagedigest = MessageDigest.getInstance(hashType); - byte strBytes[] = str.getBytes(); + byte[] strBytes = str.getBytes(); messagedigest.update(strBytes); return oldFunnyHex(messagedigest.digest()); Modified: ofbiz/trunk/framework/datafile/src/org/ofbiz/datafile/Record.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/datafile/src/org/ofbiz/datafile/Record.java?rev=1334172&r1=1334171&r2=1334172&view=diff ============================================================================== --- ofbiz/trunk/framework/datafile/src/org/ofbiz/datafile/Record.java (original) +++ ofbiz/trunk/framework/datafile/src/org/ofbiz/datafile/Record.java Fri May 4 20:42:45 2012 @@ -269,7 +269,7 @@ public class Record implements Serializa else if (fieldType.equals("java.lang.String") || fieldType.equals("String")) if (field.format.equals("EncryptedString")) { String hashType = LoginServices.getHashType(); - set(name, HashCrypt.getDigestHash(value, hashType)); + set(name, HashCrypt.digestHash(hashType, value.getBytes())); } else { set(name, value); } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java?rev=1334172&r1=1334171&r2=1334172&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java Fri May 4 20:42:45 2012 @@ -1290,7 +1290,11 @@ public class GenericEntity extends Obser ModelField field = this.getModelEntity().getField(curKey); if (field.getEncrypt() && curValue instanceof String) { String encryptField = (String) curValue; - curValue = HashCrypt.getDigestHash(encryptField); + // the encryptField may not actually be UTF8, it could be any + // random encoding; just treat it as a series of raw bytes. + // This won't give the same output as the value stored in the + // database, but should be good enough for printing + curValue = HashCrypt.cryptBytes(null, null, encryptField.getBytes()); } theString.append('['); theString.append(curKey); Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java?rev=1334172&r1=1334171&r2=1334172&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java Fri May 4 20:42:45 2012 @@ -119,7 +119,7 @@ public class EntityCrypto { } protected SecretKey getKeyFromStore(String originalKeyName, boolean useOldFunnyKeyHash) throws EntityCryptoException { - String hashedKeyName = useOldFunnyKeyHash? HashCrypt.getDigestHashOldFunnyHexEncode(originalKeyName, null) : HashCrypt.getDigestHash(originalKeyName); + String hashedKeyName = useOldFunnyKeyHash? HashCrypt.digestHashOldFunnyHex(null, originalKeyName) : HashCrypt.digestHash("SHA", null, originalKeyName); GenericValue keyValue = null; try { |
Free forum by Nabble | Edit this page |