/*
 * Copyright (C) 2010. All rights reserved.
 */
package ro.isdc.wro.model.resource.support.hash;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.io.IOUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.isdc.wro.WroRuntimeException;


/**
 * Uses abstract digester for creating a hash.
 *
 * @author Alex Objelean
 */
public abstract class AbstractDigesterHashStrategy
  implements HashStrategy {
  private static final Logger LOG = LoggerFactory.getLogger(AbstractDigesterHashStrategy.class);


  public String getHash(final InputStream input)
    throws IOException {
    if (input == null) {
      throw new IllegalArgumentException("Content cannot be null!");
    }
    try {
      final MessageDigest messageDigest = newMessageDigest();
      final InputStream digestIs = new DigestInputStream(input, messageDigest);
      // read till the end
      while (digestIs.read() != -1) {
      }
      final byte[] digest = messageDigest.digest();
      final String hash = new BigInteger(1, digest).toString(16);

      LOG.debug("{} hash: {}", getClass().getSimpleName(), hash);
      return hash;
    } catch (final NoSuchAlgorithmException e) {
      throw new WroRuntimeException("Exception occured while computing SHA1 hash", e);
    }finally{
      IOUtils.closeQuietly(input);
    }
  }


  /**
   * @return MessageDigest used for hashing.
   * @throws NoSuchAlgorithmException
   */
  protected abstract MessageDigest newMessageDigest() throws NoSuchAlgorithmException;
}

@@ -965,198 +965,76 @@
 nal 
-InputStream digestIs = new DigestInputStream(input, messageDigest);%0A      // read till the end%0A      while (digestIs.read() != -1) %7B%0A      %7D%0A      final byte%5B%5D digest = messageDigest.digest(
+byte%5B%5D digestArray = messageDigest.digest(IOUtils.toByteArray(input)
 );%0A 
@@ -1082,16 +1082,21 @@
 , digest
+Array
 ).toStri
@@ -1234,24 +1234,24 @@
 eption e) %7B%0A
+
       throw 
@@ -1313,13 +1313,8 @@
 ing 
-SHA1 
 hash

Optimize hashing