@ -1,6 +1,9 @@
package com.ccsens.wechatutil.util ;
import cn.hutool.core.codec.Base64Encoder ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson.JSONObject ;
import com.ccsens.util.KCPlayerSignature ;
import javax.crypto.Mac ;
import javax.crypto.spec.SecretKeySpec ;
@ -9,9 +12,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets ;
import java.security.MessageDigest ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.TimeZone ;
import java.util.TreeMap ;
import java.util.* ;
/ * *
* @description :
@ -22,7 +23,12 @@ public class TC3Util {
private final static Charset UTF8 = StandardCharsets . UTF_8 ;
private final static String SECRET_ID = "AKIDxhBRRAdplRpwnMfnfGaeRxDBsJTN0NTI" ;
private final static String SECRET_KEY = "Zrte9MPFo68tMZU8WcXDeqnVx95rYzA6" ;
private final static String APP_KEY = "mEulfDBBOFeOjGavy" ;
private final static String APP_SECRET = "FaFKWiwVUWbJmfxVnSdd" ;
private final static String CT_JSON = "application/json; charset=utf-8" ;
//签名算法
private static final String HMAC_ALGORITHM = "HmacSHA1" ;
private static final String CONTENT_CHARSET = "UTF-8" ;
public static byte [ ] hmac256 ( byte [ ] key , String msg ) throws Exception {
Mac mac = Mac . getInstance ( "HmacSHA256" ) ;
@ -37,6 +43,72 @@ public class TC3Util {
return DatatypeConverter . printHexBinary ( d ) . toLowerCase ( ) ;
}
public static Map < String , String > getHeaders ( String service , long timestamp , String signature ) {
Map < String , String > headMap = new HashMap < > ( ) ;
String algorithm = "TC3-HMAC-SHA256" ;
String signedHeaders = "content-type;host" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
// 注意时区,否则容易出错
sdf . setTimeZone ( TimeZone . getTimeZone ( "UTC" ) ) ;
String date = sdf . format ( new Date ( Long . valueOf ( timestamp + "000" ) ) ) ;
String credentialScope = date + "/" + service + "/" + "tc3_request" ;
String authorization = algorithm + " " + "Credential=" + SECRET_ID + "/" + credentialScope + ", "
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature ;
headMap . put ( "Authorization" , authorization ) ;
return headMap ;
}
public static void main ( String [ ] args ) throws Exception {
JSONObject json = new JSONObject ( ) ;
json . put ( "Action" , "AppGetTokenByWeiXin" ) ;
json . put ( "RequestId" , "4c582f20-608a-41eb-8a04-b19cd341a328" ) ;
json . put ( "AppKey" , "mEulfDBBOFeOjGavy" ) ;
json . put ( "Timestamp" , "1638345578" ) ;
json . put ( "Nonce" , "3493" ) ;
json . put ( "WxOpenId" , "oOBSe5HAi885IlNeAAb3FQYxwExI" ) ;
json . put ( "NickName" , "wu" ) ;
json . put ( "Avatar" , "https://thirdwx.qlogo.cn/mmopen/vi_32/4KIkPtTLnuGdQPo1YqiaqRAgjyIPz4NyDzZVwwKJiatibWhszIH02XmWyqnl7LW1zYmRMbF2UYQG5o4N0cgyPH0qw/132" ) ;
System . out . println ( json ) ;
String s = generateSignature ( json ) ;
System . out . println ( "s:" + s ) ;
// String data = "Action=AppCreateCellphoneUser&AppKey=ahPxdK****TGrejd&CountryCode=86&Nonce=71087795&Password=My!P@ssword&PhoneNumber=13900000000&RequestId=8b8d499bbba1ac28b6da21b4&Timestamp=1546315200&VerificationCode=123456";
// Mac mac = Mac.getInstance(HMAC_ALGORITHM);
// SecretKeySpec secretKey = new SecretKeySpec("NcbHqk****TCGbKnQH".getBytes(), HMAC_ALGORITHM);
// mac.init(secretKey);
//
// byte[] hash = mac.doFinal(data.getBytes());
// String strSign = Base64Encoder.encode(hash);
// System.out.println("strSign:" + strSign);
}
public static String generateSignature ( Map < String , Object > param ) throws Exception {
StringBuilder builder = new StringBuilder ( ) ;
Set < String > keys = param . keySet ( ) ;
Object [ ] keyArr = keys . toArray ( ) ;
Arrays . sort ( keyArr ) ;
for ( Object keyObj : keyArr ) {
String key = ( String ) keyObj ;
if ( param . get ( key ) = = null | | "" . equals ( param . get ( key ) ) ) {
continue ;
}
builder . append ( key . replaceAll ( "_" , "." ) ) . append ( "=" ) . append ( param . get ( key ) ) . append ( "&" ) ;
}
System . out . println ( "builder:" + builder . toString ( ) ) ;
String plaintext = builder . substring ( 0 , builder . length ( ) - 1 ) ;
Mac mac = Mac . getInstance ( HMAC_ALGORITHM ) ;
SecretKeySpec secretKey = new SecretKeySpec ( APP_SECRET . getBytes ( ) , HMAC_ALGORITHM ) ;
mac . init ( secretKey ) ;
byte [ ] hash = mac . doFinal ( plaintext . getBytes ( ) ) ;
// String strSign = KCPlayerSignature.base64Encode(hash);
String strSign = Base64Encoder . encode ( hash ) ;
// strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
return strSign ;
}
public static String generateSignature ( String service , String host , long timestamp , String payload ) throws Exception {
//String service = "cvm";
//String host = "cvm.tencentcloudapi.com";
@ -74,34 +146,35 @@ public class TC3Util {
byte [ ] secretService = hmac256 ( secretDate , service ) ;
byte [ ] secretSigning = hmac256 ( secretService , "tc3_request" ) ;
String signature = DatatypeConverter . printHexBinary ( hmac256 ( secretSigning , stringToSign ) ) . toLowerCase ( ) ;
System . out . println ( signature ) ;
// ************* 步骤 4:拼接 Authorization *************
String authorization = algorithm + " " + "Credential=" + SECRET_ID + "/" + credentialScope + ", "
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature ;
TreeMap < String , String > headers = new TreeMap < String , String > ( ) ;
headers . put ( "Authorization" , authorization ) ;
headers . put ( "Content-Type" , CT_JSON ) ;
headers . put ( "Host" , host ) ;
// headers.put("X-TC-Action", action);
// headers.put("X-TC-Timestamp", timestamp);
// headers.put("X-TC-Version", version);
// headers.put("X-TC-Region", region);
StringBuilder sb = new StringBuilder ( ) ;
sb . append ( "curl -X POST https://" ) . append ( host )
. append ( " -H \"Authorization: " ) . append ( authorization ) . append ( "\"" )
. append ( " -H \"Content-Type: application/json; charset=utf-8\"" )
. append ( " -H \"Host: " ) . append ( host ) . append ( "\"" ) ;
// .append(" -H \"X-TC-Action: ").append(action).append("\"")
// .append(" -H \"X-TC-Timestamp: ").append(timestamp).append("\"")
// .append(" -H \"X-TC-Version: ").append(version).append("\"")
// .append(" -H \"X-TC-Region: ").append(region).append("\"")
if ( StrUtil . isNotEmpty ( payload ) ) {
sb . append ( " -d '" ) . append ( payload ) . append ( "'" ) ;
}
return sb . toString ( ) ;
return signature ;
// System.out.println(signature);
//
// // ************* 步骤 4:拼接 Authorization *************
// String authorization = algorithm + " " + "Credential=" + SECRET_ID + "/" + credentialScope + ", "
// + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
//
// TreeMap<String, String> headers = new TreeMap<String, String>();
// headers.put("Authorization", authorization);
// headers.put("Content-Type", CT_JSON);
// headers.put("Host", host);
//// headers.put("X-TC-Action", action);
//// headers.put("X-TC-Timestamp", timestamp);
//// headers.put("X-TC-Version", version);
//// headers.put("X-TC-Region", region);
//
// StringBuilder sb = new StringBuilder();
// sb.append("curl -X POST https://").append(host)
// .append(" -H \"Authorization: ").append(authorization).append("\"")
// .append(" -H \"Content-Type: application/json; charset=utf-8\"")
// .append(" -H \"Host: ").append(host).append("\"");
//// .append(" -H \"X-TC-Action: ").append(action).append("\"")
//// .append(" -H \"X-TC-Timestamp: ").append(timestamp).append("\"")
//// .append(" -H \"X-TC-Version: ").append(version).append("\"")
//// .append(" -H \"X-TC-Region: ").append(region).append("\"")
// if (StrUtil.isNotEmpty(payload)) {
// sb.append(" -d '").append(payload).append("'");
// }
//
// return sb.toString();
}
}