public String generateDigest ( InputStream stream ) { MessageDigest digest = null ; try { digest = MessageDigest . getInstance ( DIGEST_ALGORITHM_SHA1 ) ; } catch ( NoSuchAlgorithmException ae ) { throw new BoxAPIException ( "Digest algorithm not found" , ae ) ; } //Calcuate the digest using the stream. DigestInputStream dis = new DigestInputStream ( stream , digest ) ; try { int value = dis . read ( ) ; while ( value != - 1 ) { value = dis . read ( ) ; } } catch ( IOException ioe ) { throw new BoxAPIException ( "Reading the stream failed." , ioe ) ; } //Get the calculated digest for the stream byte [ ] digestBytes = digest . digest ( ) ; return Base64 . encode ( digestBytes ) ; }
Generates the Base64 encoded SHA - 1 hash for content available in the stream . It can be used to calculate the hash of a file .