Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SHA1.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 org.eclipse.jgit.util.sha1.Sha1CollisionException} from any digest
  69. * method if a potential 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. /**
  82. * Create a new context to compute a SHA-1 hash of data.
  83. *
  84. * @return a new context to compute a SHA-1 hash of data.
  85. */
  86. public static SHA1 newInstance() {
  87. return new SHA1();
  88. }
  89. private final State h = new State();
  90. private final int[] w = new int[80];
  91. /** Buffer to accumulate partial blocks to 64 byte alignment. */
  92. private final byte[] buffer = new byte[64];
  93. /** Total number of bytes in the message. */
  94. private long length;
  95. private boolean detectCollision = DETECT_COLLISIONS;
  96. private boolean foundCollision;
  97. private final int[] w2 = new int[80];
  98. private final State state58 = new State();
  99. private final State state65 = new State();
  100. private final State hIn = new State();
  101. private final State hTmp = new State();
  102. private SHA1() {
  103. h.init();
  104. }
  105. /**
  106. * Enable likely collision detection.
  107. * <p>
  108. * Default is {@code true}.
  109. * <p>
  110. * May also be set by system property:
  111. * {@code -Dorg.eclipse.jgit.util.sha1.detectCollision=true}.
  112. *
  113. * @param detect
  114. * a boolean.
  115. * @return {@code this}
  116. */
  117. public SHA1 setDetectCollision(boolean detect) {
  118. detectCollision = detect;
  119. return this;
  120. }
  121. /**
  122. * Update the digest computation by adding a byte.
  123. *
  124. * @param b a byte.
  125. */
  126. public void update(byte b) {
  127. int bufferLen = (int) (length & 63);
  128. length++;
  129. buffer[bufferLen] = b;
  130. if (bufferLen == 63) {
  131. compress(buffer, 0);
  132. }
  133. }
  134. /**
  135. * Update the digest computation by adding bytes to the message.
  136. *
  137. * @param in
  138. * input array of bytes.
  139. */
  140. public void update(byte[] in) {
  141. update(in, 0, in.length);
  142. }
  143. /**
  144. * Update the digest computation by adding bytes to the message.
  145. *
  146. * @param in
  147. * input array of bytes.
  148. * @param p
  149. * offset to start at from {@code in}.
  150. * @param len
  151. * number of bytes to hash.
  152. */
  153. public void update(byte[] in, int p, int len) {
  154. // SHA-1 compress can only process whole 64 byte blocks.
  155. // Hold partial updates in buffer, whose length is the low bits.
  156. int bufferLen = (int) (length & 63);
  157. length += len;
  158. if (bufferLen > 0) {
  159. int n = Math.min(64 - bufferLen, len);
  160. System.arraycopy(in, p, buffer, bufferLen, n);
  161. p += n;
  162. len -= n;
  163. if (bufferLen + n < 64) {
  164. return;
  165. }
  166. compress(buffer, 0);
  167. }
  168. while (len >= 64) {
  169. compress(in, p);
  170. p += 64;
  171. len -= 64;
  172. }
  173. if (len > 0) {
  174. System.arraycopy(in, p, buffer, 0, len);
  175. }
  176. }
  177. private void compress(byte[] block, int p) {
  178. initBlock(block, p);
  179. int ubcDvMask = detectCollision ? UbcCheck.check(w) : 0;
  180. compress();
  181. while (ubcDvMask != 0) {
  182. int b = numberOfTrailingZeros(lowestOneBit(ubcDvMask));
  183. UbcCheck.DvInfo dv = UbcCheck.DV[b];
  184. for (int i = 0; i < 80; i++) {
  185. w2[i] = w[i] ^ dv.dm[i];
  186. }
  187. recompress(dv.testt);
  188. if (eq(hTmp, h)) {
  189. foundCollision = true;
  190. break;
  191. }
  192. ubcDvMask &= ~(1 << b);
  193. }
  194. }
  195. private void initBlock(byte[] block, int p) {
  196. for (int t = 0; t < 16; t++) {
  197. w[t] = NB.decodeInt32(block, p + (t << 2));
  198. }
  199. // RFC 3174 6.1.b, extend state vector to 80 words.
  200. for (int t = 16; t < 80; t++) {
  201. int x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  202. w[t] = rotateLeft(x, 1); // S^1(...)
  203. }
  204. }
  205. private void compress() {
  206. // Method 1 from RFC 3174 section 6.1.
  207. // Method 2 (circular queue of 16 words) is slower.
  208. int a = h.a, b = h.b, c = h.c, d = h.d, e = h.e;
  209. // @formatter:off
  210. e += s1(a, b, c, d,w[ 0]); b = rotateLeft( b, 30);
  211. d += s1(e, a, b, c,w[ 1]); a = rotateLeft( a, 30);
  212. c += s1(d, e, a, b,w[ 2]); e = rotateLeft( e, 30);
  213. b += s1(c, d, e, a,w[ 3]); d = rotateLeft( d, 30);
  214. a += s1(b, c, d, e,w[ 4]); c = rotateLeft( c, 30);
  215. e += s1(a, b, c, d,w[ 5]); b = rotateLeft( b, 30);
  216. d += s1(e, a, b, c,w[ 6]); a = rotateLeft( a, 30);
  217. c += s1(d, e, a, b,w[ 7]); e = rotateLeft( e, 30);
  218. b += s1(c, d, e, a,w[ 8]); d = rotateLeft( d, 30);
  219. a += s1(b, c, d, e,w[ 9]); c = rotateLeft( c, 30);
  220. e += s1(a, b, c, d,w[ 10]); b = rotateLeft( b, 30);
  221. d += s1(e, a, b, c,w[ 11]); a = rotateLeft( a, 30);
  222. c += s1(d, e, a, b,w[ 12]); e = rotateLeft( e, 30);
  223. b += s1(c, d, e, a,w[ 13]); d = rotateLeft( d, 30);
  224. a += s1(b, c, d, e,w[ 14]); c = rotateLeft( c, 30);
  225. e += s1(a, b, c, d,w[ 15]); b = rotateLeft( b, 30);
  226. d += s1(e, a, b, c,w[ 16]); a = rotateLeft( a, 30);
  227. c += s1(d, e, a, b,w[ 17]); e = rotateLeft( e, 30);
  228. b += s1(c, d, e, a,w[ 18]); d = rotateLeft( d, 30);
  229. a += s1(b, c, d, e,w[ 19]); c = rotateLeft( c, 30);
  230. e += s2(a, b, c, d,w[ 20]); b = rotateLeft( b, 30);
  231. d += s2(e, a, b, c,w[ 21]); a = rotateLeft( a, 30);
  232. c += s2(d, e, a, b,w[ 22]); e = rotateLeft( e, 30);
  233. b += s2(c, d, e, a,w[ 23]); d = rotateLeft( d, 30);
  234. a += s2(b, c, d, e,w[ 24]); c = rotateLeft( c, 30);
  235. e += s2(a, b, c, d,w[ 25]); b = rotateLeft( b, 30);
  236. d += s2(e, a, b, c,w[ 26]); a = rotateLeft( a, 30);
  237. c += s2(d, e, a, b,w[ 27]); e = rotateLeft( e, 30);
  238. b += s2(c, d, e, a,w[ 28]); d = rotateLeft( d, 30);
  239. a += s2(b, c, d, e,w[ 29]); c = rotateLeft( c, 30);
  240. e += s2(a, b, c, d,w[ 30]); b = rotateLeft( b, 30);
  241. d += s2(e, a, b, c,w[ 31]); a = rotateLeft( a, 30);
  242. c += s2(d, e, a, b,w[ 32]); e = rotateLeft( e, 30);
  243. b += s2(c, d, e, a,w[ 33]); d = rotateLeft( d, 30);
  244. a += s2(b, c, d, e,w[ 34]); c = rotateLeft( c, 30);
  245. e += s2(a, b, c, d,w[ 35]); b = rotateLeft( b, 30);
  246. d += s2(e, a, b, c,w[ 36]); a = rotateLeft( a, 30);
  247. c += s2(d, e, a, b,w[ 37]); e = rotateLeft( e, 30);
  248. b += s2(c, d, e, a,w[ 38]); d = rotateLeft( d, 30);
  249. a += s2(b, c, d, e,w[ 39]); c = rotateLeft( c, 30);
  250. e += s3(a, b, c, d,w[ 40]); b = rotateLeft( b, 30);
  251. d += s3(e, a, b, c,w[ 41]); a = rotateLeft( a, 30);
  252. c += s3(d, e, a, b,w[ 42]); e = rotateLeft( e, 30);
  253. b += s3(c, d, e, a,w[ 43]); d = rotateLeft( d, 30);
  254. a += s3(b, c, d, e,w[ 44]); c = rotateLeft( c, 30);
  255. e += s3(a, b, c, d,w[ 45]); b = rotateLeft( b, 30);
  256. d += s3(e, a, b, c,w[ 46]); a = rotateLeft( a, 30);
  257. c += s3(d, e, a, b,w[ 47]); e = rotateLeft( e, 30);
  258. b += s3(c, d, e, a,w[ 48]); d = rotateLeft( d, 30);
  259. a += s3(b, c, d, e,w[ 49]); c = rotateLeft( c, 30);
  260. e += s3(a, b, c, d,w[ 50]); b = rotateLeft( b, 30);
  261. d += s3(e, a, b, c,w[ 51]); a = rotateLeft( a, 30);
  262. c += s3(d, e, a, b,w[ 52]); e = rotateLeft( e, 30);
  263. b += s3(c, d, e, a,w[ 53]); d = rotateLeft( d, 30);
  264. a += s3(b, c, d, e,w[ 54]); c = rotateLeft( c, 30);
  265. e += s3(a, b, c, d,w[ 55]); b = rotateLeft( b, 30);
  266. d += s3(e, a, b, c,w[ 56]); a = rotateLeft( a, 30);
  267. c += s3(d, e, a, b,w[ 57]); e = rotateLeft( e, 30);
  268. state58.save(a, b, c, d, e);
  269. b += s3(c, d, e, a,w[ 58]); d = rotateLeft( d, 30);
  270. a += s3(b, c, d, e,w[ 59]); c = rotateLeft( c, 30);
  271. e += s4(a, b, c, d,w[ 60]); b = rotateLeft( b, 30);
  272. d += s4(e, a, b, c,w[ 61]); a = rotateLeft( a, 30);
  273. c += s4(d, e, a, b,w[ 62]); e = rotateLeft( e, 30);
  274. b += s4(c, d, e, a,w[ 63]); d = rotateLeft( d, 30);
  275. a += s4(b, c, d, e,w[ 64]); c = rotateLeft( c, 30);
  276. state65.save(a, b, c, d, e);
  277. e += s4(a, b, c, d,w[ 65]); b = rotateLeft( b, 30);
  278. d += s4(e, a, b, c,w[ 66]); a = rotateLeft( a, 30);
  279. c += s4(d, e, a, b,w[ 67]); e = rotateLeft( e, 30);
  280. b += s4(c, d, e, a,w[ 68]); d = rotateLeft( d, 30);
  281. a += s4(b, c, d, e,w[ 69]); c = rotateLeft( c, 30);
  282. e += s4(a, b, c, d,w[ 70]); b = rotateLeft( b, 30);
  283. d += s4(e, a, b, c,w[ 71]); a = rotateLeft( a, 30);
  284. c += s4(d, e, a, b,w[ 72]); e = rotateLeft( e, 30);
  285. b += s4(c, d, e, a,w[ 73]); d = rotateLeft( d, 30);
  286. a += s4(b, c, d, e,w[ 74]); c = rotateLeft( c, 30);
  287. e += s4(a, b, c, d,w[ 75]); b = rotateLeft( b, 30);
  288. d += s4(e, a, b, c,w[ 76]); a = rotateLeft( a, 30);
  289. c += s4(d, e, a, b,w[ 77]); e = rotateLeft( e, 30);
  290. b += s4(c, d, e, a,w[ 78]); d = rotateLeft( d, 30);
  291. a += s4(b, c, d, e,w[ 79]); c = rotateLeft( c, 30);
  292. // @formatter:on
  293. h.save(h.a + a, h.b + b, h.c + c, h.d + d, h.e + e);
  294. }
  295. private void recompress(int t) {
  296. State s;
  297. if (t == 58) {
  298. s = state58;
  299. } else if (t == 65) {
  300. s = state65;
  301. } else {
  302. throw new IllegalStateException();
  303. }
  304. int a = s.a, b = s.b, c = s.c, d = s.d, e = s.e;
  305. // @formatter:off
  306. if (t == 65) {
  307. { c = rotateRight( c, 30); a -= s4(b, c, d, e,w2[ 64]);}
  308. { d = rotateRight( d, 30); b -= s4(c, d, e, a,w2[ 63]);}
  309. { e = rotateRight( e, 30); c -= s4(d, e, a, b,w2[ 62]);}
  310. { a = rotateRight( a, 30); d -= s4(e, a, b, c,w2[ 61]);}
  311. { b = rotateRight( b, 30); e -= s4(a, b, c, d,w2[ 60]);}
  312. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 59]);}
  313. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 58]);}
  314. }
  315. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 57]);}
  316. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 56]);}
  317. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 55]);}
  318. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 54]);}
  319. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 53]);}
  320. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 52]);}
  321. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 51]);}
  322. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 50]);}
  323. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 49]);}
  324. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 48]);}
  325. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 47]);}
  326. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 46]);}
  327. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 45]);}
  328. { c = rotateRight( c, 30); a -= s3(b, c, d, e,w2[ 44]);}
  329. { d = rotateRight( d, 30); b -= s3(c, d, e, a,w2[ 43]);}
  330. { e = rotateRight( e, 30); c -= s3(d, e, a, b,w2[ 42]);}
  331. { a = rotateRight( a, 30); d -= s3(e, a, b, c,w2[ 41]);}
  332. { b = rotateRight( b, 30); e -= s3(a, b, c, d,w2[ 40]);}
  333. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 39]);}
  334. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 38]);}
  335. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 37]);}
  336. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 36]);}
  337. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 35]);}
  338. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 34]);}
  339. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 33]);}
  340. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 32]);}
  341. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 31]);}
  342. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 30]);}
  343. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 29]);}
  344. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 28]);}
  345. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 27]);}
  346. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 26]);}
  347. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 25]);}
  348. { c = rotateRight( c, 30); a -= s2(b, c, d, e,w2[ 24]);}
  349. { d = rotateRight( d, 30); b -= s2(c, d, e, a,w2[ 23]);}
  350. { e = rotateRight( e, 30); c -= s2(d, e, a, b,w2[ 22]);}
  351. { a = rotateRight( a, 30); d -= s2(e, a, b, c,w2[ 21]);}
  352. { b = rotateRight( b, 30); e -= s2(a, b, c, d,w2[ 20]);}
  353. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 19]);}
  354. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 18]);}
  355. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 17]);}
  356. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 16]);}
  357. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 15]);}
  358. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 14]);}
  359. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 13]);}
  360. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 12]);}
  361. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 11]);}
  362. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 10]);}
  363. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 9]);}
  364. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 8]);}
  365. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 7]);}
  366. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 6]);}
  367. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 5]);}
  368. { c = rotateRight( c, 30); a -= s1(b, c, d, e,w2[ 4]);}
  369. { d = rotateRight( d, 30); b -= s1(c, d, e, a,w2[ 3]);}
  370. { e = rotateRight( e, 30); c -= s1(d, e, a, b,w2[ 2]);}
  371. { a = rotateRight( a, 30); d -= s1(e, a, b, c,w2[ 1]);}
  372. { b = rotateRight( b, 30); e -= s1(a, b, c, d,w2[ 0]);}
  373. hIn.save(a, b, c, d, e);
  374. a = s.a; b = s.b; c = s.c; d = s.d; e = s.e;
  375. if (t == 58) {
  376. { b += s3(c, d, e, a,w2[ 58]); d = rotateLeft( d, 30);}
  377. { a += s3(b, c, d, e,w2[ 59]); c = rotateLeft( c, 30);}
  378. { e += s4(a, b, c, d,w2[ 60]); b = rotateLeft( b, 30);}
  379. { d += s4(e, a, b, c,w2[ 61]); a = rotateLeft( a, 30);}
  380. { c += s4(d, e, a, b,w2[ 62]); e = rotateLeft( e, 30);}
  381. { b += s4(c, d, e, a,w2[ 63]); d = rotateLeft( d, 30);}
  382. { a += s4(b, c, d, e,w2[ 64]); c = rotateLeft( c, 30);}
  383. }
  384. { e += s4(a, b, c, d,w2[ 65]); b = rotateLeft( b, 30);}
  385. { d += s4(e, a, b, c,w2[ 66]); a = rotateLeft( a, 30);}
  386. { c += s4(d, e, a, b,w2[ 67]); e = rotateLeft( e, 30);}
  387. { b += s4(c, d, e, a,w2[ 68]); d = rotateLeft( d, 30);}
  388. { a += s4(b, c, d, e,w2[ 69]); c = rotateLeft( c, 30);}
  389. { e += s4(a, b, c, d,w2[ 70]); b = rotateLeft( b, 30);}
  390. { d += s4(e, a, b, c,w2[ 71]); a = rotateLeft( a, 30);}
  391. { c += s4(d, e, a, b,w2[ 72]); e = rotateLeft( e, 30);}
  392. { b += s4(c, d, e, a,w2[ 73]); d = rotateLeft( d, 30);}
  393. { a += s4(b, c, d, e,w2[ 74]); c = rotateLeft( c, 30);}
  394. { e += s4(a, b, c, d,w2[ 75]); b = rotateLeft( b, 30);}
  395. { d += s4(e, a, b, c,w2[ 76]); a = rotateLeft( a, 30);}
  396. { c += s4(d, e, a, b,w2[ 77]); e = rotateLeft( e, 30);}
  397. { b += s4(c, d, e, a,w2[ 78]); d = rotateLeft( d, 30);}
  398. { a += s4(b, c, d, e,w2[ 79]); c = rotateLeft( c, 30);}
  399. // @formatter:on
  400. hTmp.save(hIn.a + a, hIn.b + b, hIn.c + c, hIn.d + d, hIn.e + e);
  401. }
  402. private static int s1(int a, int b, int c, int d, int w_t) {
  403. return rotateLeft(a, 5)
  404. // f: 0 <= t <= 19
  405. + ((b & c) | ((~b) & d))
  406. + 0x5A827999 + w_t;
  407. }
  408. private static int s2(int a, int b, int c, int d, int w_t) {
  409. return rotateLeft(a, 5)
  410. // f: 20 <= t <= 39
  411. + (b ^ c ^ d)
  412. + 0x6ED9EBA1 + w_t;
  413. }
  414. private static int s3(int a, int b, int c, int d, int w_t) {
  415. return rotateLeft(a, 5)
  416. // f: 40 <= t <= 59
  417. + ((b & c) | (b & d) | (c & d))
  418. + 0x8F1BBCDC + w_t;
  419. }
  420. private static int s4(int a, int b, int c, int d, int w_t) {
  421. return rotateLeft(a, 5)
  422. // f: 60 <= t <= 79
  423. + (b ^ c ^ d)
  424. + 0xCA62C1D6 + w_t;
  425. }
  426. private static boolean eq(State q, State r) {
  427. return q.a == r.a
  428. && q.b == r.b
  429. && q.c == r.c
  430. && q.d == r.d
  431. && q.e == r.e;
  432. }
  433. private void finish() {
  434. int bufferLen = (int) (length & 63);
  435. if (bufferLen > 55) {
  436. // Last block is too small; pad, compress, pad another block.
  437. buffer[bufferLen++] = (byte) 0x80;
  438. Arrays.fill(buffer, bufferLen, 64, (byte) 0);
  439. compress(buffer, 0);
  440. Arrays.fill(buffer, 0, 56, (byte) 0);
  441. } else {
  442. // Last block can hold padding and length.
  443. buffer[bufferLen++] = (byte) 0x80;
  444. Arrays.fill(buffer, bufferLen, 56, (byte) 0);
  445. }
  446. // SHA-1 appends the length of the message in bits after the
  447. // padding block (above). Here length is in bytes. Multiply by
  448. // 8 by shifting by 3 as part of storing the 64 bit byte length
  449. // into the two words expected in the trailer.
  450. NB.encodeInt32(buffer, 56, (int) (length >>> (32 - 3)));
  451. NB.encodeInt32(buffer, 60, (int) (length << 3));
  452. compress(buffer, 0);
  453. if (foundCollision) {
  454. ObjectId id = h.toObjectId();
  455. LOG.warn("possible SHA-1 collision " + id.name()); //$NON-NLS-1$
  456. throw new Sha1CollisionException(id);
  457. }
  458. }
  459. /**
  460. * Finish the digest and return the resulting hash.
  461. * <p>
  462. * Once {@code digest()} is called, this instance should be discarded.
  463. *
  464. * @return the bytes for the resulting hash.
  465. * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  466. * if a collision was detected and safeHash is false.
  467. */
  468. public byte[] digest() throws Sha1CollisionException {
  469. finish();
  470. byte[] b = new byte[20];
  471. NB.encodeInt32(b, 0, h.a);
  472. NB.encodeInt32(b, 4, h.b);
  473. NB.encodeInt32(b, 8, h.c);
  474. NB.encodeInt32(b, 12, h.d);
  475. NB.encodeInt32(b, 16, h.e);
  476. return b;
  477. }
  478. /**
  479. * Finish the digest and return the resulting hash.
  480. * <p>
  481. * Once {@code digest()} is called, this instance should be discarded.
  482. *
  483. * @return the ObjectId for the resulting hash.
  484. * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  485. * if a collision was detected and safeHash is false.
  486. */
  487. public ObjectId toObjectId() throws Sha1CollisionException {
  488. finish();
  489. return h.toObjectId();
  490. }
  491. /**
  492. * Finish the digest and return the resulting hash.
  493. * <p>
  494. * Once {@code digest()} is called, this instance should be discarded.
  495. *
  496. * @param id
  497. * destination to copy the digest to.
  498. * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  499. * if a collision was detected and safeHash is false.
  500. */
  501. public void digest(MutableObjectId id) throws Sha1CollisionException {
  502. finish();
  503. id.set(h.a, h.b, h.c, h.d, h.e);
  504. }
  505. /**
  506. * Check if a collision was detected.
  507. *
  508. * <p>
  509. * This method only returns an accurate result after the digest was obtained
  510. * through {@link #digest()}, {@link #digest(MutableObjectId)} or
  511. * {@link #toObjectId()}, as the hashing function must finish processing to
  512. * know the final state.
  513. *
  514. * @return {@code true} if a likely collision was detected.
  515. */
  516. public boolean hasCollision() {
  517. return foundCollision;
  518. }
  519. /**
  520. * Reset this instance to compute another hash.
  521. *
  522. * @return {@code this}.
  523. */
  524. public SHA1 reset() {
  525. h.init();
  526. length = 0;
  527. foundCollision = false;
  528. return this;
  529. }
  530. private static final class State {
  531. int a;
  532. int b;
  533. int c;
  534. int d;
  535. int e;
  536. final void init() {
  537. // Magic initialization constants defined by FIPS180.
  538. save(0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0);
  539. }
  540. final void save(int a1, int b1, int c1, int d1, int e1) {
  541. a = a1;
  542. b = b1;
  543. c = c1;
  544. d = d1;
  545. e = e1;
  546. }
  547. ObjectId toObjectId() {
  548. return new ObjectId(a, b, c, d, e);
  549. }
  550. }
  551. }