package com.dw.lib;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.UUID;

public class CryptoGenerator {
	
	public enum CryptoType {
		MD5,
		SHA_1,
		SHA_256,
	}
	
	public final static String md5 = "MD5";
	public final static String sha_1 = "SHA-1";
	public final static String SHA_256 = "SHA-256";
	String algorithmType = "";
	
	public CryptoGenerator() {
		algorithmType = md5;
	}
	
	public CryptoGenerator(CryptoType cryptoType) {
		if(cryptoType== CryptoType.MD5) {
			algorithmType = md5;
		} else if(cryptoType== CryptoType.SHA_1) {
			algorithmType = sha_1;
		} else if(cryptoType== CryptoType.SHA_256) {
			algorithmType = SHA_256;
		}
	}

	public String generateValue() throws Exception {
		return generateValue(UUID.randomUUID().toString());
	}

	private static final char[] hexCode = "0123456789abcdef".toCharArray();

	public static String toHexString(byte[] data) {
		if (data == null) {
			return null;
		}
		StringBuilder r = new StringBuilder(data.length * 2);
		for (byte b : data) {
			r.append(hexCode[(b >> 4) & 0xF]);
			r.append(hexCode[(b & 0xF)]);
		}
		return r.toString();
	}

	public String generateValue(String param) throws Exception {
		try {
			MessageDigest algorithm = MessageDigest.getInstance(algorithmType);
			algorithm.reset();
			algorithm.update(param.getBytes());
			byte[] messageDigest = algorithm.digest();
			return toHexString(messageDigest);
		} catch (Exception e) {
			throw new Exception("OAuth Token cannot be generated.", e);
		}
	}
	
	public String generateFileValue(String filePath) throws Exception {
		try {
			MessageDigest algorithm = MessageDigest.getInstance(algorithmType);
			algorithm.reset();
			algorithm.update(Files.readAllBytes(Paths.get(filePath)));
			byte[] messageDigest = algorithm.digest();
			return toHexString(messageDigest);
		} catch (Exception e) {
			throw new Exception("Generate MD5 of file show exceptions: ", e);
		}
	}
	
	public static void main(String[] args) {
		CryptoGenerator m = new CryptoGenerator(CryptoGenerator.CryptoType.MD5);
		try {
			System.out.println(m.generateFileValue("/Users/ewu/test/1.json"));
			System.out.println(m.generateValue("1234"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -106,16 +106,78 @@
 Digest;%0A
+import java.security.Provider;%0Aimport java.security.Security;%0A
 import j
@@ -2018,32 +2018,735 @@
 %22, e);%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09public void printSupportAlgorithm() %7B%0A%09%09System.out.println(%22Algorithms Supported in this JCE.%22);%0A        System.out.println(%22====================%22);%0A        // heading%0A        System.out.println(%22Provider: type.algorithm -%3E className%22 + %22%5Cn  aliases:%22 + %22%5Cn  attributes:%5Cn%22);%0A        // discover providers%0A        Provider%5B%5D providers = Security.getProviders();%0A        for (Provider provider : providers) %7B%0A            System.out.println(%22%3C%3E%3C%3E%3C%3E%22 + provider + %22%3C%3E%3C%3E%3C%3E%5Cn%22);%0A            // discover services of each provider%0A            for (Provider.Service service : provider.getServices()) %7B%0A                System.out.println(service);%0A            %7D%0A            System.out.println();%0A        %7D%0A%09%7D%0A%09%0A
 %09public static v

add support algorithm