001: /*
002: * $Id: HashEncrypt.java,v 1.1 2003/08/17 10:51:03 jonesde Exp $
003: *
004: * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024:
025: package org.ofbiz.securityext.login;
026:
027: import java.security.MessageDigest;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.base.util.UtilProperties;
031:
032: /**
033: * Utility class for doing SHA One-Way Hash Encryption
034: *
035: *@author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: *@created 10 Mar 2002
037: *@version 1.0
038: */
039: public class HashEncrypt {
040:
041: public static final String module = HashEncrypt.class.getName();
042:
043: private static char hexChars[] = { '0', '1', '2', '3', '4', '5',
044: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
045:
046: public static String getHash(String str) {
047: String hashType = UtilProperties.getPropertyValue(
048: "security.properties", "password.encrypt.hash.type");
049:
050: if (hashType == null || hashType.length() == 0) {
051: Debug
052: .logWarning(
053: "Password encrypt hash type is not specified in security.properties, use SHA",
054: module);
055: hashType = "SHA";
056: }
057:
058: return getDigestHash(str, hashType);
059: }
060:
061: public static String getDigestHash(String str, String hashType) {
062: if (str == null)
063: return null;
064: try {
065: MessageDigest messagedigest = MessageDigest
066: .getInstance(hashType);
067: int i = str.length();
068: byte strBytes[] = str.getBytes();
069:
070: messagedigest.update(strBytes);
071: byte digestBytes[] = messagedigest.digest();
072: int k = 0;
073: char digestChars[] = new char[digestBytes.length * 2];
074:
075: for (int l = 0; l < digestBytes.length; l++) {
076: int i1 = digestBytes[l];
077:
078: if (i1 < 0)
079: i1 = 127 + i1 * -1;
080: encodeInt(i1, k, digestChars);
081: k += 2;
082: }
083:
084: return new String(digestChars, 0, digestChars.length);
085: } catch (Exception e) {
086: Debug.logError(e, "Error while computing hash of type "
087: + hashType, module);
088: }
089: return str;
090: }
091:
092: public static String getDigestHash(String str, String code,
093: String hashType) {
094: if (str == null)
095: return null;
096: try {
097: byte codeBytes[] = null;
098:
099: if (code == null)
100: codeBytes = str.getBytes();
101: else
102: codeBytes = str.getBytes(code);
103: MessageDigest messagedigest = MessageDigest
104: .getInstance(hashType);
105:
106: messagedigest.update(codeBytes);
107: byte digestBytes[] = messagedigest.digest();
108: int i = 0;
109: char digestChars[] = new char[digestBytes.length * 2];
110:
111: for (int j = 0; j < digestBytes.length; j++) {
112: int k = digestBytes[j];
113:
114: if (k < 0) {
115: k = 127 + k * -1;
116: }
117: encodeInt(k, i, digestChars);
118: i += 2;
119: }
120:
121: return new String(digestChars, 0, digestChars.length);
122: } catch (Exception e) {
123: Debug.logError(e, "Error while computing hash of type "
124: + hashType, module);
125: }
126: return str;
127: }
128:
129: private static char[] encodeInt(int i, int j, char digestChars[]) {
130: if (i < 16) {
131: digestChars[j] = '0';
132: }
133: j++;
134: do {
135: digestChars[j--] = hexChars[i & 0xf];
136: i >>>= 4;
137: } while (i != 0);
138: return digestChars;
139: }
140: }
|