JAVA
## JAVA SDK
此sdk正在构建中,签名的源码如下:
```JAVA
package com.tuochee.common.utils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* 拖车易签名类
*/
public class Auth {
//前缀
private static final String PREFIX = "TCE ";
/**
* 签名
* @param ak 公钥
* @param sk 私钥
* @return 签名
*/
public static String Sign(String ak, String sk) throws Exception {
//1获取UTC时间戳
Calendar cal = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("GMT");
cal.setTimeZone(tz);
String unixTimes= String.valueOf(cal.getTimeInMillis()/1000);// 返回的UTC时间戳
//2.使用sk进行HMAC-SHA256签名
String signed = HMACSHA256(unixTimes, sk);
//3.把PREFIX,AK,时间戳,sk组合成签名。
//生成的token有效期为30分钟
String accessToken = PREFIX+ak+":"+unixTimes+":"+signed;
return accessToken;
}
/**
* 生成 HMACSHA256
* @param data 待处理数据
* @param key 密钥
* @return 加密结果
* @throws Exception
*/
private static String HMACSHA256(String data, String key) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
}
```