package com.goafanti.common.utils;

import java.util.Date;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import com.goafanti.common.model.Admin;
import com.goafanti.common.model.AftUser;
import com.goafanti.common.model.User;


public class PasswordUtil {
	private String algorithmName = "md5";
	private int hashIterations = 2;

	public void setAlgorithmName(String algorithmName) {
		this.algorithmName = algorithmName;
	}

	public void setHashIterations(int hashIterations) {
		this.hashIterations = hashIterations;
	}

	public void encryptPassword(User user) {
		user.setPassword(getEncryptPwd(user));
	}
	
	public void encryptPassword(Admin admin){
		admin.setPassword(getEncryptPwd(admin));
	}

	public String getEncryptPwd(User user) {
		return new SimpleHash(algorithmName, user.getPassword(), getSalt(user), hashIterations).toHex();
	}
	
	public String getEncryptPwd(Admin admin) {
		return new SimpleHash(algorithmName, admin.getPassword(), getManageSalt(admin), hashIterations).toHex();
	}

	public String getEncryptPwd(String pwd, Date date) {
		return new SimpleHash(algorithmName, pwd, getSalt(date), hashIterations).toHex();
	}

	public ByteSource getSalt(User user) {
		return ByteSource.Util.bytes(String.valueOf(user.getCreateTime().getTime()));
	}
	
	public ByteSource getManageSalt(Admin admin) {
		return ByteSource.Util.bytes(String.valueOf(admin.getCreateTime().getTime()));
	}
	
	public ByteSource getAftSalt(AftUser aftUser) {
		return ByteSource.Util.bytes(String.valueOf(aftUser.getCreateTime().getTime()));
	}

	public ByteSource getSalt(Date date) {
		return ByteSource.Util.bytes(String.valueOf(date.getTime()));
	}
}
