Class MessageDigest

java.lang.Object
dev.gmitch215.bytebox.security.MessageDigest

public final class MessageDigest extends Object
java.security.MessageDigest, which the class library does not carry, for MD5, SHA-1, SHA-256, SHA-384 and SHA-512.

Written in Java rather than backed by SubtleCrypto.digest, because that returns a promise and a substituted class cannot suspend. SHA-224, SHA-512/256, SHA-3 and the MD2 digest are not provided; asking for them throws NoSuchAlgorithmException the way an unknown name does on a JVM. Providers are not modelled: there is no getInstance(String, String) and no getProvider, and no clone.

Since:
1.0.3
  • Method Details

    • getInstance

      public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
      Parameters:
      algorithm - a name such as SHA-256, in any case, or one of the JDK's aliases
      Returns:
      a digest that has taken no input
      Throws:
      NoSuchAlgorithmException - when the algorithm is not one of the five
    • update

      public void update(byte input)
      Parameters:
      input - the byte to add
    • update

      public void update(byte[] input)
      Parameters:
      input - the bytes to add
    • update

      public void update(byte[] input, int offset, int len)
      Parameters:
      input - the array to read from
      offset - where in it to start
      len - how many bytes to add
    • update

      public void update(ByteBuffer input)
      Parameters:
      input - the buffer to drain from its position to its limit
    • digest

      public byte[] digest()
      Returns:
      the digest of everything added, after which this is as new
    • digest

      public byte[] digest(byte[] input)
      Parameters:
      input - the last bytes to add
      Returns:
      the digest of everything added, after which this is as new
    • digest

      public int digest(byte[] buf, int offset, int len) throws DigestException
      Parameters:
      buf - where to put the digest
      offset - where in it to start
      len - how much room to use, at least the digest length
      Returns:
      the number of bytes written
      Throws:
      DigestException - when len is shorter than the digest
    • reset

      public void reset()
      Drops everything added so far.
    • getAlgorithm

      public String getAlgorithm()
      Returns:
      the name this was asked for, as given
    • getDigestLength

      public int getDigestLength()
      Returns:
      the digest length in bytes
    • isEqual

      public static boolean isEqual(byte[] a, byte[] b)
      Parameters:
      a - one digest, or null
      b - another, or null
      Returns:
      whether they are equal, without stopping at the first difference
    • toString

      public String toString()
      Overrides:
      toString in class Object