You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SHA1.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright (C) 2017, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util.sha1;
  44. import static java.lang.Integer.lowestOneBit;
  45. import static java.lang.Integer.numberOfTrailingZeros;
  46. import static java.lang.Integer.rotateLeft;
  47. import static java.lang.Integer.rotateRight;
  48. import java.util.Arrays;
  49. import org.eclipse.jgit.lib.MutableObjectId;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.util.NB;
  52. import org.eclipse.jgit.util.SystemReader;
  53. import org.slf4j.Logger;
  54. import org.slf4j.LoggerFactory;
  55. /**
  56. * Pure Java implementation of SHA-1 from FIPS 180-1 / RFC 3174.
  57. *
  58. * <p>
  59. * See <a href="https://tools.ietf.org/html/rfc3174">RFC 3174</a>.
  60. * <p>
  61. * Unlike MessageDigest, this implementation includes the algorithm used by
  62. * {@code sha1dc} to detect cryptanalytic collision attacks against SHA-1, such
  63. * as the one used by <a href="https://shattered.it/">SHAttered</a>. See
  64. * <a href="https://github.com/cr-marcstevens/sha1collisiondetection">
  65. * sha1collisiondetection</a> for more information.
  66. * <p>
  67. * When detectCollision is true (default), this implementation throws
  68. * {@link Sha1CollisionException} from any digest method if a potential
  69. * collision was detected.
  70. *
  71. * @since 4.7
  72. */
  73. public class SHA1 {
  74. private static Logger LOG = LoggerFactory.getLogger(SHA1.class);
  75. private static final boolean DETECT_COLLISIONS;
  76. static {
  77. SystemReader sr = SystemReader.getInstance();
  78. String v = sr.getProperty("org.eclipse.jgit.util.sha1.detectCollision"); //$NON-NLS-1$
  79. DETECT_COLLISIONS = v != null ? Boolean.parseBoolean(v) : true;
  80. }
  81. /** @return a new context to compute a SHA-1 hash of data. */
  82. public static SHA1 newInstance() {
  83. return new SHA1();
  84. }
  85. private final State h = new State();
  86. private final int[] w = new int[80];
  87. /** Buffer to accumulate partial blocks to 64 byte alignment. */
  88. private final byte[] buffer = new byte[64];
  89. /** Total number of bytes in the message. */
  90. private long length;
  91. private boolean detectCollision = DETECT_COLLISIONS;
  92. private boolean foundCollision;
  93. private final int[] w2 = new int[80];
  94. private final State state58 = new State();
  95. private final State state65 = new State();
  96. private final State hIn = new State();
  97. private final State hTmp = new State();
  98. private SHA1() {
  99. h.init();
  100. }
  101. /**
  102. * Enable likely collision detection.
  103. * <p>
  104. * Default is {@code true}.
  105. * <p>
  106. * May also be set by system property:
  107. * {@code -Dorg.eclipse.jgit.util.sha1.detectCollision=true}.
  108. *
  109. * @param detect
  110. * @return {@code this}
  111. */
  112. public SHA1 setDetectCollision(boolean detect) {
  113. detectCollision = detect;
  114. return this;
  115. }
  116. /**
  117. * Update the digest computation by adding a byte.
  118. *
  119. * @param b
  120. */
  121. public void update(byte b) {
  122. int bufferLen = (int) (length & 63);
  123. length++;
  124. buffer[bufferLen] = b;
  125. if (bufferLen == 63) {
  126. compress(buffer, 0);
  127. }
  128. }
  129. /**
  130. * Update the digest computation by adding bytes to the message.
  131. *
  132. * @param in
  133. * input array of bytes.
  134. */
  135. public void update(byte[] in) {
  136. update(in, 0, in.length);
  137. }
  138. /**
  139. * Update the digest computation by adding bytes to the message.
  140. *
  141. * @param in
  142. * input array of bytes.
  143. * @param p
  144. * offset to start at from {@code in}.
  145. * @param len
  146. * number of bytes to hash.
  147. */
  148. public void update(byte[] in, int p, int len) {
  149. // SHA-1 compress can only process whole 64 byte blocks.
  150. // Hold partial updates in buffer, whose length is the low bits.
  151. int bufferLen = (int) (length & 63);
  152. length += len;
  153. if (bufferLen > 0) {
  154. int n = Math.min(64 - bufferLen, len);
  155. System.arraycopy(in, p, buffer, bufferLen, n);
  156. p += n;
  157. len -= n;
  158. if (bufferLen + n < 64) {
  159. return;
  160. }
  161. compress(buffer, 0);
  162. }
  163. while (len >= 64) {
  164. compress(in, p);
  165. p += 64;
  166. len -= 64;
  167. }
  168. if (len > 0) {
  169. System.arraycopy(in, p, buffer, 0, len);
  170. }
  171. }
  172. private void compress(byte[] block, int p) {
  173. initBlock(block, p);
  174. int ubcDvMask = detectCollision ? UbcCheck.check(w) : 0;
  175. compress();
  176. while (ubcDvMask != 0) {
  177. int b = numberOfTrailingZeros(lowestOneBit(ubcDvMask));
  178. UbcCheck.DvInfo dv = UbcCheck.DV[b];
  179. for (int i = 0; i < 80; i++) {
  180. w2[i] = w[i] ^ dv.dm[i];
  181. }
  182. recompress(dv.testt);
  183. if (eq(hTmp, h)) {
  184. foundCollision = true;
  185. break;
  186. }
  187. ubcDvMask &= ~(1 << b);
  188. }
  189. }
  190. private void initBlock(byte[] block, int p) {
  191. for (int t = 0; t < 16; t++) {
  192. w[t] = NB.decodeInt32(block, p + (t << 2));
  193. }
  194. // RFC 3174 6.1.b, extend state vector to 80 words.
  195. for (int t = 16; t < 80; t++) {
  196. int x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  197. w[t] = rotateLeft(x, 1); // S^1(...)
  198. }
  199. }
  200. private void compress() {
  201. // Method 1 from RFC 3174 section 6.1.
  202. // Method 2 (circular queue of 16 words) is slower.
  203. int a = h.a, b = h.b, c = h.c, d = h.d, e = h.e;
  204. // @formatter:off
  205. e += s1(a, b, c, d,w[ 0]); b = rotateLeft( b, 30);
  206. d += s1(e, a, b, c,w[ 1]); a = rotateLeft( a, 30);
  207. c += s1(d, e, a, b,w[ 2]); e = rotateLeft( e, 30);
  208. b += s1(c, d, e, a,w[ 3]); d = rotateLeft( d, 30);
  209. a += s1(b, c, d, e,w[ 4]); c = rotateLeft( c, 30);
  210. e += s1(a, b, c, d,w[ 5]); b = rotateLeft( b, 30);
  211. d += s1(e, a, b, c,w[ 6]); a = rotateLeft( a, 30);
  212. c += s1(d, e, a, b,w[ 7]); e = rotateLeft( e, 30);
  213. b += s1(c, d, e, a,w[ 8]); d = rotateLeft( d, 30);
  214. a += s1(b, c, d, e,w[ 9]); c = rotateLeft( c, 30);
  215. e += s1(a, b, c, d,w[ 10]); b = rotateLeft( b, 30);
  216. d += s1(e, a, b, c,w[ 11]); a = rotateLeft( a, 30);
  217. c += s1(d, e, a, b,w[ 12]); e = rotateLeft( e, 30);
  218. b += s1(c, d, e, a,w[ 13]); d = rotateLeft( d, 30);
  219. a += s1(b, c, d, e,w[ 14]); c = rotateLeft( c, 30);
  220. e += s1(a, b, c, d,w[ 15]); b = rotateLeft( b, 30);
  221. d += s1(e, a, b, c,w[ 16]); a = rotateLeft( a, 30);
  222. c += s1(d, e, a, b,w[ 17]); e = rotateLeft( e, 30);
  223. b += s1(c, d, e, a,w[ 18]); d = rotateLeft( d, 30);
  224. a += s1(b, c, d, e,w[ 19]); c = rotateLeft( c, 30);
  225. e += s2(a, b, c, d,w[ 20]); b = rotateLeft( b, 30);
  226. d += s2(e, a, b, c,w[ 21]); a = rotateLeft( a, 30);
  227. c += s2(d, e, a, b,w[ 22]); e = rotateLeft( e, 30);
  228. b += s2(c, d, e, a,w[ 23]); d = rotateLeft( d, 30);
  229. a += s2(b, c, d, e,w[ 24]); c = rotateLeft( c, 30);
  230. e += s2(a, b, c, d,w[ 25]); b = rotateLeft( b, 30);
  231. d += s2(e, a, b, c,w[ 26]); a = rotateLeft( a, 30);
  232. c += s2(d, e, a, b,w[ 27]); e = rotateLeft( e, 30);
  233. b += s2(c, d, e, a,w[ 28]); d = rotateLeft( d, 30);
  234. a += s2(b, c, d, e,w[ 29]); c = rotateLeft( c, 30);
  235. e += s2(a, b, c, d,w[ 30]); b = rotateLeft( b, 30);
  236. d += s2(e, a, b, c,w[ 31]); a = rotateLeft( a, 30);
  237. c += s2(d, e, a, b,w[ 32]); e = rotateLeft( e, 30);
  238. b += s2(c, d, e, a,w[ 33]); d = rotateLeft( d, 30);
  239. a += s2(b, c, d, e,w[ 34]); c = rotateLeft( c, 30);
  240. e += s2(a, b, c, d,w[ 35]); b = rotateLeft( b, 30);
  241. d += s2(e, a, b, c,w[ 36]); a = rotateLeft( a, 30);
  242. c += s2(d, e, a, b,w[ 37]); e = rotateLeft( e, 30);
  243. b += s2(c, d, e, a,w[ 38]); d = rotateLeft( d, 30);
  244. a += s2(b, c, d, e,w[ 39]); c = rotateLeft( c, 30);
  245. e += s3(a, b, c, d,w[ 40]); b = rotateLeft( b, 30);
  246. d += s3(e, a, b, c,w[ 41]); a = rotateLeft( a, 30);
  247. c += s3(d, e, a, b,w[ 42]); e = rotateLeft( e, 30);
  248. b += s3(c, d, e, a,w[ 43]); d = rotateLeft( d, 30);
  249. a += s3(b, c, d, e,w[ 44]); c = rotateLeft( c, 30);
  250. e += s3(a, b, c, d,w[ 45]); b = rotateLeft( b, 30);
  251. d += s3(e, a, b, c,w[ 46]); a = rotateLeft( a, 30);
  252. c += s3(d, e, a, b,w[ 47]); e = rotateLeft( e, 30);
  253. b += s3(c, d, e, a,w[ 48]); d = rotateLeft( d, 30);
  254. a += s3(b, c, d, e,w[ 49]); c = rotateLeft( c, 30);
  255. e += s3(a, b, c, d,w[ 50]); b = rotateLeft( b, 30);
  256. d += s3(e, a, b, c,w[ 51]); a = rotateLeft( a, 30);
  257. c += s3(d, e, a, b,w[ 52]); e = rotateLeft( e, 30);
  258. b += s3(c, d, e, a,w[ 53]); d = rotateLeft( d, 30);
  259. a += s3(b, c, d, e,w[ 54]); c = rotateLeft( c, 30);
  260. e += s3(a, b, c, d,w[ 55]); b = rotateLeft( b, 30);
  261. d += s3(e, a, b, c,w[ 56]); a = rotateLeft( a, 30);
  262. c += s3(d, e, a, b,w[ 57]); e = rotateLeft( e, 30);
  263. state58.save(a, b, c, d, e);
  264. b += s3(c, d, e, a,w[ 58]); d = rotateLeft( d, 30);
  265. a += s3(b, c, d, e,w[ 59]); c = rotateLeft( c, 30);
  266. e += s4(a, b, c, d,w[ 60]); b = rotateLeft( b, 30);
  267. d += s4(e, a, b, c,w[ 61]); a = rotateLeft( a, 30);
  268. c += s4(d, e, a, b,w[ 62]); e = rotateLeft( e, 30);
  269. b += s4(c, d, e, a,w[ 63]); d = rotateLeft( d, 30);
  270. a += s4(b, c, d, e,w[ 64]); c = rotateLeft( c, 30);
  271. state65.save(a, b, c, d, e);
  272. e += s4(a, b, c, d,w[ 65]); b = rotateLeft( b, 30);
  273. d += s4(e, a, b, c,w[ 66]); a = rotateLeft( a, 30);
  274. c += s4(d, e, a, b,w[ 67]); e = rotateLeft( e, 30);
  275. b += s4(c, d, e, a,w[ 68]); d = rotateLeft( d, 30);
  276. a += s4(b, c, d, e,w[ 69]); c = rotateLeft( c, 30);
  277. e += s4(a, b, c, d,w[ 70]); b = rotateLeft( b, 30);
  278. d += s4(e, a, b, c,w[ 71]); a = rotateLeft( a, 30);
  279. c += s4(d, e, a, b,w[ 72]); e = rotateLeft( e, 30);
  280. b += s4(c, d, e, a,w[ 73]); d = rotateLeft( d, 30);
  281. a += s4(b, c, d, e,w[ 74]); c = rotateLeft( c, 30);
  282. e += s4(a, b, c, d,w[ 75]); b = rotateLeft( b, 30);
  283. d += s4(e, a, b, c,w[ 76]); a = rotateLeft( a, 30);
  284. c += s4(d, e, a, b,w[ 77]); e = rotateLeft( e, 30);
  285. b += s4(c, d, e, a,w[ 78]); d = rotateLeft( d, 30);
  286. a += s4(b, c, d, e,w[ 79]); c = rotateLeft( c, 30);
  287. // @formatter:on
  288. h.save(h.a + a, h.b + b, h.c + c, h.d + d, h.e + e);
  289. }
  290. private void recompress(int t) {
  291. State s;
  292. if (t == 58) {
  293. s = state58;
  294. } else if (t == 65) {
  295. s = state65;
  296. } else {
  297. throw new IllegalStateException();
  298. }
  299. int a = s.a, b = s.b, c = s.c, d = s.d, e = s.e;
  300. // @formatter:off
  301. if (t == 65) {
  302. { c = rotateRight( c, 30); a -= s4(b, c, d, e,w2[ 64]);}
  303. { d = rotateRight( d, 30); b -= s4(c, d, e, a,w2[ 63]);}
  304. { e = rotateRight( e, 30); c -= s4(d, e, a, b,w2[ 62]);}
  305. { a = rotateRight( a, 30); d -= s4(e, a, b, c,w2[ 61]);}
  306. { b = rotateRight( b, 30); e -= s4(a, b, c, d,w2[ 60]);}
  307. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 59]);}
  308. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 58]);}
  309. }
  310. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 57]);}
  311. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 56]);}
  312. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 55]);}
  313. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 54]);}
  314. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 53]);}
  315. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 52]);}
  316. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 51]);}
  317. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 50]);}
  318. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 49]);}
  319. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 48]);}
  320. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 47]);}
  321. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 46]);}
  322. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 45]);}
  323. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 44]);}
  324. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 43]);}
  325. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 42]);}
  326. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 41]);}
  327. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 40]);}
  328. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 39]);}
  329. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 38]);}
  330. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 37]);}
  331. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 36]);}
  332. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 35]);}
  333. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 34]);}
  334. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 33]);}
  335. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 32]);}
  336. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 31]);}
  337. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 30]);}
  338. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 29]);}
  339. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 28]);}
  340. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 27]);}
  341. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 26]);}
  342. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 25]);}
  343. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 24]);}
  344. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 23]);}
  345. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 22]);}
  346. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 21]);}
  347. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 20]);}
  348. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 19]);}
  349. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 18]);}
  350. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 17]);}
  351. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 16]);}
  352. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 15]);}
  353. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 14]);}
  354. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 13]);}
  355. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 12]);}
  356. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 11]);}
  357. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 10]);}
  358. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 9]);}
  359. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 8]);}
  360. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 7]);}
  361. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 6]);}
  362. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 5]);}
  363. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 4]);}
  364. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 3]);}
  365. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 2]);}
  366. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 1]);}
  367. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 0]);}
  368. hIn.save(a, b, c, d, e);
  369. a = s.a; b = s.b; c = s.c; d = s.d; e = s.e;
  370. if (t == 58) {
  371. { b += s3(c, d, e, a,w2[ 58]); d = rotateLeft( d, 30);}
  372. { a += s3(b, c, d, e,w2[ 59]); c = rotateLeft( c, 30);}
  373. { e += s4(a, b, c, d,w2[ 60]); b = rotateLeft( b, 30);}
  374. { d += s4(e, a, b, c,w2[ 61]); a = rotateLeft( a, 30);}
  375. { c += s4(d, e, a, b,w2[ 62]); e = rotateLeft( e, 30);}
  376. { b += s4(c, d, e, a,w2[ 63]); d = rotateLeft( d, 30);}
  377. { a += s4(b, c, d, e,w2[ 64]); c = rotateLeft( c, 30);}
  378. }
  379. { e += s4(a, b, c, d,w2[ 65]); b = rotateLeft( b, 30);}
  380. { d += s4(e, a, b, c,w2[ 66]); a = rotateLeft( a, 30);}
  381. { c += s4(d, e, a, b,w2[ 67]); e = rotateLeft( e, 30);}
  382. { b += s4(c, d, e, a,w2[ 68]); d = rotateLeft( d, 30);}
  383. { a += s4(b, c, d, e,w2[ 69]); c = rotateLeft( c, 30);}
  384. { e += s4(a, b, c, d,w2[ 70]); b = rotateLeft( b, 30);}
  385. { d += s4(e, a, b, c,w2[ 71]); a = rotateLeft( a, 30);}
  386. { c += s4(d, e, a, b,w2[ 72]); e = rotateLeft( e, 30);}
  387. { b += s4(c, d, e, a,w2[ 73]); d = rotateLeft( d, 30);}
  388. { a += s4(b, c, d, e,w2[ 74]); c = rotateLeft( c, 30);}
  389. { e += s4(a, b, c, d,w2[ 75]); b = rotateLeft( b, 30);}
  390. { d += s4(e, a, b, c,w2[ 76]); a = rotateLeft( a, 30);}
  391. { c += s4(d, e, a, b,w2[ 77]); e = rotateLeft( e, 30);}
  392. { b += s4(c, d, e, a,w2[ 78]); d = rotateLeft( d, 30);}
  393. { a += s4(b, c, d, e,w2[ 79]); c = rotateLeft( c, 30);}
  394. // @formatter:on
  395. hTmp.save(hIn.a + a, hIn.b + b, hIn.c + c, hIn.d + d, hIn.e + e);
  396. }
  397. private static int s1(int a, int b, int c, int d, int w_t) {
  398. return rotateLeft(a, 5)
  399. // f: 0 <= t <= 19
  400. + ((b & c) | ((~b) & d))
  401. + 0x5A827999 + w_t;
  402. }
  403. private static int s2(int a, int b, int c, int d, int w_t) {
  404. return rotateLeft(a, 5)
  405. // f: 20 <= t <= 39
  406. + (b ^ c ^ d)
  407. + 0x6ED9EBA1 + w_t;
  408. }
  409. private static int s3(int a, int b, int c, int d, int w_t) {
  410. return rotateLeft(a, 5)
  411. // f: 40 <= t <= 59
  412. + ((b & c) | (b & d) | (c & d))
  413. + 0x8F1BBCDC + w_t;
  414. }
  415. private static int s4(int a, int b, int c, int d, int w_t) {
  416. return rotateLeft(a, 5)
  417. // f: 60 <= t <= 79
  418. + (b ^ c ^ d)
  419. + 0xCA62C1D6 + w_t;
  420. }
  421. private static boolean eq(State q, State r) {
  422. return q.a == r.a
  423. && q.b == r.b
  424. && q.c == r.c
  425. && q.d == r.d
  426. && q.e == r.e;
  427. }
  428. private void finish() {
  429. int bufferLen = (int) (length & 63);
  430. if (bufferLen > 55) {
  431. // Last block is too small; pad, compress, pad another block.
  432. buffer[bufferLen++] = (byte) 0x80;
  433. Arrays.fill(buffer, bufferLen, 64, (byte) 0);
  434. compress(buffer, 0);
  435. Arrays.fill(buffer, 0, 56, (byte) 0);
  436. } else {
  437. // Last block can hold padding and length.
  438. buffer[bufferLen++] = (byte) 0x80;
  439. Arrays.fill(buffer, bufferLen, 56, (byte) 0);
  440. }
  441. // SHA-1 appends the length of the message in bits after the
  442. // padding block (above). Here length is in bytes. Multiply by
  443. // 8 by shifting by 3 as part of storing the 64 bit byte length
  444. // into the two words expected in the trailer.
  445. NB.encodeInt32(buffer, 56, (int) (length >>> (32 - 3)));
  446. NB.encodeInt32(buffer, 60, (int) (length << 3));
  447. compress(buffer, 0);
  448. if (foundCollision) {
  449. ObjectId id = h.toObjectId();
  450. LOG.warn("possible SHA-1 collision " + id.name()); //$NON-NLS-1$
  451. throw new Sha1CollisionException(id);
  452. }
  453. }
  454. /**
  455. * Finish the digest and return the resulting hash.
  456. * <p>
  457. * Once {@code digest()} is called, this instance should be discarded.
  458. *
  459. * @return the bytes for the resulting hash.
  460. * @throws Sha1CollisionException
  461. * if a collision was detected and safeHash is false.
  462. */
  463. public byte[] digest() throws Sha1CollisionException {
  464. finish();
  465. byte[] b = new byte[20];
  466. NB.encodeInt32(b, 0, h.a);
  467. NB.encodeInt32(b, 4, h.b);
  468. NB.encodeInt32(b, 8, h.c);
  469. NB.encodeInt32(b, 12, h.d);
  470. NB.encodeInt32(b, 16, h.e);
  471. return b;
  472. }
  473. /**
  474. * Finish the digest and return the resulting hash.
  475. * <p>
  476. * Once {@code digest()} is called, this instance should be discarded.
  477. *
  478. * @return the ObjectId for the resulting hash.
  479. * @throws Sha1CollisionException
  480. * if a collision was detected and safeHash is false.
  481. */
  482. public ObjectId toObjectId() throws Sha1CollisionException {
  483. finish();
  484. return h.toObjectId();
  485. }
  486. /**
  487. * Finish the digest and return the resulting hash.
  488. * <p>
  489. * Once {@code digest()} is called, this instance should be discarded.
  490. *
  491. * @param id
  492. * destination to copy the digest to.
  493. * @throws Sha1CollisionException
  494. * if a collision was detected and safeHash is false.
  495. */
  496. public void digest(MutableObjectId id) throws Sha1CollisionException {
  497. finish();
  498. id.set(h.a, h.b, h.c, h.d, h.e);
  499. }
  500. /**
  501. * Check if a collision was detected.
  502. *
  503. * <p>
  504. * This method only returns an accurate result after the digest was obtained
  505. * through {@link #digest()}, {@link #digest(MutableObjectId)} or
  506. * {@link #toObjectId()}, as the hashing function must finish processing to
  507. * know the final state.
  508. *
  509. * @return {@code true} if a likely collision was detected.
  510. */
  511. public boolean hasCollision() {
  512. return foundCollision;
  513. }
  514. /**
  515. * Reset this instance to compute another hash.
  516. *
  517. * @return {@code this}.
  518. */
  519. public SHA1 reset() {
  520. h.init();
  521. length = 0;
  522. foundCollision = false;
  523. return this;
  524. }
  525. private static final class State {
  526. int a;
  527. int b;
  528. int c;
  529. int d;
  530. int e;
  531. final void init() {
  532. // Magic initialization constants defined by FIPS180.
  533. save(0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0);
  534. }
  535. final void save(int a1, int b1, int c1, int d1, int e1) {
  536. a = a1;
  537. b = b1;
  538. c = c1;
  539. d = d1;
  540. e = e1;
  541. }
  542. ObjectId toObjectId() {
  543. return new ObjectId(a, b, c, d, e);
  544. }
  545. }
  546. }