import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * sha-1工具
 */
public class Sha1Util {
     /**
      * 计算字符串的sha-1串
      */
     public static byte[] sha1( String str ){
          try {
              return sha1( str.getBytes( "utf-8" ) );
          } catch (UnsupportedEncodingException e) {
              throw new RuntimeException( e );
          }
     }
     
     /**
      * 计算文件的sha-1串
      */
     public static byte[] sha1( File file ){
          byte[] bytes = new byte[ (int)file.length() ];
          try {
              FileInputStream fis = new FileInputStream( file );
              fis.read( bytes );
              fis.close();
          } catch (IOException e) {
              throw new RuntimeException( e );
          }
          
          return sha1( bytes );
     }
     
     /**
      * 计算字节数组的sha-1串
      */
     public static byte[] sha1( byte[] bytes ){
          try {
              MessageDigest messageDigest = MessageDigest.getInstance( "SHA-1" );
              byte[] bs = messageDigest.digest( bytes );
              return bs;
          } catch (NoSuchAlgorithmException e) {
              throw new RuntimeException( e );
          }
     }
}