package com.iformall.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * @author Stormeye
 * @since 2018.08.09
 */
public class HashUtil {
	private static final java.security.SecureRandom random = new java.security.SecureRandom();
	private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
	private static final char[] CHAR_ARRAY = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
			.toCharArray();

	public static String md5(String srcStr) {
		return hash("MD5", srcStr);
	}

	public static String sha1(String srcStr) {
		return hash("SHA-1", srcStr);
	}

	public static String sha256(String srcStr) {
		return hash("SHA-256", srcStr);
	}

	public static String sha384(String srcStr) {
		return hash("SHA-384", srcStr);
	}

	public static String sha512(String srcStr) {
		return hash("SHA-512", srcStr);
	}

	public static String hash(String algorithm, String srcStr) {
		try {
			MessageDigest md = MessageDigest.getInstance(algorithm);
			byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
			return toHex(bytes);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private static String toHex(byte[] bytes) {
		StringBuilder ret = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
			ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
		}
		return ret.toString();
	}

	/**
	 * md5 128bit 16bytes sha1 160bit 20bytes sha256 256bit 32bytes sha384
	 * 384bit 48bytes sha512 512bit 64bytes
	 */
	public static String generateSalt(int saltLength) {
		StringBuilder salt = new StringBuilder();
		for (int i = 0; i < saltLength; i++) {
			salt.append(CHAR_ARRAY[random.nextInt(CHAR_ARRAY.length)]);
		}
		return salt.toString();
	}

	public static String generateSaltForSha256() {
		return generateSalt(32);
	}

	public static String generateSaltForSha512() {
		return generateSalt(64);
	}

	public static boolean slowEquals(byte[] a, byte[] b) {
		if (a == null || b == null) {
			return false;
		}

		int diff = a.length ^ b.length;
		for (int i = 0; i < a.length && i < b.length; i++) {
			diff |= a[i] ^ b[i];
		}
		return diff == 0;
	}

	public static String EncoderByMd5(String str) {
		if (str == null) {
			return null;
		}
		try {
			String enc = hash("md5", str);
			enc = new String(Base64.getUrlEncoder().encode(enc.getBytes("utf-8")), "utf-8");
			return enc;
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	public static String DecodeByMD5(String str) {
		if (str == null) {
			return null;
		}
		try {
			String dec = new String(Base64.getUrlDecoder().decode(str), "utf-8");
			return dec;
		} catch (Exception e) {
			System.out.println(str);
			System.out.println(e.getMessage());
			return null;
		}
	}
}
