KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > crypto > HashCrypt


1 /*
2  * $Id: HashCrypt.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.crypto;
26
27 import java.security.MessageDigest JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.StringUtil;
31
32 /**
33  * Utility class for doing SHA-1/MD5 One-Way Hash Encryption
34  *
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 3.2
39  */

40 public class HashCrypt {
41
42     public static final String JavaDoc module = HashCrypt.class.getName();
43
44     public static String JavaDoc getDigestHash(String JavaDoc str) {
45         return getDigestHash(str, "SHA");
46     }
47
48     public static String JavaDoc getDigestHash(String JavaDoc str, String JavaDoc hashType) {
49         if (str == null) return null;
50         try {
51             MessageDigest JavaDoc 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 JavaDoc(digestChars, 0, digestChars.length);
69         } catch (Exception JavaDoc e) {
70             Debug.logError(e, "Error while computing hash of type " + hashType, module);
71         }
72         return str;
73     }
74
75     public static String JavaDoc getDigestHash(String JavaDoc str, String JavaDoc code, String JavaDoc 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 JavaDoc 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 JavaDoc(digestChars, 0, digestChars.length);
100         } catch (Exception JavaDoc e) {
101             Debug.logError(e, "Error while computing hash of type " + hashType, module);
102         }
103         return str;
104     }
105 }
106
Popular Tags