首页 > 其他 > 详细

MessageDigest---sha-1加密

时间:2016-10-27 02:08:17      阅读:241      评论:0      收藏:0      [点我收藏+]
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

MessageDigest---sha-1加密

原文:http://7915791.blog.51cto.com/7905791/1865953

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!