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 ); } } }
本文出自 “my dream fly on the sky” 博客,请务必保留此出处http://7915791.blog.51cto.com/7905791/1865953
原文地址:http://7915791.blog.51cto.com/7905791/1865953