
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;
import java.util.Scanner;
import java.util.UUID;
public class AutoIotPram {
public static void main(String[] args) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
Scanner input = new Scanner(System.in);
System.out.print("请输入【productKey】 :");
String productKey = input.nextLine();
while (productKey == null || "".equals(productKey)){
System.out.print("请输入【productKey】 :");
productKey = input.nextLine();
}
System.out.print("请输入【deviceName】 :");
String deviceName = input.nextLine();
while (deviceName == null || "".equals(deviceName)){
System.out.print("请输入【deviceName】 :");
deviceName = input.nextLine();
}
System.out.print("请输入【deviceSecret】:");
String deviceSecret = input.nextLine();
while (deviceSecret == null || "".equals(deviceSecret)){
System.out.print("请输入【deviceSecret】 :");
deviceSecret = input.nextLine();
}
System.out.println();
System.out.println("生成mqtt连接参数中....");
System.out.println();
String clientId = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String region = "cn-shanghai";
String data = "clientId" + clientId + "deviceName" + deviceName + "productKey" + productKey;
System.out.println("port : 1883");
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 : " + re(data, deviceSecret).toUpperCase());
System.out.println();
System.out.println("是否退出(Y/n):");
String str = input.nextLine();
while (!"Y".equals(str.toUpperCase())){
System.out.println("是否退出(Y/n):");
str = input.nextLine();
}
System.exit(0);
}
private static String re(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeyException {
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()));
}
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();
}
}