package com.xmomen.module.account.service;

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;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-1-28
 * <p>Version: 1.0
 */
public class PasswordHelper {

    // 随机数生成器
    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    // 加密算法
    private String algorithmName = "md5";
    // 加密次数
    private int hashIterations = 2;

    // 设置随机数生成器
    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
        this.randomNumberGenerator = randomNumberGenerator;
    }

    // 设置加密算法
    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    // 设置加密次数
    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    // 生成随机盐
    public String getSalt(){
        return randomNumberGenerator.nextBytes().toHex();
    }

    // 加密密码
    public String encryptPassword(String password, String salt) {
        return new SimpleHash(
                algorithmName,
                password,
                ByteSource.Util.bytes(salt),
                hashIterations).toHex();

    }
}
