package com.neusoft.smart.pos.payment.utils;
/**
 * Created by dsq on 2017/6/15.
 */

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChinaumsSign {
    public static String appId = "f0ec96ad2c3848b5b810e7aadf369e2f";
    public static String timestamp = "20180730101110";
    public static String nonce = "1731378";
    public static String body = "{\n" +
            "\t\"merchantCode\":\"123456789900081\",\n" +
            "\t\"terminalCode\":\"00810001\",\n" +
            "\t\"transactionAmount\":1,\n" +
            "\t\"transactionCurrencyCode\":\"156\",\n" +
            "\t\"merchantOrderId\":\"test000006\",\n" +
            "\t\"merchantRemark\":\"东软\",\n" +
            "\t\"payMode\":\"CODE_SCAN\",\n" +
            "\t\"payCode\":\"135022034912784444\"\n" +
            "}";
    public static byte[] data = body.getBytes();
    public static InputStream is = new ByteArrayInputStream(data);
    public static String str1_C = appId+timestamp+nonce+testSHA256(is);
    public static String appKey_D = "775481e2556e4564985f5439a5e6a277";

    private static String testSHA256(InputStream is){
        try {
            return DigestUtils.sha256Hex(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String testSHA256_STRING(String str){
        return DigestUtils.sha256Hex(str);
    }

    public static byte[] hmacSHA256(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException {
        String algorithm = "HmacSHA256";
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(key, algorithm));
        return mac.doFinal(data);
    }
//    public static void main(String[] args) throws Exception {
//        byte[] data = body.getBytes();
//        InputStream is = new ByteArrayInputStream(data);
//        System.out.println("bodyDigest:\n" + testSHA256(is));
//
//        System.out.println("str1_C:\n" + str1_C);
//
//        System.out.println("appKey_D:\n" + appKey_D);
//
//        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
//        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
//        System.out.println("timestamp:\n" + date);
//
//        byte[] localSignature = hmacSHA256(str1_C.getBytes(), appKey_D.getBytes());
//        String localSignatureStr = Base64.encodeBase64String(localSignature);
//        System.out.println("signature:\n" + localSignatureStr);
//
//        System.out.println("Authorization:\n" + "OPEN-BODY-SIG AppId="+"\""+appId+"\""+", Timestamp="+"\""+timestamp+"\""+", Nonce="+"\""+nonce+"\""+", Signature="+"\""+localSignatureStr+"\"");
//    }
    
    public static String sign(String msgBody, String appId, String appKey) {
    	try{
	    	byte[] data = msgBody.getBytes();
	    	InputStream is = new ByteArrayInputStream(data);
	    	SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
	        String timestamp = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
	    	String str1_C = appId+timestamp+nonce+testSHA256(is);
	    	byte[] localSignature = hmacSHA256(str1_C.getBytes(), appKey.getBytes());
	    	String localSignatureStr = Base64.encodeBase64String(localSignature);
	    	String Authorization = "OPEN-BODY-SIG AppId="+"\""+appId+"\""+", Timestamp="+"\""+timestamp+"\""+", Nonce="+"\""+nonce+"\""+", Signature="+"\""+localSignatureStr+"\"";
	    	return Authorization;
    	}catch (Exception e){
    		return "generate sign fail";
    	}
    }
}
