package cn.citycraft.AuthMe.security.crypts;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import cn.citycraft.AuthMe.AuthMe;

public class PHPFUSION implements EncryptionMethod {

	private static String getSHA1(String message) throws NoSuchAlgorithmException {
		MessageDigest sha1 = MessageDigest.getInstance("SHA1");
		sha1.reset();
		sha1.update(message.getBytes());
		byte[] digest = sha1.digest();
		return String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
	}

	@Override
	public boolean comparePassword(String hash, String password, String playerName) throws NoSuchAlgorithmException {
		String salt = AuthMe.getInstance().database.getAuth(playerName).getSalt();
		return hash.equals(getHash(password, salt, ""));
	}

	@Override
	public String getHash(String password, String salt, String name) throws NoSuchAlgorithmException {
		String digest = null;
		String algo = "HmacSHA256";
		String keyString = getSHA1(salt);
		try {
			SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
			Mac mac = Mac.getInstance(algo);
			mac.init(key);
			byte[] bytes = mac.doFinal(password.getBytes("ASCII"));
			StringBuffer hash = new StringBuffer();
			for (int i = 0; i < bytes.length; i++) {
				String hex = Integer.toHexString(0xFF & bytes[i]);
				if (hex.length() == 1) {
					hash.append('0');
				}
				hash.append(hex);
			}
			digest = hash.toString();
		} catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) {
			// ingore
		}

		return digest;
	}

}
