001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.util;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.security.MessageDigest;
022: import java.security.NoSuchAlgorithmException;
023:
024: import de.finix.contelligent.core.ContelligentImpl;
025: import de.finix.contelligent.exception.ContelligentRuntimeException;
026: import de.finix.contelligent.logging.LoggingService;
027:
028: public class StringHash {
029:
030: static final org.apache.log4j.Logger log = LoggingService
031: .getLogger(StringHash.class);
032:
033: static final private String ALPHA32 = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
034:
035: /**
036: * Creates a hex-dump hash string out of a given source text, using SHA-1.
037: */
038: static public String getHash(String sourceText) {
039: try {
040: MessageDigest md = MessageDigest.getInstance("SHA-1");
041: byte[] msg;
042: try {
043: msg = sourceText.getBytes(ContelligentImpl
044: .getInstance().getDefaultEncoding());
045: } catch (UnsupportedEncodingException uee) {
046: log.error("Unsupported encoding.", uee);
047: msg = sourceText.getBytes();
048: }
049: md.update(msg);
050: byte[] digest = md.digest();
051: StringBuffer calcKey = new StringBuffer();
052: byte[] c = new byte[2];
053: int j;
054: for (int i = 0; i < digest.length; i++) {
055: j = digest[i];
056: if (j < 0)
057: j = 256 + j;
058: c[0] = (byte) ((j / 16) + 48);
059: if (c[0] > 57)
060: c[0] += 39;
061: c[1] = (byte) ((j % 16) + 48);
062: if (c[1] > 57)
063: c[1] += 39;
064: calcKey.append(new String(c));
065: }
066: return calcKey.toString();
067: } catch (NoSuchAlgorithmException nsae) {
068: throw new ContelligentRuntimeException(
069: "Unable to generate hash.", nsae);
070: }
071: }
072:
073: static public String getHash16(String sourceText) {
074: return StringHash.getHash(sourceText);
075: }
076:
077: /**
078: * Creates a base64-dump hash string out of a given source text, using
079: * SHA-1.
080: */
081: static public String getHash64(String sourceText) {
082: try {
083: MessageDigest md = MessageDigest.getInstance("SHA-1");
084: byte[] msg;
085: try {
086: msg = sourceText.getBytes(ContelligentImpl
087: .getInstance().getDefaultEncoding());
088: } catch (UnsupportedEncodingException uee) {
089: log.error("Unsupported encoding.", uee);
090: msg = sourceText.getBytes();
091: }
092: md.update(msg);
093: byte[] digest = md.digest();
094: return Base64.encodeBytes(digest);
095: } catch (NoSuchAlgorithmException nsae) {
096: throw new ContelligentRuntimeException(
097: "Unable to generate hash.", nsae);
098: }
099: }
100:
101: static public String getHash32(String sourceText) {
102: try {
103: MessageDigest md = MessageDigest.getInstance("SHA-1");
104: byte[] msg;
105: try {
106: msg = sourceText.getBytes(ContelligentImpl
107: .getInstance().getDefaultEncoding());
108: } catch (UnsupportedEncodingException uee) {
109: log.error("Unsupported encoding.", uee);
110: msg = sourceText.getBytes();
111: }
112: md.update(msg);
113: byte[] digest = md.digest();
114: StringBuilder output = new StringBuilder();
115: for (int i = 0; i < 4; i++) {
116: int part = digest[i];
117: if (part < 0) {
118: part = 256 + part;
119: }
120: output.append(ALPHA32.charAt(part % 32));
121: }
122: return output.toString();
123: } catch (NoSuchAlgorithmException nsae) {
124: throw new ContelligentRuntimeException(
125: "Unable to generate hash.", nsae);
126: }
127: }
128:
129: static public String getHash32(byte[] digest) {
130: StringBuilder output = new StringBuilder();
131: for (int i = 0; i < 4; i++) {
132: int part = digest[i];
133: if (part < 0) {
134: part = 256 + part;
135: }
136: output.append(ALPHA32.charAt(part % 32));
137: }
138: return output.toString();
139: }
140:
141: }
|