01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.core.util;
17:
18: import java.security.MessageDigest;
19: import java.security.NoSuchAlgorithmException;
20:
21: import org.apache.ojb.broker.util.GUID;
22:
23: /**
24: *
25: * This class wraps an OJB Guid so that it conforms to the format (and using the algorithm) described in
26: * RFC 4122 entitled " A Universally Unique IDentifier (UUID) URN Namespace"
27: */
28: public class Guid {
29:
30: private final static char HYPHEN = '-';
31: private final static String DIGITS = "0123456789ABCDEF";
32: private String stringValue = null;
33: private GUID guid;
34:
35: public Guid() {
36:
37: guid = new GUID(); // This OJB class is deprecated; remove this line when we upgrade OJB
38:
39: String guidString = guid.toString();
40: // this is roughly the prefered way with the new OJB GUIDFactory:
41: // String guidString=org.apache.ojb.broker.util.GUIDFactory.next();
42:
43: MessageDigest sha;
44: try {
45: sha = MessageDigest.getInstance("SHA-1");
46: } catch (NoSuchAlgorithmException e) {
47: throw new RuntimeException(e);
48: }
49: sha.update(guidString.getBytes());
50: byte[] hash = sha.digest();
51:
52: StringBuffer result = new StringBuffer();
53: for (int i = 0; i < hash.length; i++) {
54: result.append(toHex(hash[i]));
55: }
56:
57: // hyphenate
58: for (int i = 20; i > 4; i -= 4) {
59: result.insert(i, HYPHEN);
60: }
61:
62: // truncate
63: result.delete(32, 40);
64: stringValue = result.toString();
65: }
66:
67: public static String toHex(byte b) {
68:
69: int ub = b < 0 ? b + 256 : b;
70:
71: StringBuffer result = new StringBuffer(2);
72: result.append(DIGITS.charAt(ub / 16));
73: result.append(DIGITS.charAt(ub % 16));
74:
75: return result.toString();
76: }
77:
78: @Override
79: public String toString() {
80: return stringValue;
81: }
82:
83: }
|