Class SHA1Spi

  • All Implemented Interfaces:
    java.lang.Cloneable

    public final class SHA1Spi
    extends java.security.MessageDigestSpi
    implements java.lang.Cloneable
    Template for hash function bindings. Unfortunately using openssl's common EVP interface for hash functions introduces significant performance overhead (to the point that the default JCE provider is faster for <= 32 byte hashes). As such we build direct bindings to the older interfaces for each of our supported hash functions - since these are nearly identical, though, we use templates on both the Java and C++ sides to accomplish this. To further improve performance, we avoid using the C++ heap to allocate the state datastructure for our hash function - instead, we allocate a java byte array and put the hash state in it directly. This is safe as long as 1) byte[]s are aligned properly (seems to be the case due to Java object headers) and 2) no native-side pointers exist pointing inside the hash function state structure. Since the latter is currently true and changing it would break the openssl ABI we should be safe. By doing this hack, then, we avoid malloc/free overheads as well as Java finalizer overhead, and can avoid a native call for initializing the hash function (by instead cloning a copy of the initial state of the hash function).
    • Constructor Detail

      • SHA1Spi

        public SHA1Spi()
    • Method Detail

      • engineUpdate

        protected void engineUpdate​(byte input)
        Specified by:
        engineUpdate in class java.security.MessageDigestSpi
      • engineUpdate

        protected void engineUpdate​(byte[] input,
                                    int offset,
                                    int length)
        Specified by:
        engineUpdate in class java.security.MessageDigestSpi
      • engineUpdate

        protected void engineUpdate​(java.nio.ByteBuffer buf)
        Overrides:
        engineUpdate in class java.security.MessageDigestSpi
      • engineGetDigestLength

        protected int engineGetDigestLength()
        Overrides:
        engineGetDigestLength in class java.security.MessageDigestSpi
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.security.MessageDigestSpi
      • engineDigest

        protected byte[] engineDigest()
        Specified by:
        engineDigest in class java.security.MessageDigestSpi
      • engineDigest

        protected int engineDigest​(byte[] buf,
                                   int offset,
                                   int len)
                            throws java.security.DigestException
        Overrides:
        engineDigest in class java.security.MessageDigestSpi
        Throws:
        java.security.DigestException
      • engineReset

        protected void engineReset()
        Specified by:
        engineReset in class java.security.MessageDigestSpi