我是靠谱客的博主 幸福大树,最近开发中收集的这篇文章主要介绍CAT1|mqtt对接onenet平台-token计算(c语言),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

token的计算是使用纯C语言实现,可以移植到任何平台。工程采用CodeBlocks编译

前言

onenet平台已经做了进一步的完善,各个功能都很齐全。现在使用mqtt进行对接

对接信息

1.服务器地址

连接协议证书地址端口说明
MQTT证书下载studio-mqtt.heclouds.com1883非加密端口接入
MQTTS证书下载studio-mqtts.heclouds.com8883加密端口接入

2.安全认证

参数是否必须参数说明
clientId设备名称
username平台分配的产品ID
password填写经过 key 计算的 token

平台的连接难点就在于token的计算。目前官网没有给出token计算的c语言方式

Token计算

1.参考文档

token计算公式icon-default.png?t=M276https://open.iot.10086.cn/doc/v5/develop/detail/624

2.代码实现-纯C语言

代码的来源也是网络,在原基础上进行了修改测试,如果冒犯,请第一时间联系!

#ifndef _CIPHER_TOKEN_H
#define _CIPHER_TOKEN_H
#ifdef  __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdint.h>

typedef enum {
    ONENET_METHOD_MD5 = 0,
    ONENET_METHOD_SHA1,
    ONENET_METHOD_SHA256,
}method_t;

typedef struct onenet_msg
{
    char produt_id[32];
    char device_name[32];
    char key[128];
}onenet_msg_t;

int onenet_creat_token_init(onenet_msg_t* msg, long long time,method_t token_method,char *token,int maxlen);


#ifdef  __cplusplus
}
#endif /* __cplusplus */
#endif /* _CIPHER_SHA256_H */
#include "token.h"
#include "base64.h"
#include "hmac.h"
#include "md5.h"
#include "sha.h"
#include "sha1.h"
#include "sha256.h"


typedef struct {
    char  et[32];
    char* version;
    char* method;
    char res[128];
    char sign[128];
} sign_msg;


typedef  struct {
    char* old_str;
    char* str;
}URL_PARAMETES;


static int url_encoding_for_token(sign_msg* msg,char *token,int maxlen)
{
    int i,j,k,slen;
    sign_msg* temp_msg = msg;
    URL_PARAMETES url_patametes[] = {
       {"+","%2B"},
       {" ","%20"},
       {"/","%2F"},
       {"?","%3F"},
       {"%","%25"},
       {"#","%23"},
       {"&","%26"},
       {"=","%3D"},
    };
    char temp[128]     = {0};
    /**
     * @brief res ½øÐÐurl±àÂë
     *
     */
    slen = strlen(temp_msg->res);
    for (i = 0,j = 0; i < slen; i++) {
        for(k = 0; k < 8; k++){
            if(temp_msg->res[i] == url_patametes[k].old_str[0]) {
                memcpy(&temp[j],url_patametes[k].str,strlen(url_patametes[k].str));
                j+=3;
                break;
            }
        }
        if (k == 8) {
            temp[j++] = temp_msg->res[i];
        }

	}
    memcpy(temp_msg->res,temp,strlen(temp));
    temp_msg->res[strlen(temp)] = 0;

    /**
     * @brief sign ½øÐÐurl ±àÂë
     *
     */
    memset(temp,0x00,sizeof(temp));
    slen = strlen(temp_msg->sign);
    for (i = 0,j = 0; i < slen; i++) {
        for(k = 0; k < 8; k++){
            if(temp_msg->sign[i] == url_patametes[k].old_str[0]) {
                memcpy(&temp[j],url_patametes[k].str,strlen(url_patametes[k].str));
                j+=3;
                break;
            }
        }
        if(k == 8)
        {
            temp[j++] = temp_msg->sign[i];
        }
	}
    memcpy(temp_msg->sign,temp,strlen(temp));
    temp_msg->sign[strlen(temp)] = 0;

    sprintf(token, "version=%s&res=%s&et=%s&method=%s&sign=%s", temp_msg->version, temp_msg->res, temp_msg->et, temp_msg->method, temp_msg->sign);
    return strlen(token);
}


int onenet_creat_token_init(onenet_msg_t* msg, long long time,method_t token_method,char *token,int maxlen)
{
    int declen = 0, enclen =  0;
    char plaintext[64]     = { 0 };
    char hmac[64]          = { 0 };
    sign_msg sign ;
    memset(&sign,0x00,sizeof(sign));
    sign.version = "2018-10-31";
    sprintf(sign.et,"%lld",time);
    sprintf(sign.res,"products/%s/devices/%s",msg->produt_id,msg->device_name);
    mbedtls_base64_decode((unsigned char*)plaintext, sizeof(plaintext), &declen, (unsigned char*)msg->key, strlen((char*)msg->key));

    char StringForSignature[256] = { 0 };

    switch (token_method)
    {
        case ONENET_METHOD_MD5:
            sign.method = "md5";
            sprintf(StringForSignature, "%sn%sn%sn%s", sign.et, sign.method, sign.res, sign.version);
            esp_hmac_md5((unsigned char*)plaintext, declen, (unsigned char*)StringForSignature, strlen(StringForSignature), (unsigned char*)hmac);
            break;
        case ONENET_METHOD_SHA1:
            sign.method = "sha1";
            sprintf(StringForSignature, "%sn%sn%sn%s", sign.et, sign.method, sign.res, sign.version);
            esp_hmac_sha1((unsigned char*)plaintext, declen, (unsigned char*)StringForSignature, strlen(StringForSignature), (unsigned char*)hmac);
            break;
        case ONENET_METHOD_SHA256:
            sign.method = "sha256";
            sprintf(StringForSignature, "%sn%sn%sn%s", sign.et, sign.method, sign.res, sign.version);
            esp_hmac_sha256((unsigned char*)plaintext, declen, (unsigned char*)StringForSignature, strlen(StringForSignature), (unsigned char*)hmac);
            break;
    }
    mbedtls_base64_encode((unsigned char*)sign.sign, sizeof(sign.sign), &enclen, (unsigned char*)hmac, strlen(hmac));
    return url_encoding_for_token(&sign,token,maxlen);
}

测试验证

 代码下载

国际惯例,代码下载

onenet_token: onenet对接token计算方式 (gitee.com)

 onenet对接token计算C语言实现

最后

以上就是幸福大树为你收集整理的CAT1|mqtt对接onenet平台-token计算(c语言)的全部内容,希望文章能够帮你解决CAT1|mqtt对接onenet平台-token计算(c语言)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(179)

评论列表共有 0 条评论

立即
投稿
返回
顶部