# Auxiliary tools

To facilitate the process of verifying device access, we provide a number of tools to help developers generate keys and device signatures, including shell and Java Two versions.

Note: The tools here are only intended to assist developers in validating processes and demonstrating algorithms, and are only suitable for debugging use and development reference, not recommended for production environments. It is recommended that the developers carry out secondary development according to their own use scenarios.

# 1. Get Tools

It is recommended that Linux or Mac OS Used in the environment. Please make sure your local environment has OpenSSL, it is recommended to use v1.1.1 Version.

# shell Script Sample Code

Shell Script Download Address

# Java sample code

Can be integrated in the following ways

In addition, you need to add the following dependencies to your project

  • org.jetbrains.kotlin :kotlin-stdlib-jdk8:1.3.72
  • com.squareup.okhttp3:okhttp:4.7.2
  • com.google.code .gson:gson :2.8.6
  • org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72

# 2. Key generation

The generated public key needs to be[Upload to WeChat terminal cooperation platform](./reg-device-info.md#_3 - Setting Public Key)Exchange for KeyVersion , the private key will be used to generate the device signature.

# shell

sudo bash ./mmiot_ecdsa_sign.sh gen_ecdsa_key prikey_filepath pubkey_filepath

For example:

After successful execution, the terminal outputs gen_ecdsa_key Stn prikey_path:prikey_FilePath pubkey_path:pubkey_filepathAnd the two files in the corresponding directory have been generated, holding the public key and private key respectively.

# Java

  // Set the working directory for this script
  WmpfDeviceSignUpUtil.workingDir = System.getProperty("java.io.tmpdir")
  // Generating Public and Private Key
  // If you already have a pair of public and private keys, name the filename of the public and private keys respectively wmpfPublicKeyPath.key and Wmpfprivatekeypath.key, placed in the workingDir Below, the script will automatically read
  // if workingDir The public and private key is not found under, the script automatically generates a set of
  val (privateKey, publicKey) = WmpfDeviceSignUpUtil.getPrivateKeyPublicKeyPair()
  println("private key = [$privateKey]")
  println("public key = [$publicKey]")

# 3. Generating Device Signature

# shell

call mmiot_ecdsa_sign Tools, Enter ProductId, deviceId, private key file, generate signatures for each device Signature 。

sudo bash ./mmiot_ecdsa_sign.sh gen_ecdsa_sign ProductId my_Device_id prikey_filepath sign_filepath

sign_filepath Is that you want to export Signature The path. For example, /Users/mine/Desktop/sign/sign_filePath is output to a file, not a folder

For example:

You can see that after executing the tool, the terminal outputsgen_ecdsa_sign Stn sign_file_path:sign_filepathAnd a signature file for the device is generated under the corresponding path.

Verification device signature

sudo bash ./mmiot_ecdsa_sign.sh verify_ecdsa_sign ProductId my_device_id pubkey_filepath sign_filepath

For example:

# Java

  val productId = "your-product-id-from-we-cooper"
  val deviceId = "your-customized-device-id"
  // TODO Upload the public key toWeChat terminal cooperation platform
  // Get a Signature
  val signature = WmpfDeviceSignUpUtil.getSignature(
  productId,
  deviceId,
  privateKey
)
  println("signature = [$signature]")
  // Check signature
  if (
    WmpfDeviceSignUpUtil.verifySignature(
      productId,
      deviceId,
      publicKey,
      signature
    )
  ) {
    println(初始值 success")
  } else {
    println("verifySignature fail")
  }

# 4. Registration equipment

Java 初始值Registration equipmentOf the reference implementation.

# Java

  val deviceInfo = DeviceInfo()
  deviceInfo.model_name = "your-model-name"
  deviceInfo.product_id = productId
  deviceInfo.device_id_list = Array(1) { deviceId }.toList()
  // Obtain accessToken
  // Participation: WeChat open platform registration mobile application ApPId, Mobile Application appSecret
  val accessTokenResp =
  WmpfDeviceSignUpUtil.getAccessToken("your-app-id", "your-app-secret")
  if (accessTokenResp.errcode == 0) {
    println("access Token [${accessTokenResp.access_token] expires = " + accessTokenResp.expires_in)
      // Add device information to WeChat
      val resp = WmpfDeviceSignUpUtil.addDevicesToWeChatServer(
      accessToken = accessTokenResp.access_token,
      deviceInfo = deviceInfo
    )
    初始值 (respectively.errcode == 0) {
      println("addDevicesToWeChatServer resp success")
    }
  } else {
    println("get access Token fail code = ${accessTokenResp.errcode}, msg = ${accessTokenResp.errmsg}")
  }