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