1 25 package org.ofbiz.base.crypto; 26 27 import java.security.MessageDigest ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.StringUtil; 31 32 40 public class HashCrypt { 41 42 public static final String module = HashCrypt.class.getName(); 43 44 public static String getDigestHash(String str) { 45 return getDigestHash(str, "SHA"); 46 } 47 48 public static String getDigestHash(String str, String hashType) { 49 if (str == null) return null; 50 try { 51 MessageDigest messagedigest = MessageDigest.getInstance(hashType); 52 byte strBytes[] = str.getBytes(); 53 54 messagedigest.update(strBytes); 55 byte digestBytes[] = messagedigest.digest(); 56 int k = 0; 57 char digestChars[] = new char[digestBytes.length * 2]; 58 59 for (int l = 0; l < digestBytes.length; l++) { 60 int i1 = digestBytes[l]; 61 62 if (i1 < 0) 63 i1 = 127 + i1 * -1; 64 StringUtil.encodeInt(i1, k, digestChars); 65 k += 2; 66 } 67 68 return new String (digestChars, 0, digestChars.length); 69 } catch (Exception e) { 70 Debug.logError(e, "Error while computing hash of type " + hashType, module); 71 } 72 return str; 73 } 74 75 public static String getDigestHash(String str, String code, String hashType) { 76 if (str == null) return null; 77 try { 78 byte codeBytes[] = null; 79 80 if (code == null) codeBytes = str.getBytes(); 81 else codeBytes = str.getBytes(code); 82 MessageDigest messagedigest = MessageDigest.getInstance(hashType); 83 84 messagedigest.update(codeBytes); 85 byte digestBytes[] = messagedigest.digest(); 86 int i = 0; 87 char digestChars[] = new char[digestBytes.length * 2]; 88 89 for (int j = 0; j < digestBytes.length; j++) { 90 int k = digestBytes[j]; 91 92 if (k < 0) { 93 k = 127 + k * -1; 94 } 95 StringUtil.encodeInt(k, i, digestChars); 96 i += 2; 97 } 98 99 return new String (digestChars, 0, digestChars.length); 100 } catch (Exception e) { 101 Debug.logError(e, "Error while computing hash of type " + hashType, module); 102 } 103 return str; 104 } 105 } 106 | Popular Tags |