package cn.superdesk.uniorder.common.security.crypt.algorithm;

import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * @Description: ## 开放接口调用说明 ### 签名机制
 * @Author: linjinyu
 * @CreateDate: 2021/10/28 下午2:30
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/28 下午2:30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class SHA1 {

	/**
	 * 用SHA1算法生成安全签名
	 *
	 * @param token     票据
	 * @param timestamp 时间戳
	 * @param nonce     随机字符串
	 * @param encrypt   密文
	 * @return 安全签名
	 * @throws UnifiedOrderRuntimeException {@link UnifiedOrderRuntimeException}
	 */
	public static String getSHA1(String token, String timestamp, String nonce, String encrypt) {

		try {

			String[] array = new String[]{
					token,
					timestamp,
					nonce,
					encrypt
			};

			StringBuffer stringBuffer = new StringBuffer();

			// 1. 请求参数按照 ASCII 码表的顺序进行排列 字符串排序
			Arrays.sort(array);
			for (int i = 0; i < 4; i++) {
				stringBuffer.append(array[i]);
			}

			/* SHA1签名生成 */
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			md.update(stringBuffer.toString().getBytes());
			byte[] digest = md.digest();

			StringBuffer hexstr = new StringBuffer();
			String       shaHex = "";

			for (int i = 0; i < digest.length; i++) {
				shaHex = Integer.toHexString(digest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexstr.append(0);
				}
				hexstr.append(shaHex);
			}

			return hexstr.toString();

		} catch (Exception e) {
			throw new UnifiedOrderRuntimeException(500, ExceptionUtils.getMessage(e));
		}
	}

	@Deprecated
	public static String getSuperdeskCloudOpenDataCallOpiSign(
			String appKey,
			String timestamp,
//			String sign,
			String v,
			String format,
			String locale,
			String appSecret
	) {
		try {

			String[] paramsArray = new String[]{
//					"appKey+" + appKey,
//					"timestamp+" + timestamp,
////					sign,
//					"v+" + v,
//					"format+" + format,
//					"locale+" + locale

					"appKey" + appKey,
					"timestamp" + timestamp,
//					sign,
					"v" + v,
					"format" + format,
					"locale" + locale
			};

			StringBuffer stringBuffer = new StringBuffer();

			// 1. 请求参数按照 ASCII 码表的顺序进行排列 字符串排序
			Arrays.sort(paramsArray);

			stringBuffer.append(appSecret).append("");
			for (int i = 0; i < paramsArray.length; i++) {
				stringBuffer.append(paramsArray[i]);
			}
			stringBuffer.append("").append(appSecret);



			/* SHA1签名生成 */
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			md.update(stringBuffer.toString().getBytes());
			byte[] digest = md.digest();

			StringBuffer hexstr = new StringBuffer();
			String       shaHex = "";

			for (int i = 0; i < digest.length; i++) {
				shaHex = Integer.toHexString(digest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexstr.append(0);
				}
				hexstr.append(shaHex);
			}

			return hexstr.toString();

		} catch (Exception e) {
			throw new UnifiedOrderRuntimeException(500, ExceptionUtils.getMessage(e));
		}
	}
}
