mqtt连接参数生成

 

 

 

 

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


}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Micropython 连接阿里云 MQTT 时,需要计算三元组信息以获取 MQTT 连接参数。三元组包括:设备的 ProductKey、DeviceName 和 DeviceSecret。 下面是计算 MQTT 连接参数的步骤: 1. 首先需要在阿里云 IoT 平台创建一个产品,获取 ProductKey。 2. 在该产品下创建一个设备,获取 DeviceName。 3. 生成 DeviceSecret,可以在设备详情页中进行查看或重新生成。 4. 计算 MQTT 连接参数。 首先需要将 ProductKey、DeviceName 和当前时间(精确到秒)按照以下格式拼接成一个字符串: ``` clientId = "p<productKey>.<deviceName>|timestamp=123456789" ``` 其中,`<productKey>` 为阿里云 IoT 平台中创建的产品的 ProductKey,`<deviceName>` 为该产品下创建的设备的 DeviceName,`123456789` 为当前时间戳。 然后,将 DeviceSecret 和上述字符串进行 HMAC-SHA1 加密,得到一个 20 字节的二进制字符串。 最后,将上述二进制字符串转换为 Base64 编码的字符串,即为设备的 password。 ``` import time import hashlib import hmac import base64 # 三元组信息 productKey = "your_productKey" deviceName = "your_deviceName" deviceSecret = "your_deviceSecret" # 计算时间戳 timestamp = str(int(time.time())) # 组装 clientId clientId = "p{}.{}|timestamp={}".format(productKey, deviceName, timestamp) # 计算 password content = clientId.encode("utf-8") secret = deviceSecret.encode("utf-8") signature = hmac.new(secret, content, hashlib.sha1).digest() password = base64.b64encode(signature).decode("utf-8") print("clientId:", clientId) print("password:", password) ``` 运行上述代码即可得到 clientId 和 password,用于连接阿里云 MQTT

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值