Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.amazonaws.services.glacier;
 
 import java.io.File;
 import java.util.List;
 
Utility class for calculating tree hashes.

For more information on tree hashing, see http://en.wikipedia.org/wiki/Hash_tree.

 
 public class TreeHashGenerator {

Calculates a hex encoded binary hash using a tree hashing algorithm for the data in the specified file.

Parameters:
file The file containing the data to hash.
Returns:
The hex encoded binary tree hash for the data in the specified file.
Throws:
com.amazonaws.AmazonClientException If any problems were encountered reading the data or computing the hash.
 
     public static String calculateTreeHash(File file)
             throws AmazonClientException {
         ResettableInputStream is = null;
         try {
             is = new ResettableInputStream(file);
             return calculateTreeHash(is);
         } catch (IOException e) {
             throw new AmazonClientException("Unable to compute hash for file: "
                     + file.getAbsolutePath(), e);
         } finally {
             if (is != null)
                 is.release();
         }
     }

Calculates a hex encoded binary hash using a tree hashing algorithm for the data in the specified input stream. The method will consume all the inputStream and close it when returned.

Parameters:
input The input stream containing the data to hash.
Returns:
The hex encoded binary tree hash for the data in the specified input stream.
Throws:
com.amazonaws.AmazonClientException If problems were encountered reading the data or calculating the hash.
 
     public static String calculateTreeHash(InputStream input)
             throws AmazonClientException {
         try {
             TreeHashInputStream treeHashInputStream =
                 new TreeHashInputStream(input);
             byte[] buffer = new byte[1024];
             while (treeHashInputStream.read(buffer, 0, buffer.length) != -1);
             // closing is currently required to compute the checksum 
             treeHashInputStream.close();
             return calculateTreeHash(treeHashInputStream.getChecksums());
         } catch (Exception e) {
             throw new AmazonClientException("Unable to compute hash"e);
         }
     }

Returns the hex encoded binary tree hash for the individual checksums given. The sums are assumed to have been generated from sequential 1MB portions of a larger file, with the possible exception of the last part, which may be less than a full MB.

Returns:
The combined hex encoded binary tree hash for the individual checksums specified.
Throws:
com.amazonaws.AmazonClientException If problems were encountered reading the data or calculating the hash.
    public static String calculateTreeHash(List<byte[]> checksumsthrows AmazonClientException {
        /*
         * The tree hash algorithm involves concatenating adjacent pairs of
         * individual checksums, then taking the checksum of the resulting bytes
         * and storing it, then recursing on this new list until there is only
         * one element. Any final odd-numbered parts at each step are carried
         * over to the next iteration as-is.
         */
        List<byte[]> hashes = new ArrayList<byte[]>();
        hashes.addAll(checksums);
        while ( hashes.size() > 1 ) {
            List<byte[]> treeHashes = new ArrayList<byte[]>();
            for ( int i = 0; i < hashes.size() / 2; i++ ) {
                byte[] firstPart = hashes.get(2 * i);
                byte[] secondPart = hashes.get(2 * i + 1);
                byte[] concatenation = new byte[firstPart.length + secondPart.length];
                System.arraycopy(firstPart, 0, concatenation, 0, firstPart.length);
                System.arraycopy(secondPart, 0, concatenationfirstPart.lengthsecondPart.length);
                try {
					treeHashes.add(computeSHA256Hash(concatenation));
catch (Exception e) {
					throw new AmazonClientException("Unable to compute hash"e);
				}
            }
            if ( hashes.size() % 2 == 1 ) {
                treeHashes.add(hashes.get(hashes.size() - 1));
            }
            hashes = treeHashes;
        }
        return BinaryUtils.toHex(hashes.get(0));
    }
    
    private static byte[] computeSHA256Hash(byte[] datathrows NoSuchAlgorithmExceptionIOException {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data));
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] buffer = new byte[16384];
            int bytesRead = -1;
            while ( (bytesRead = bis.read(buffer, 0, buffer.length)) != -1 ) {
                messageDigest.update(buffer, 0, bytesRead);
            }
            return messageDigest.digest();
        } finally {
            try { bis.close(); } catch ( Exception e ) {}
        }
    }
New to GrepCode? Check out our FAQ X