利用Apache shiro SimpleHash 加密字符串

原文:利用Apache shiro SimpleHash 加密字符串

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 散列算法 生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,
 */
public class MD5Utils {
    //干扰数据 盐 防破解
    private static final String SALT = "mar%#$@";
    //散列算法类型为MD5
    private static final String ALGORITH_NAME = "md5";
    //hash的次数
    private static final int HASH_ITERATIONS = 2;

    public static String encrypt(String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
        return newPassword;
    }

    public static String encrypt(String username, String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
                HASH_ITERATIONS).toHex();
        return newPassword;
    }
    
    public static void main(String[] args) {        
        System.out.println(MD5Utils.encrypt("test", "123456"));
    }

}

 

posted @ 2019-07-05 15:37  山水花草  阅读(1728)  评论(0编辑  收藏  举报