001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.util;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
029:
030: import java.io.ByteArrayInputStream;
031:
032: import java.security.DigestInputStream;
033: import java.security.MessageDigest;
034:
035: import java.util.Arrays;
036:
037: /**
038: *
039: *
040: * @author $author$
041: * @version $Revision: 1.8 $
042: */
043: public class UID {
044: byte[] uid;
045:
046: private UID(String str) throws UIDException {
047: if (str == null) {
048: throw new UIDException("UID cannot be NULL");
049: }
050:
051: try {
052: uid = new byte[str.length() / 2];
053:
054: String tmp;
055: int pos = 0;
056:
057: for (int i = 0; i < str.length(); i += 2) {
058: tmp = str.substring(i, i + 2);
059: uid[pos++] = (byte) Integer.parseInt(tmp, 16);
060: }
061: } catch (NumberFormatException ex) {
062: throw new UIDException("Failed to parse UID String: "
063: + ex.getMessage());
064: }
065: }
066:
067: private UID(byte[] uid) {
068: this .uid = uid;
069: }
070:
071: /**
072: *
073: *
074: * @param content
075: *
076: * @return
077: *
078: * @throws UIDException
079: */
080: public static UID generateUniqueId(byte[] content)
081: throws UIDException {
082: try {
083: // Create a uniqiue identifier from the content and some random data
084: MessageDigest messageDigest = MessageDigest
085: .getInstance("MD5");
086:
087: if (content != null) {
088: ByteArrayInputStream in = new ByteArrayInputStream(
089: content);
090: DigestInputStream dis = new DigestInputStream(in,
091: messageDigest);
092:
093: while (dis.read() != -1) {
094: ;
095: }
096:
097: dis.close();
098: in.close();
099: }
100:
101: // Add some random noise so the id is not generated soley by the
102: // file content
103: byte[] noise = new byte[1024];
104: ConfigurationLoader.getRND().nextBytes(noise);
105: messageDigest.update(noise);
106:
107: // Generate the id
108: UID uid = new UID(messageDigest.digest());
109:
110: return uid;
111: } catch (Exception ex) {
112: throw new UIDException(
113: "Failed to generate a unique identifier: "
114: + ex.getMessage());
115: }
116: }
117:
118: /**
119: *
120: *
121: * @param uid
122: *
123: * @return
124: *
125: * @throws UIDException
126: */
127: public static UID fromString(String uid) throws UIDException {
128: return new UID(uid);
129: }
130:
131: /**
132: *
133: *
134: * @return
135: */
136: public String toString() {
137: StringBuffer checksumSb = new StringBuffer();
138:
139: for (int i = 0; i < uid.length; i++) {
140: String hexStr = Integer.toHexString(0x00ff & uid[i]);
141:
142: if (hexStr.length() < 2) {
143: checksumSb.append("0");
144: }
145:
146: checksumSb.append(hexStr);
147: }
148:
149: return checksumSb.toString();
150: }
151:
152: /**
153: *
154: *
155: * @param obj
156: *
157: * @return
158: */
159: public boolean equals(Object obj) {
160: if ((obj != null) && obj instanceof UID) {
161: return Arrays.equals(uid, ((UID) obj).uid);
162: }
163:
164: return false;
165: }
166:
167: /**
168: *
169: *
170: * @return
171: */
172: public int hashCode() {
173: return toString().hashCode();
174: }
175: }
|