swing 窗体小工具版本 https://blog.csdn.net/jintaocccq/article/details/108364751
@Test
public void re() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
String productKey = "";
String deviceName = "";
String deviceSecret = "";
String clientId = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String region = "cn-shanghai";
String data = "clientId" + clientId + "deviceName" + deviceName + "productKey" + productKey;
System.out.println("address : " + productKey + ".iot-as-mqtt." + region + ".aliyuncs.com");
System.out.println("clientId : " + clientId + "|securemode=3,signmethod=hmacsha1|");
System.out.println("userName : " + deviceName + "&" + productKey);
System.out.println("passwd : " + SignUtil.re(data, deviceSecret).toUpperCase());
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
/**
* @author ljt
* @version 1.0
* @date 2019/9/3 16:03
*/
public class SignUtil {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String re(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeyException, NoSuchAlgorithmException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
}