阿里雲IOT 用MQTT.fx 連接 生成用戶密碼

 @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()));

    }

   
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章