org.modeshape.common.util

## Class SecureHash

- java.lang.Object
- - org.modeshape.common.util.SecureHash

-     @Immutable
      public class SecureHash
      extends Object

  A simple utility to generate various kinds of secure hashes.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `SecureHash.Algorithm` Commonly-used hashing algorithms.
    `static class` | `SecureHash.HashingInputStream`
    `static class` | `SecureHash.HashingReader`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SecureHash()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static String` | `asHexString(byte[] hash)` Get the string representation of the supplied binary hash.
    `static SecureHash.HashingReader` | `createHashingReader(SecureHash.Algorithm algorithm, Reader reader, Charset charset)` Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the supplied name) as the returned Reader is used.
    `static SecureHash.HashingReader` | `createHashingReader(String digestName, Reader reader, Charset charset)` Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the supplied name) as the returned Reader is used.
    `static SecureHash.HashingInputStream` | `createHashingStream(SecureHash.Algorithm algorithm, InputStream inputStream)` Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the supplied name) as the returned stream is used.
    `static SecureHash.HashingInputStream` | `createHashingStream(String digestName, InputStream inputStream)` Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the supplied name) as the returned stream is used.
    `static byte[]` | `getHash(SecureHash.Algorithm algorithm, byte[] content)` Get the hash of the supplied content, using the supplied digest algorithm.
    `static byte[]` | `getHash(SecureHash.Algorithm algorithm, File file)` Get the hash of the supplied content, using the supplied digest algorithm.
    `static byte[]` | `getHash(SecureHash.Algorithm algorithm, InputStream stream)` Get the hash of the supplied content, using the supplied digest algorithm.
    `static byte[]` | `getHash(String digestName, byte[] content)` Get the hash of the supplied content, using the digest identified by the supplied name.
    `static byte[]` | `getHash(String digestName, File file)` Get the hash of the supplied content, using the digest identified by the supplied name.
    `static byte[]` | `getHash(String digestName, InputStream stream)` Get the hash of the supplied content, using the digest identified by the supplied name.
    `static String` | `sha1(String string)` Computes the sha1 value for the given string.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SecureHash

          public SecureHash()

  - ### Method Detail

    - #### getHash

          public static byte[] getHash(SecureHash.Algorithm algorithm,
                       byte[] content)
                                throws NoSuchAlgorithmException

      Get the hash of the supplied content, using the supplied digest algorithm.
      Parameters:
        `algorithm` - the hashing function algorithm that should be used
        `content` - the content to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found
        `IllegalArgumentException` - if the algorithm is null

    - #### getHash

          public static byte[] getHash(SecureHash.Algorithm algorithm,
                       File file)
                                throws NoSuchAlgorithmException,
                                       IOException

      Get the hash of the supplied content, using the supplied digest algorithm.
      Parameters:
        `algorithm` - the hashing function algorithm that should be used
        `file` - the file containing the content to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found
        `IllegalArgumentException` - if the algorithm is null
        `IOException` - if there is an error reading the file

    - #### getHash

          public static byte[] getHash(SecureHash.Algorithm algorithm,
                       InputStream stream)
                                throws NoSuchAlgorithmException,
                                       IOException

      Get the hash of the supplied content, using the supplied digest algorithm.
      Parameters:
        `algorithm` - the hashing function algorithm that should be used
        `stream` - the stream containing the content to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found
        `IllegalArgumentException` - if the algorithm is null
        `IOException` - if there is an error reading the stream

    - #### getHash

          public static byte[] getHash(String digestName,
                       byte[] content)
                                throws NoSuchAlgorithmException

      Get the hash of the supplied content, using the digest identified by the supplied name.
      Parameters:
        `digestName` - the name of the hashing function (or `message digest`) that should be used
        `content` - the content to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found

    - #### getHash

          public static byte[] getHash(String digestName,
                       File file)
                                throws NoSuchAlgorithmException,
                                       IOException

      Get the hash of the supplied content, using the digest identified by the supplied name.
      Parameters:
        `digestName` - the name of the hashing function (or `message digest`) that should be used
        `file` - the file whose content is to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found
        `IOException` - if there is an error reading the file

    - #### getHash

          public static byte[] getHash(String digestName,
                       InputStream stream)
                                throws NoSuchAlgorithmException,
                                       IOException

      Get the hash of the supplied content, using the digest identified by the supplied name. Note that this method never closes the supplied stream.
      Parameters:
        `digestName` - the name of the hashing function (or `message digest`) that should be used
        `stream` - the stream containing the content to be hashed; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException` - if the supplied algorithm could not be found
        `IOException` - if there is an error reading the stream

    - #### createHashingStream

          public static SecureHash.HashingInputStream createHashingStream(SecureHash.Algorithm algorithm,
                                                          InputStream inputStream)
                                                                   throws NoSuchAlgorithmException

      Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being processed by another reader, and saves from having to process the same stream twice.
      Parameters:
        `algorithm` - the hashing function algorithm that should be used
        `inputStream` - the stream containing the content that is to be hashed
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException`

    - #### createHashingStream

          public static SecureHash.HashingInputStream createHashingStream(String digestName,
                                                          InputStream inputStream)
                                                                   throws NoSuchAlgorithmException

      Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being processed by another reader, and saves from having to process the same stream twice.
      Parameters:
        `digestName` - the name of the hashing function (or `message digest`) that should be used
        `inputStream` - the stream containing the content that is to be hashed
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException`

    - #### createHashingReader

          public static SecureHash.HashingReader createHashingReader(SecureHash.Algorithm algorithm,
                                                     Reader reader,
                                                     Charset charset)
                                                              throws NoSuchAlgorithmException

      Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the supplied name) as the returned Reader is used. This can be used to compute the hash while the content is being processed, and saves from having to process the same content twice.
      Parameters:
        `algorithm` - the hashing function algorithm that should be used
        `reader` - the reader containing the content that is to be hashed
        `charset` - the character set used within the supplied reader; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException`

    - #### createHashingReader

          public static SecureHash.HashingReader createHashingReader(String digestName,
                                                     Reader reader,
                                                     Charset charset)
                                                              throws NoSuchAlgorithmException

      Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the supplied name) as the returned Reader is used. This can be used to compute the hash while the content is being processed, and saves from having to process the same content twice.
      Parameters:
        `digestName` - the name of the hashing function (or `message digest`) that should be used
        `reader` - the reader containing the content that is to be hashed
        `charset` - the character set used within the supplied reader; may not be null
      Returns:
        the hash of the contents as a byte array
      Throws:
        `NoSuchAlgorithmException`

    - #### asHexString

          public static String asHexString(byte[] hash)

      Get the string representation of the supplied binary hash.
      Parameters:
        `hash` - the binary hash
      Returns:
        the hex-encoded representation of the binary hash, or null if the hash is null

    - #### sha1

          public static String sha1(String string)

      Computes the sha1 value for the given string.
      Parameters:
        `string` - a non-null string
      Returns:
        the SHA1 value for the given string.

Copyright © 2008-2014 JBoss, a division of Red Hat. All Rights Reserved.