SHA1Java.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. class SHA1Java extends SHA1 {
  43.     private static final Logger LOG = LoggerFactory.getLogger(SHA1Java.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.     private final State h = new State();
  51.     private final int[] w = new int[80];

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

  54.     /** Total number of bytes in the message. */
  55.     private long length;

  56.     private boolean detectCollision = DETECT_COLLISIONS;
  57.     private boolean foundCollision;

  58.     private final int[] w2 = new int[80];
  59.     private final State state58 = new State();
  60.     private final State state65 = new State();
  61.     private final State hIn = new State();
  62.     private final State hTmp = new State();

  63.     SHA1Java() {
  64.         h.init();
  65.     }

  66.     /**
  67.      * Enable likely collision detection.
  68.      * <p>
  69.      * Default is {@code true}.
  70.      * <p>
  71.      * May also be set by system property:
  72.      * {@code -Dorg.eclipse.jgit.util.sha1.detectCollision=true}.
  73.      *
  74.      * @param detect
  75.      *            a boolean.
  76.      * @return {@code this}
  77.      */
  78.     @Override
  79.     public SHA1 setDetectCollision(boolean detect) {
  80.         detectCollision = detect;
  81.         return this;
  82.     }

  83.     /**
  84.      * Update the digest computation by adding a byte.
  85.      *
  86.      * @param b a byte.
  87.      */
  88.     @Override
  89.     public void update(byte b) {
  90.         int bufferLen = (int) (length & 63);
  91.         length++;
  92.         buffer[bufferLen] = b;
  93.         if (bufferLen == 63) {
  94.             compress(buffer, 0);
  95.         }
  96.     }

  97.     /**
  98.      * Update the digest computation by adding bytes to the message.
  99.      *
  100.      * @param in
  101.      *            input array of bytes.
  102.      */
  103.     @Override
  104.     public void update(byte[] in) {
  105.         update(in, 0, in.length);
  106.     }

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

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

  142.     private void compress(byte[] block, int p) {
  143.         initBlock(block, p);
  144.         int ubcDvMask = detectCollision ? UbcCheck.check(w) : 0;
  145.         compress();

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

  160.     private void initBlock(byte[] block, int p) {
  161.         for (int t = 0; t < 16; t++) {
  162.             w[t] = NB.decodeInt32(block, p + (t << 2));
  163.         }

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

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

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

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

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

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

  257.         // @formatter:on
  258.         h.save(h.a + a, h.b + b, h.c + c, h.d + d, h.e + e);
  259.     }

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

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

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

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

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

  341.         hIn.save(a, b, c, d, e);
  342.         a = s.a; b = s.b; c = s.c; d = s.d; e = s.e;

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

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

  367.         // @formatter:on
  368.         hTmp.save(hIn.a + a, hIn.b + b, hIn.c + c, hIn.d + d, hIn.e + e);
  369.     }

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

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

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

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

  394.     private static boolean eq(State q, State r) {
  395.         return q.a == r.a
  396.                 && q.b == r.b
  397.                 && q.c == r.c
  398.                 && q.d == r.d
  399.                 && q.e == r.e;
  400.     }

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

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

  421.         if (foundCollision) {
  422.             ObjectId id = h.toObjectId();
  423.             LOG.warn(MessageFormat.format(JGitText.get().sha1CollisionDetected,
  424.                     id.name()));
  425.             throw new Sha1CollisionException(id);
  426.         }
  427.     }

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

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

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

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

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

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

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

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

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

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