Class SHA1

java.lang.Object
org.eclipse.jgit.util.sha1.SHA1

public abstract class SHA1 extends Object
SHA-1 interface from FIPS 180-1 / RFC 3174 with optional collision detection. Some implementations may not support collision detection.

See RFC 3174.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    SHA1 implementations available in JGit
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract byte[]
    Finish the digest and return the resulting hash.
    abstract void
    Finish the digest and return the resulting hash.
    abstract boolean
    Check if a collision was detected.
    static SHA1
    Create a new context to compute a SHA-1 hash of data.
    abstract SHA1
    Reset this instance to compute another hash.
    abstract SHA1
    setDetectCollision(boolean detect)
    Enable likely collision detection.
    abstract ObjectId
    Finish the digest and return the resulting hash.
    abstract void
    update(byte b)
    Update the digest computation by adding a byte.
    abstract void
    update(byte[] in)
    Update the digest computation by adding bytes to the message.
    abstract void
    update(byte[] in, int p, int len)
    Update the digest computation by adding bytes to the message.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SHA1

      public SHA1()
  • Method Details

    • newInstance

      public static SHA1 newInstance()
      Create a new context to compute a SHA-1 hash of data.

      If core.sha1Implementation = jdkNative in the user level global git configuration or the system property org.eclipse.jgit.util.sha1.implementation = jdkNative it will create an object using the implementation in the JDK. If both are set the system property takes precedence. Otherwise the pure Java implementation will be used which supports collision detection but is slower.

      Returns:
      a new context to compute a SHA-1 hash of data.
    • update

      public abstract void update(byte b)
      Update the digest computation by adding a byte.
      Parameters:
      b - a byte.
    • update

      public abstract void update(byte[] in)
      Update the digest computation by adding bytes to the message.
      Parameters:
      in - input array of bytes.
    • update

      public abstract void update(byte[] in, int p, int len)
      Update the digest computation by adding bytes to the message.
      Parameters:
      in - input array of bytes.
      p - offset to start at from in.
      len - number of bytes to hash.
    • digest

      public abstract byte[] digest() throws Sha1CollisionException
      Finish the digest and return the resulting hash.

      Once digest() is called, this instance should be discarded.

      Returns:
      the bytes for the resulting hash.
      Throws:
      Sha1CollisionException - if a collision was detected and safeHash is false.
    • toObjectId

      public abstract ObjectId toObjectId() throws Sha1CollisionException
      Finish the digest and return the resulting hash.

      Once digest() is called, this instance should be discarded.

      Returns:
      the ObjectId for the resulting hash.
      Throws:
      Sha1CollisionException - if a collision was detected and safeHash is false.
    • digest

      public abstract void digest(MutableObjectId id) throws Sha1CollisionException
      Finish the digest and return the resulting hash.

      Once digest() is called, this instance should be discarded.

      Parameters:
      id - destination to copy the digest to.
      Throws:
      Sha1CollisionException - if a collision was detected and safeHash is false.
    • reset

      public abstract SHA1 reset()
      Reset this instance to compute another hash.
      Returns:
      this.
    • setDetectCollision

      public abstract SHA1 setDetectCollision(boolean detect)
      Enable likely collision detection.

      Default for implementations supporting collision detection is true.

      Implementations not supporting collision detection ignore calls to this method.

      Parameters:
      detect - a boolean.
      Returns:
      this
    • hasCollision

      public abstract boolean hasCollision()
      Check if a collision was detected. This method only returns an accurate result after the digest was obtained through digest(), digest(MutableObjectId) or toObjectId(), as the hashing function must finish processing to know the final state.

      Implementations not supporting collision detection always return false.

      Returns:
      true if a likely collision was detected.