SHA1.java

  1. /*
  2.  * Copyright (C) 2017, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.util.sha1;

  11. import static java.lang.Integer.lowestOneBit;
  12. import static java.lang.Integer.numberOfTrailingZeros;
  13. import static java.lang.Integer.rotateLeft;
  14. import static java.lang.Integer.rotateRight;

  15. import java.text.MessageFormat;
  16. import java.util.Arrays;

  17. import org.eclipse.jgit.internal.JGitText;
  18. import org.eclipse.jgit.lib.MutableObjectId;
  19. import org.eclipse.jgit.lib.ObjectId;
  20. import org.eclipse.jgit.util.NB;
  21. import org.eclipse.jgit.util.SystemReader;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. /**
  25.  * Pure Java implementation of SHA-1 from FIPS 180-1 / RFC 3174.
  26.  *
  27.  * <p>
  28.  * See <a href="https://tools.ietf.org/html/rfc3174">RFC 3174</a>.
  29.  * <p>
  30.  * Unlike MessageDigest, this implementation includes the algorithm used by
  31.  * {@code sha1dc} to detect cryptanalytic collision attacks against SHA-1, such
  32.  * as the one used by <a href="https://shattered.it/">SHAttered</a>. See
  33.  * <a href="https://github.com/cr-marcstevens/sha1collisiondetection">
  34.  * sha1collisiondetection</a> for more information.
  35.  * <p>
  36.  * When detectCollision is true (default), this implementation throws
  37.  * {@link org.eclipse.jgit.util.sha1.Sha1CollisionException} from any digest
  38.  * method if a potential collision was detected.
  39.  *
  40.  * @since 4.7
  41.  */
  42. public class SHA1 {
  43.     private static final Logger LOG = LoggerFactory.getLogger(SHA1.class);
  44.     private static final boolean DETECT_COLLISIONS;

  45.     static {
  46.         SystemReader sr = SystemReader.getInstance();
  47.         String v = sr.getProperty("org.eclipse.jgit.util.sha1.detectCollision"); //$NON-NLS-1$
  48.         DETECT_COLLISIONS = v != null ? Boolean.parseBoolean(v) : true;
  49.     }

  50.     /**
  51.      * Create a new context to compute a SHA-1 hash of data.
  52.      *
  53.      * @return a new context to compute a SHA-1 hash of data.
  54.      */
  55.     public static SHA1 newInstance() {
  56.         return new SHA1();
  57.     }

  58.     private final State h = new State();
  59.     private final int[] w = new int[80];

  60.     /** Buffer to accumulate partial blocks to 64 byte alignment. */
  61.     private final byte[] buffer = new byte[64];

  62.     /** Total number of bytes in the message. */
  63.     private long length;

  64.     private boolean detectCollision = DETECT_COLLISIONS;
  65.     private boolean foundCollision;

  66.     private final int[] w2 = new int[80];
  67.     private final State state58 = new State();
  68.     private final State state65 = new State();
  69.     private final State hIn = new State();
  70.     private final State hTmp = new State();

  71.     private SHA1() {
  72.         h.init();
  73.     }

  74.     /**
  75.      * Enable likely collision detection.
  76.      * <p>
  77.      * Default is {@code true}.
  78.      * <p>
  79.      * May also be set by system property:
  80.      * {@code -Dorg.eclipse.jgit.util.sha1.detectCollision=true}.
  81.      *
  82.      * @param detect
  83.      *            a boolean.
  84.      * @return {@code this}
  85.      */
  86.     public SHA1 setDetectCollision(boolean detect) {
  87.         detectCollision = detect;
  88.         return this;
  89.     }

  90.     /**
  91.      * Update the digest computation by adding a byte.
  92.      *
  93.      * @param b a byte.
  94.      */
  95.     public void update(byte b) {
  96.         int bufferLen = (int) (length & 63);
  97.         length++;
  98.         buffer[bufferLen] = b;
  99.         if (bufferLen == 63) {
  100.             compress(buffer, 0);
  101.         }
  102.     }

  103.     /**
  104.      * Update the digest computation by adding bytes to the message.
  105.      *
  106.      * @param in
  107.      *            input array of bytes.
  108.      */
  109.     public void update(byte[] in) {
  110.         update(in, 0, in.length);
  111.     }

  112.     /**
  113.      * Update the digest computation by adding bytes to the message.
  114.      *
  115.      * @param in
  116.      *            input array of bytes.
  117.      * @param p
  118.      *            offset to start at from {@code in}.
  119.      * @param len
  120.      *            number of bytes to hash.
  121.      */
  122.     public void update(byte[] in, int p, int len) {
  123.         // SHA-1 compress can only process whole 64 byte blocks.
  124.         // Hold partial updates in buffer, whose length is the low bits.
  125.         int bufferLen = (int) (length & 63);
  126.         length += len;

  127.         if (bufferLen > 0) {
  128.             int n = Math.min(64 - bufferLen, len);
  129.             System.arraycopy(in, p, buffer, bufferLen, n);
  130.             p += n;
  131.             len -= n;
  132.             if (bufferLen + n < 64) {
  133.                 return;
  134.             }
  135.             compress(buffer, 0);
  136.         }
  137.         while (len >= 64) {
  138.             compress(in, p);
  139.             p += 64;
  140.             len -= 64;
  141.         }
  142.         if (len > 0) {
  143.             System.arraycopy(in, p, buffer, 0, len);
  144.         }
  145.     }

  146.     private void compress(byte[] block, int p) {
  147.         initBlock(block, p);
  148.         int ubcDvMask = detectCollision ? UbcCheck.check(w) : 0;
  149.         compress();

  150.         while (ubcDvMask != 0) {
  151.             int b = numberOfTrailingZeros(lowestOneBit(ubcDvMask));
  152.             UbcCheck.DvInfo dv = UbcCheck.DV[b];
  153.             for (int i = 0; i < 80; i++) {
  154.                 w2[i] = w[i] ^ dv.dm[i];
  155.             }
  156.             recompress(dv.testt);
  157.             if (eq(hTmp, h)) {
  158.                 foundCollision = true;
  159.                 break;
  160.             }
  161.             ubcDvMask &= ~(1 << b);
  162.         }
  163.     }

  164.     private void initBlock(byte[] block, int p) {
  165.         for (int t = 0; t < 16; t++) {
  166.             w[t] = NB.decodeInt32(block, p + (t << 2));
  167.         }

  168.         // RFC 3174 6.1.b, extend state vector to 80 words.
  169.         for (int t = 16; t < 80; t++) {
  170.             int x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  171.             w[t] = rotateLeft(x, 1); // S^1(...)
  172.         }
  173.     }

  174.     private void compress() {
  175.         // Method 1 from RFC 3174 section 6.1.
  176.         // Method 2 (circular queue of 16 words) is slower.
  177.         int a = h.a, b = h.b, c = h.c, d = h.d, e = h.e;

  178.         // @formatter:off
  179.          e += s1(a, b, c, d,w[ 0]);  b = rotateLeft( b, 30);
  180.          d += s1(e, a, b, c,w[ 1]);  a = rotateLeft( a, 30);
  181.          c += s1(d, e, a, b,w[ 2]);  e = rotateLeft( e, 30);
  182.          b += s1(c, d, e, a,w[ 3]);  d = rotateLeft( d, 30);
  183.          a += s1(b, c, d, e,w[ 4]);  c = rotateLeft( c, 30);
  184.          e += s1(a, b, c, d,w[ 5]);  b = rotateLeft( b, 30);
  185.          d += s1(e, a, b, c,w[ 6]);  a = rotateLeft( a, 30);
  186.          c += s1(d, e, a, b,w[ 7]);  e = rotateLeft( e, 30);
  187.          b += s1(c, d, e, a,w[ 8]);  d = rotateLeft( d, 30);
  188.          a += s1(b, c, d, e,w[ 9]);  c = rotateLeft( c, 30);
  189.          e += s1(a, b, c, d,w[ 10]);  b = rotateLeft( b, 30);
  190.          d += s1(e, a, b, c,w[ 11]);  a = rotateLeft( a, 30);
  191.          c += s1(d, e, a, b,w[ 12]);  e = rotateLeft( e, 30);
  192.          b += s1(c, d, e, a,w[ 13]);  d = rotateLeft( d, 30);
  193.          a += s1(b, c, d, e,w[ 14]);  c = rotateLeft( c, 30);
  194.          e += s1(a, b, c, d,w[ 15]);  b = rotateLeft( b, 30);
  195.          d += s1(e, a, b, c,w[ 16]);  a = rotateLeft( a, 30);
  196.          c += s1(d, e, a, b,w[ 17]);  e = rotateLeft( e, 30);
  197.          b += s1(c, d, e, a,w[ 18]);  d = rotateLeft( d, 30);
  198.          a += s1(b, c, d, e,w[ 19]);  c = rotateLeft( c, 30);

  199.          e += s2(a, b, c, d,w[ 20]);  b = rotateLeft( b, 30);
  200.          d += s2(e, a, b, c,w[ 21]);  a = rotateLeft( a, 30);
  201.          c += s2(d, e, a, b,w[ 22]);  e = rotateLeft( e, 30);
  202.          b += s2(c, d, e, a,w[ 23]);  d = rotateLeft( d, 30);
  203.          a += s2(b, c, d, e,w[ 24]);  c = rotateLeft( c, 30);
  204.          e += s2(a, b, c, d,w[ 25]);  b = rotateLeft( b, 30);
  205.          d += s2(e, a, b, c,w[ 26]);  a = rotateLeft( a, 30);
  206.          c += s2(d, e, a, b,w[ 27]);  e = rotateLeft( e, 30);
  207.          b += s2(c, d, e, a,w[ 28]);  d = rotateLeft( d, 30);
  208.          a += s2(b, c, d, e,w[ 29]);  c = rotateLeft( c, 30);
  209.          e += s2(a, b, c, d,w[ 30]);  b = rotateLeft( b, 30);
  210.          d += s2(e, a, b, c,w[ 31]);  a = rotateLeft( a, 30);
  211.          c += s2(d, e, a, b,w[ 32]);  e = rotateLeft( e, 30);
  212.          b += s2(c, d, e, a,w[ 33]);  d = rotateLeft( d, 30);
  213.          a += s2(b, c, d, e,w[ 34]);  c = rotateLeft( c, 30);
  214.          e += s2(a, b, c, d,w[ 35]);  b = rotateLeft( b, 30);
  215.          d += s2(e, a, b, c,w[ 36]);  a = rotateLeft( a, 30);
  216.          c += s2(d, e, a, b,w[ 37]);  e = rotateLeft( e, 30);
  217.          b += s2(c, d, e, a,w[ 38]);  d = rotateLeft( d, 30);
  218.          a += s2(b, c, d, e,w[ 39]);  c = rotateLeft( c, 30);

  219.          e += s3(a, b, c, d,w[ 40]);  b = rotateLeft( b, 30);
  220.          d += s3(e, a, b, c,w[ 41]);  a = rotateLeft( a, 30);
  221.          c += s3(d, e, a, b,w[ 42]);  e = rotateLeft( e, 30);
  222.          b += s3(c, d, e, a,w[ 43]);  d = rotateLeft( d, 30);
  223.          a += s3(b, c, d, e,w[ 44]);  c = rotateLeft( c, 30);
  224.          e += s3(a, b, c, d,w[ 45]);  b = rotateLeft( b, 30);
  225.          d += s3(e, a, b, c,w[ 46]);  a = rotateLeft( a, 30);
  226.          c += s3(d, e, a, b,w[ 47]);  e = rotateLeft( e, 30);
  227.          b += s3(c, d, e, a,w[ 48]);  d = rotateLeft( d, 30);
  228.          a += s3(b, c, d, e,w[ 49]);  c = rotateLeft( c, 30);
  229.          e += s3(a, b, c, d,w[ 50]);  b = rotateLeft( b, 30);
  230.          d += s3(e, a, b, c,w[ 51]);  a = rotateLeft( a, 30);
  231.          c += s3(d, e, a, b,w[ 52]);  e = rotateLeft( e, 30);
  232.          b += s3(c, d, e, a,w[ 53]);  d = rotateLeft( d, 30);
  233.          a += s3(b, c, d, e,w[ 54]);  c = rotateLeft( c, 30);
  234.          e += s3(a, b, c, d,w[ 55]);  b = rotateLeft( b, 30);
  235.          d += s3(e, a, b, c,w[ 56]);  a = rotateLeft( a, 30);
  236.          c += s3(d, e, a, b,w[ 57]);  e = rotateLeft( e, 30);
  237.         state58.save(a, b, c, d, e);
  238.          b += s3(c, d, e, a,w[ 58]);  d = rotateLeft( d, 30);
  239.          a += s3(b, c, d, e,w[ 59]);  c = rotateLeft( c, 30);

  240.          e += s4(a, b, c, d,w[ 60]);  b = rotateLeft( b, 30);
  241.          d += s4(e, a, b, c,w[ 61]);  a = rotateLeft( a, 30);
  242.          c += s4(d, e, a, b,w[ 62]);  e = rotateLeft( e, 30);
  243.          b += s4(c, d, e, a,w[ 63]);  d = rotateLeft( d, 30);
  244.          a += s4(b, c, d, e,w[ 64]);  c = rotateLeft( c, 30);
  245.         state65.save(a, b, c, d, e);
  246.          e += s4(a, b, c, d,w[ 65]);  b = rotateLeft( b, 30);
  247.          d += s4(e, a, b, c,w[ 66]);  a = rotateLeft( a, 30);
  248.          c += s4(d, e, a, b,w[ 67]);  e = rotateLeft( e, 30);
  249.          b += s4(c, d, e, a,w[ 68]);  d = rotateLeft( d, 30);
  250.          a += s4(b, c, d, e,w[ 69]);  c = rotateLeft( c, 30);
  251.          e += s4(a, b, c, d,w[ 70]);  b = rotateLeft( b, 30);
  252.          d += s4(e, a, b, c,w[ 71]);  a = rotateLeft( a, 30);
  253.          c += s4(d, e, a, b,w[ 72]);  e = rotateLeft( e, 30);
  254.          b += s4(c, d, e, a,w[ 73]);  d = rotateLeft( d, 30);
  255.          a += s4(b, c, d, e,w[ 74]);  c = rotateLeft( c, 30);
  256.          e += s4(a, b, c, d,w[ 75]);  b = rotateLeft( b, 30);
  257.          d += s4(e, a, b, c,w[ 76]);  a = rotateLeft( a, 30);
  258.          c += s4(d, e, a, b,w[ 77]);  e = rotateLeft( e, 30);
  259.          b += s4(c, d, e, a,w[ 78]);  d = rotateLeft( d, 30);
  260.          a += s4(b, c, d, e,w[ 79]);  c = rotateLeft( c, 30);

  261.         // @formatter:on
  262.         h.save(h.a + a, h.b + b, h.c + c, h.d + d, h.e + e);
  263.     }

  264.     private void recompress(int t) {
  265.         State s;
  266.         switch (t) {
  267.         case 58:
  268.             s = state58;
  269.             break;
  270.         case 65:
  271.             s = state65;
  272.             break;
  273.         default:
  274.             throw new IllegalStateException();
  275.         }
  276.         int a = s.a, b = s.b, c = s.c, d = s.d, e = s.e;

  277.         // @formatter:off
  278.       if (t == 65) {
  279.         { c = rotateRight( c, 30);  a -= s4(b, c, d, e,w2[ 64]);}
  280.         { d = rotateRight( d, 30);  b -= s4(c, d, e, a,w2[ 63]);}
  281.         { e = rotateRight( e, 30);  c -= s4(d, e, a, b,w2[ 62]);}
  282.         { a = rotateRight( a, 30);  d -= s4(e, a, b, c,w2[ 61]);}
  283.         { b = rotateRight( b, 30);  e -= s4(a, b, c, d,w2[ 60]);}

  284.         { c = rotateRight( c, 30);  a -= s3(b, c, d, e,w2[ 59]);}
  285.         { d = rotateRight( d, 30);  b -= s3(c, d, e, a,w2[ 58]);}
  286.       }
  287.         { e = rotateRight( e, 30);  c -= s3(d, e, a, b,w2[ 57]);}
  288.         { a = rotateRight( a, 30);  d -= s3(e, a, b, c,w2[ 56]);}
  289.         { b = rotateRight( b, 30);  e -= s3(a, b, c, d,w2[ 55]);}
  290.         { c = rotateRight( c, 30);  a -= s3(b, c, d, e,w2[ 54]);}
  291.         { d = rotateRight( d, 30);  b -= s3(c, d, e, a,w2[ 53]);}
  292.         { e = rotateRight( e, 30);  c -= s3(d, e, a, b,w2[ 52]);}
  293.         { a = rotateRight( a, 30);  d -= s3(e, a, b, c,w2[ 51]);}
  294.         { b = rotateRight( b, 30);  e -= s3(a, b, c, d,w2[ 50]);}
  295.         { c = rotateRight( c, 30);  a -= s3(b, c, d, e,w2[ 49]);}
  296.         { d = rotateRight( d, 30);  b -= s3(c, d, e, a,w2[ 48]);}
  297.         { e = rotateRight( e, 30);  c -= s3(d, e, a, b,w2[ 47]);}
  298.         { a = rotateRight( a, 30);  d -= s3(e, a, b, c,w2[ 46]);}
  299.         { b = rotateRight( b, 30);  e -= s3(a, b, c, d,w2[ 45]);}
  300.         { c = rotateRight( c, 30);  a -= s3(b, c, d, e,w2[ 44]);}
  301.         { d = rotateRight( d, 30);  b -= s3(c, d, e, a,w2[ 43]);}
  302.         { e = rotateRight( e, 30);  c -= s3(d, e, a, b,w2[ 42]);}
  303.         { a = rotateRight( a, 30);  d -= s3(e, a, b, c,w2[ 41]);}
  304.         { b = rotateRight( b, 30);  e -= s3(a, b, c, d,w2[ 40]);}

  305.         { c = rotateRight( c, 30);  a -= s2(b, c, d, e,w2[ 39]);}
  306.         { d = rotateRight( d, 30);  b -= s2(c, d, e, a,w2[ 38]);}
  307.         { e = rotateRight( e, 30);  c -= s2(d, e, a, b,w2[ 37]);}
  308.         { a = rotateRight( a, 30);  d -= s2(e, a, b, c,w2[ 36]);}
  309.         { b = rotateRight( b, 30);  e -= s2(a, b, c, d,w2[ 35]);}
  310.         { c = rotateRight( c, 30);  a -= s2(b, c, d, e,w2[ 34]);}
  311.         { d = rotateRight( d, 30);  b -= s2(c, d, e, a,w2[ 33]);}
  312.         { e = rotateRight( e, 30);  c -= s2(d, e, a, b,w2[ 32]);}
  313.         { a = rotateRight( a, 30);  d -= s2(e, a, b, c,w2[ 31]);}
  314.         { b = rotateRight( b, 30);  e -= s2(a, b, c, d,w2[ 30]);}
  315.         { c = rotateRight( c, 30);  a -= s2(b, c, d, e,w2[ 29]);}
  316.         { d = rotateRight( d, 30);  b -= s2(c, d, e, a,w2[ 28]);}
  317.         { e = rotateRight( e, 30);  c -= s2(d, e, a, b,w2[ 27]);}
  318.         { a = rotateRight( a, 30);  d -= s2(e, a, b, c,w2[ 26]);}
  319.         { b = rotateRight( b, 30);  e -= s2(a, b, c, d,w2[ 25]);}
  320.         { c = rotateRight( c, 30);  a -= s2(b, c, d, e,w2[ 24]);}
  321.         { d = rotateRight( d, 30);  b -= s2(c, d, e, a,w2[ 23]);}
  322.         { e = rotateRight( e, 30);  c -= s2(d, e, a, b,w2[ 22]);}
  323.         { a = rotateRight( a, 30);  d -= s2(e, a, b, c,w2[ 21]);}
  324.         { b = rotateRight( b, 30);  e -= s2(a, b, c, d,w2[ 20]);}

  325.         { c = rotateRight( c, 30);  a -= s1(b, c, d, e,w2[ 19]);}
  326.         { d = rotateRight( d, 30);  b -= s1(c, d, e, a,w2[ 18]);}
  327.         { e = rotateRight( e, 30);  c -= s1(d, e, a, b,w2[ 17]);}
  328.         { a = rotateRight( a, 30);  d -= s1(e, a, b, c,w2[ 16]);}
  329.         { b = rotateRight( b, 30);  e -= s1(a, b, c, d,w2[ 15]);}
  330.         { c = rotateRight( c, 30);  a -= s1(b, c, d, e,w2[ 14]);}
  331.         { d = rotateRight( d, 30);  b -= s1(c, d, e, a,w2[ 13]);}
  332.         { e = rotateRight( e, 30);  c -= s1(d, e, a, b,w2[ 12]);}
  333.         { a = rotateRight( a, 30);  d -= s1(e, a, b, c,w2[ 11]);}
  334.         { b = rotateRight( b, 30);  e -= s1(a, b, c, d,w2[ 10]);}
  335.         { c = rotateRight( c, 30);  a -= s1(b, c, d, e,w2[ 9]);}
  336.         { d = rotateRight( d, 30);  b -= s1(c, d, e, a,w2[ 8]);}
  337.         { e = rotateRight( e, 30);  c -= s1(d, e, a, b,w2[ 7]);}
  338.         { a = rotateRight( a, 30);  d -= s1(e, a, b, c,w2[ 6]);}
  339.         { b = rotateRight( b, 30);  e -= s1(a, b, c, d,w2[ 5]);}
  340.         { c = rotateRight( c, 30);  a -= s1(b, c, d, e,w2[ 4]);}
  341.         { d = rotateRight( d, 30);  b -= s1(c, d, e, a,w2[ 3]);}
  342.         { e = rotateRight( e, 30);  c -= s1(d, e, a, b,w2[ 2]);}
  343.         { a = rotateRight( a, 30);  d -= s1(e, a, b, c,w2[ 1]);}
  344.         { b = rotateRight( b, 30);  e -= s1(a, b, c, d,w2[ 0]);}

  345.         hIn.save(a, b, c, d, e);
  346.         a = s.a; b = s.b; c = s.c; d = s.d; e = s.e;

  347.       if (t == 58) {
  348.         { b += s3(c, d, e, a,w2[ 58]);  d = rotateLeft( d, 30);}
  349.         { a += s3(b, c, d, e,w2[ 59]);  c = rotateLeft( c, 30);}

  350.         { e += s4(a, b, c, d,w2[ 60]);  b = rotateLeft( b, 30);}
  351.         { d += s4(e, a, b, c,w2[ 61]);  a = rotateLeft( a, 30);}
  352.         { c += s4(d, e, a, b,w2[ 62]);  e = rotateLeft( e, 30);}
  353.         { b += s4(c, d, e, a,w2[ 63]);  d = rotateLeft( d, 30);}
  354.         { a += s4(b, c, d, e,w2[ 64]);  c = rotateLeft( c, 30);}
  355.       }
  356.         { e += s4(a, b, c, d,w2[ 65]);  b = rotateLeft( b, 30);}
  357.         { d += s4(e, a, b, c,w2[ 66]);  a = rotateLeft( a, 30);}
  358.         { c += s4(d, e, a, b,w2[ 67]);  e = rotateLeft( e, 30);}
  359.         { b += s4(c, d, e, a,w2[ 68]);  d = rotateLeft( d, 30);}
  360.         { a += s4(b, c, d, e,w2[ 69]);  c = rotateLeft( c, 30);}
  361.         { e += s4(a, b, c, d,w2[ 70]);  b = rotateLeft( b, 30);}
  362.         { d += s4(e, a, b, c,w2[ 71]);  a = rotateLeft( a, 30);}
  363.         { c += s4(d, e, a, b,w2[ 72]);  e = rotateLeft( e, 30);}
  364.         { b += s4(c, d, e, a,w2[ 73]);  d = rotateLeft( d, 30);}
  365.         { a += s4(b, c, d, e,w2[ 74]);  c = rotateLeft( c, 30);}
  366.         { e += s4(a, b, c, d,w2[ 75]);  b = rotateLeft( b, 30);}
  367.         { d += s4(e, a, b, c,w2[ 76]);  a = rotateLeft( a, 30);}
  368.         { c += s4(d, e, a, b,w2[ 77]);  e = rotateLeft( e, 30);}
  369.         { b += s4(c, d, e, a,w2[ 78]);  d = rotateLeft( d, 30);}
  370.         { a += s4(b, c, d, e,w2[ 79]);  c = rotateLeft( c, 30);}

  371.         // @formatter:on
  372.         hTmp.save(hIn.a + a, hIn.b + b, hIn.c + c, hIn.d + d, hIn.e + e);
  373.     }

  374.     private static int s1(int a, int b, int c, int d, int w_t) {
  375.         return rotateLeft(a, 5)
  376.                 // f: 0 <= t <= 19
  377.                 + ((b & c) | ((~b) & d))
  378.                 + 0x5A827999 + w_t;
  379.     }

  380.     private static int s2(int a, int b, int c, int d, int w_t) {
  381.         return rotateLeft(a, 5)
  382.                 // f: 20 <= t <= 39
  383.                 + (b ^ c ^ d)
  384.                 + 0x6ED9EBA1 + w_t;
  385.     }

  386.     private static int s3(int a, int b, int c, int d, int w_t) {
  387.         return rotateLeft(a, 5)
  388.                 // f: 40 <= t <= 59
  389.                 + ((b & c) | (b & d) | (c & d))
  390.                 + 0x8F1BBCDC + w_t;
  391.     }

  392.     private static int s4(int a, int b, int c, int d, int w_t) {
  393.         return rotateLeft(a, 5)
  394.                 // f: 60 <= t <= 79
  395.                 + (b ^ c ^ d)
  396.                 + 0xCA62C1D6 + w_t;
  397.     }

  398.     private static boolean eq(State q, State r) {
  399.         return q.a == r.a
  400.                 && q.b == r.b
  401.                 && q.c == r.c
  402.                 && q.d == r.d
  403.                 && q.e == r.e;
  404.     }

  405.     private void finish() {
  406.         int bufferLen = (int) (length & 63);
  407.         if (bufferLen > 55) {
  408.             // Last block is too small; pad, compress, pad another block.
  409.             buffer[bufferLen++] = (byte) 0x80;
  410.             Arrays.fill(buffer, bufferLen, 64, (byte) 0);
  411.             compress(buffer, 0);
  412.             Arrays.fill(buffer, 0, 56, (byte) 0);
  413.         } else {
  414.             // Last block can hold padding and length.
  415.             buffer[bufferLen++] = (byte) 0x80;
  416.             Arrays.fill(buffer, bufferLen, 56, (byte) 0);
  417.         }

  418.         // SHA-1 appends the length of the message in bits after the
  419.         // padding block (above). Here length is in bytes. Multiply by
  420.         // 8 by shifting by 3 as part of storing the 64 bit byte length
  421.         // into the two words expected in the trailer.
  422.         NB.encodeInt32(buffer, 56, (int) (length >>> (32 - 3)));
  423.         NB.encodeInt32(buffer, 60, (int) (length << 3));
  424.         compress(buffer, 0);

  425.         if (foundCollision) {
  426.             ObjectId id = h.toObjectId();
  427.             LOG.warn(MessageFormat.format(JGitText.get().sha1CollisionDetected,
  428.                     id.name()));
  429.             throw new Sha1CollisionException(id);
  430.         }
  431.     }

  432.     /**
  433.      * Finish the digest and return the resulting hash.
  434.      * <p>
  435.      * Once {@code digest()} is called, this instance should be discarded.
  436.      *
  437.      * @return the bytes for the resulting hash.
  438.      * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  439.      *             if a collision was detected and safeHash is false.
  440.      */
  441.     public byte[] digest() throws Sha1CollisionException {
  442.         finish();

  443.         byte[] b = new byte[20];
  444.         NB.encodeInt32(b, 0, h.a);
  445.         NB.encodeInt32(b, 4, h.b);
  446.         NB.encodeInt32(b, 8, h.c);
  447.         NB.encodeInt32(b, 12, h.d);
  448.         NB.encodeInt32(b, 16, h.e);
  449.         return b;
  450.     }

  451.     /**
  452.      * Finish the digest and return the resulting hash.
  453.      * <p>
  454.      * Once {@code digest()} is called, this instance should be discarded.
  455.      *
  456.      * @return the ObjectId for the resulting hash.
  457.      * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  458.      *             if a collision was detected and safeHash is false.
  459.      */
  460.     public ObjectId toObjectId() throws Sha1CollisionException {
  461.         finish();
  462.         return h.toObjectId();
  463.     }

  464.     /**
  465.      * Finish the digest and return the resulting hash.
  466.      * <p>
  467.      * Once {@code digest()} is called, this instance should be discarded.
  468.      *
  469.      * @param id
  470.      *            destination to copy the digest to.
  471.      * @throws org.eclipse.jgit.util.sha1.Sha1CollisionException
  472.      *             if a collision was detected and safeHash is false.
  473.      */
  474.     public void digest(MutableObjectId id) throws Sha1CollisionException {
  475.         finish();
  476.         id.set(h.a, h.b, h.c, h.d, h.e);
  477.     }

  478.     /**
  479.      * Check if a collision was detected.
  480.      *
  481.      * <p>
  482.      * This method only returns an accurate result after the digest was obtained
  483.      * through {@link #digest()}, {@link #digest(MutableObjectId)} or
  484.      * {@link #toObjectId()}, as the hashing function must finish processing to
  485.      * know the final state.
  486.      *
  487.      * @return {@code true} if a likely collision was detected.
  488.      */
  489.     public boolean hasCollision() {
  490.         return foundCollision;
  491.     }

  492.     /**
  493.      * Reset this instance to compute another hash.
  494.      *
  495.      * @return {@code this}.
  496.      */
  497.     public SHA1 reset() {
  498.         h.init();
  499.         length = 0;
  500.         foundCollision = false;
  501.         return this;
  502.     }

  503.     private static final class State {
  504.         int a;
  505.         int b;
  506.         int c;
  507.         int d;
  508.         int e;

  509.         final void init() {
  510.             // Magic initialization constants defined by FIPS180.
  511.             save(0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0);
  512.         }

  513.         final void save(int a1, int b1, int c1, int d1, int e1) {
  514.             a = a1;
  515.             b = b1;
  516.             c = c1;
  517.             d = d1;
  518.             e = e1;
  519.         }

  520.         ObjectId toObjectId() {
  521.             return new ObjectId(a, b, c, d, e);
  522.         }
  523.     }
  524. }