comparison src/org/tmatesoft/hg/internal/DigestHelper.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents src/com/tmate/hgkit/ll/DigestHelper.java@92c3d0920d58
children a5275143664c
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.security.MessageDigest;
22 import java.security.NoSuchAlgorithmException;
23
24 import org.tmatesoft.hg.core.Nodeid;
25
26
27 /**
28 * <pre>
29 * DigestHelper dh;
30 * dh.sha1(...).asHexString();
31 * or
32 * dh = dh.sha1(...);
33 * nodeid.equalsTo(dh.asBinary());
34 * </pre>
35 *
36 * @author Artem Tikhomirov
37 * @author TMate Software Ltd.
38 */
39 public class DigestHelper {
40 private MessageDigest sha1;
41 private byte[] digest;
42
43 public DigestHelper() {
44 }
45
46 private MessageDigest getSHA1() {
47 if (sha1 == null) {
48 try {
49 sha1 = MessageDigest.getInstance("SHA-1");
50 } catch (NoSuchAlgorithmException ex) {
51 // could hardly happen, JDK from Sun always has sha1.
52 ex.printStackTrace(); // FIXME log error
53 }
54 }
55 return sha1;
56 }
57
58
59 public DigestHelper sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) {
60 return sha1(nodeid1.toByteArray(), nodeid2.toByteArray(), data);
61 }
62
63 // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text)
64 public DigestHelper sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
65 MessageDigest alg = getSHA1();
66 if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) {
67 alg.update(nodeidParent1);
68 alg.update(nodeidParent2);
69 } else {
70 alg.update(nodeidParent2);
71 alg.update(nodeidParent1);
72 }
73 digest = alg.digest(data);
74 assert digest.length == 20;
75 return this;
76 }
77
78 public String asHexString() {
79 if (digest == null) {
80 throw new IllegalStateException("Shall init with sha1() call first");
81 }
82 return toHexString(digest, 0, digest.length);
83 }
84
85 // by reference, be careful not to modify (or #clone() if needed)
86 public byte[] asBinary() {
87 if (digest == null) {
88 throw new IllegalStateException("Shall init with sha1() call first");
89 }
90 return digest;
91 }
92
93 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
94 public DigestHelper sha1(InputStream is /*ByteBuffer*/) throws IOException {
95 MessageDigest alg = getSHA1();
96 byte[] buf = new byte[1024];
97 int c;
98 while ((c = is.read(buf)) != -1) {
99 alg.update(buf, 0, c);
100 }
101 digest = alg.digest();
102 return this;
103 }
104
105 public static String toHexString(byte[] data, final int offset, final int count) {
106 char[] result = new char[count << 1];
107 final String hexDigits = "0123456789abcdef";
108 final int end = offset+count;
109 for (int i = offset, j = 0; i < end; i++) {
110 result[j++] = hexDigits.charAt((data[i] >>> 4) & 0x0F);
111 result[j++] = hexDigits.charAt(data[i] & 0x0F);
112 }
113 return new String(result);
114 }
115 }