package com.kiisoo.ic.utils;

import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 密码加密类
 * @author Arvin
 *
 */
public class PasswordEncry {

    /** 指定散列算法为md5 */
	public static String algorithmName = "MD5";
    
    /** 散列迭代次数 */
    public final static int hashIterations = 2;
    
    /** 随机数生成器 */
    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

    /**
     * 生成随机盐值对密码进行加密
     * @param userDO  登录识别串（用户名）
     * @return
     */
    public PrivilageAccountDO encrypt(PrivilageAccountDO userDO) {
    	userDO.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(algorithmName,userDO.getPassword(), ByteSource.Util.bytes(userDO.getCredentialsSalt()),hashIterations).toHex();
        userDO.setPassword(newPassword);
        return userDO;
    }
}

