码迷,mamicode.com
首页 > 编程语言 > 详细

java中MessageDigest加密工具类

时间:2015-02-11 16:04:48      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

import java.security.MessageDigest;

public class EncryptionKit {
    
    public static String md5Encrypt(String srcStr){
        return encrypt("MD5", srcStr);
    }
    
    public static String sha1Encrypt(String srcStr){
        return encrypt("SHA-1", srcStr);
    }
    
    public static String sha256Encrypt(String srcStr){
        return encrypt("SHA-256", srcStr);
    }
    
    public static String sha384Encrypt(String srcStr){
        return encrypt("SHA-384", srcStr);
    }
    
    public static String sha512Encrypt(String srcStr){
        return encrypt("SHA-512", srcStr);
    }
    
    public static String encrypt(String algorithm, String srcStr) {
        try {
            StringBuilder result = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
            for (byte b :bytes) {
                String hex = Integer.toHexString(b&0xFF);
                if (hex.length() == 1)
                    result.append("0");
                result.append(hex);
            }
            return result.toString();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

java中MessageDigest加密工具类

标签:

原文地址:http://www.cnblogs.com/smile361/p/4286131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!