package com.neusoft.smart.pos.framework.utils;

import java.security.MessageDigest;

public class HashUtils {

	/**
	 * 生成 SHA256
	 *
	 * @param data 待处理数据
	 * @return SHA256结果
	 */
	public static final  String ALG_SHA256 = "SHA-256";
	public static final  String ALG_MD5 = "MD5";
	public static final  String FMT_UTF8 = "UTF-8";

	public static String SHA256(String data) {
		return Hash(data, "UTF-8", ALG_SHA256);
	}

	public static String MD5(String data) {
		return Hash(data, "UTF-8", ALG_SHA256);
	}

	public static String Hash(String data, String fmt, String alg){
		try {
			MessageDigest md =  MessageDigest.getInstance(alg);
			byte[] array = md.digest(data.getBytes(fmt));
			return StringUtils.bytesToHex(array);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
