Interface DigestAlgorithm

  • All Implemented Interfaces:

    
    public interface DigestAlgorithm
    
                        

    Interface defining a custom digest calculation implementation. The interface should be implemented if a CPK developer wishes to provide support for digest algorithms beyond those supported by the Corda platform. The implementation of the interface must be coupled with the implementation of the DigestAlgorithmFactory which will be used to create the instances. For each algorithm there must be matching a pair of DigestAlgorithmFactory and DigestAlgorithm implementations.

    Example use of DigestAlgorithm:
    • Kotlin:
      
      class TripleSha256Digest : DigestAlgorithm {
          companion object {
              const val ALGORITHM = "SHA-256-TRIPLE"
              const val STREAM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE
              fun ByteArray.sha256Bytes(): ByteArray =
                  MessageDigest.getInstance(DigestAlgorithmName.SHA2_256.name).digest(this)
          }
      
          override fun getAlgorithm() = ALGORITHM
          override fun getDigestLength() = 32
          override fun digest(bytes: ByteArray): ByteArray = bytes.sha256Bytes().sha256Bytes().sha256Bytes()
          override fun digest(inputStream: InputStream): ByteArray {
              val messageDigest = MessageDigest.getInstance(DigestAlgorithmName.SHA2_256.name)
              val buffer = ByteArray(STREAM_BUFFER_SIZE)
              while (true) {
                  val read = inputStream.read(buffer)
                  if (read <= 0) break
                  messageDigest.update(buffer, 0, read)
              }
              return messageDigest.digest().sha256Bytes().sha256Bytes()
          }
      }
      </pre></li>
      <li>Java:<pre>{
      public class TripleSha256Digest implements DigestAlgorithm {
          static String ALGORITHM = "SHA-256-TRIPLE";
          static int BUFFER_SIZE = 8192;
          static byte[] sha256Bytes(MessageDigest md, byte[] bytes) {
              md.reset();
              md.update(bytes);
              return md.digest();
          }
      
          
          public String getAlgorithm() {
              return ALGORITHM;
          }
          
          public int getDigestLength() {
              return 32;
          }
          
          public byte[] digest(@NotNull byte[] bytes) {
              try {
                  MessageDigest md = MessageDigest.getInstance(DigestAlgorithmName.SHA2_256.getName());
                  byte[] digestOnce = sha256Bytes(md, bytes);
                  byte[] digestTwice = sha256Bytes(md, digestOnce);
                  byte[] digestThrice = sha256Bytes(md, digestTwice);
                  return digestThrice;
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
          
          public byte[] digest(@NotNull InputStream inputStream) {
              try {
                  MessageDigest md = MessageDigest.getInstance(DigestAlgorithmName.SHA2_256.getName());
                  byte[] buffer = new byte[BUFFER_SIZE];
                  int read;
                  while ((read = inputStream.read(buffer)) >= 0) {
                      md.update(buffer, 0, read);
                  }
                  byte[] digestOnce = md.digest();
                  byte[] digestTwice = sha256Bytes(md, digestOnce);
                  byte[] digestThrice = sha256Bytes(md, digestTwice);
                  return digestThrice;
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      }
      </pre></li>
      </ul>
      
      Example use of {DigestAlgorithmFactory}:
      <ul>
      <li>Kotlin:<pre>{
      class TripleSha256 : DigestAlgorithmFactory {
          override fun getAlgorithm() = TripleSha256Digest.ALGORITHM
          override fun getInstance(): DigestAlgorithm = TripleSha256Digest()
      }
      </pre></li>
      <li>Java:<pre>{
      class TripleSha256 implements DigestAlgorithmFactory {
          
          public String getAlgorithm() {
              return TripleSha256Digest.ALGORITHM;
          }
          
          public DigestAlgorithm getInstance() {
              return new TripleSha256Digest();
          }
      }
      </pre></li>
      </ul>
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      abstract String getAlgorithm() Get the algorithm identifier, for example, 'QUAD-SHA-256', the unique name (per Corda Platform and given CPK) of the digest algorithm.
      abstract int getDigestLength() The length of the digest in bytes.
      abstract Array<byte> digest(@NotNull() Array<byte> bytes) Computes the digest of the byte array.
      abstract Array<byte> digest(@NotNull() InputStream inputStream) Computes the digest of the InputStream bytes.
      • Methods inherited from class java.lang.Object

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

    • Method Detail

      • getAlgorithm

        @NotNull() abstract String getAlgorithm()

        Get the algorithm identifier, for example, 'QUAD-SHA-256', the unique name (per Corda Platform and given CPK) of the digest algorithm. The name must match the names used by the corresponding [DigestAlgorithmFactory].

      • getDigestLength

         abstract int getDigestLength()

        The length of the digest in bytes.

      • digest

        @NotNull() abstract Array<byte> digest(@NotNull() Array<byte> bytes)

        Computes the digest of the byte array. The computation must be stateless and thread safe.

        Parameters:
        bytes - The byte array to hash.
      • digest

        @NotNull() abstract Array<byte> digest(@NotNull() InputStream inputStream)

        Computes the digest of the InputStream bytes. The computation must be stateless and thread safe.

        Parameters:
        inputStream - The [InputStream] to hash.