主頁 >  其他 > 騰訊物聯網云平臺 密鑰 MQTT引數生成及密碼加密演算法實作

騰訊物聯網云平臺 密鑰 MQTT引數生成及密碼加密演算法實作

2021-11-04 12:23:52 其他

騰訊物聯網云平臺 密鑰 MQTT引數生成及密碼加密演算法實作

  • 騰訊云三元組

騰訊云三元組

首先在騰訊物聯網云平臺創建好專案、產品、設備,然后獲取該設備的三元組,下面通過我的創建的產品作為示例:
ProductID:IAYFFH3EO2
DeviceName:dev3
DeviceSecret: xjOShbtCetQmvEaJ75RJ1g==
#獲取clinetID、Username、Password
clientID:IAYFFH3EO2dev3(ProductID+DeviceName)

Username:IAYFFH3EO2dev3;12010126;HD3CI;1635759071( c l i e n t i d ; {clientid}; clientid;{sdkappid}; c o n n i d ; {connid}; connid;{expiry})
sdkappid、connid可隨機生成,但注意字符個數sdkappid為8個字符,connid為5個字符,expiry為失效時間戳,即超過該時間戳后該密鑰失效,Username可通過后面的加密演算法自動生成,不需要自行生成,

Password:password 是由Username和DeviceSecret進行hmacSha1或者hmacSha256生成的,而且DeviceSecret是通過base64加密生成的,在生成Password前需要DeviceSecret解密base64,再進行hmacSha1或者hmacSha256加密生成,

騰訊云也提供了幾種語言的演算法實作,目前試過python的腳本是hmacsha256演算法腳本且能正常生成引數但是我們是進行C開發的所以不適合通過呼叫python腳本來生成,其提供的C是hmacsha1演算法但是工程不完整的而且是HAL平臺的,本人通過下載騰訊云的sdk將一部分代碼提取了出來,實作c通過hmacsha1演算法生成clientID、Username、Password,
test.c

#include "limits.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "utils_base64.h"
#include "utils_hmac.h"
#include <time.h>
#include <string.h>

/* Max size of base64 encoded PSK = 64, after decode: 64/4*3 = 48*/
#define DECODE_PSK_LENGTH 48

/* MAX valid time when connect to MQTT server. 0: always valid */
/* Use this only if the device has accurate UTC time. Otherwise, set to 0 */
#define MAX_ACCESS_EXPIRE_TIMEOUT (60 * 1000)  //過期時間我這里設定的是1分鐘,只要每次連接前都生成一次這里可以設定為0

/* Max size of conn Id  */
#define MAX_CONN_ID_LEN (6)

/* IoT C-SDK APPID */
#define QCLOUD_IOT_DEVICE_SDK_APPID     "12010126"   //sdkappid設定的固定值
#define QCLOUD_IOT_DEVICE_SDK_APPID_LEN (sizeof(QCLOUD_IOT_DEVICE_SDK_APPID) - 1)

static void HexDump(uint8_t *pData, uint16_t len)
{
    int i;

    for (i = 0; i < len; i++) {
        if (i % 32 == 0) {
            printf("\n");
        }
        printf(" %02X", pData[i]);
    }
    printf("\n");
}


int main(int argc, char **argv)
{
    char *product_id    = NULL;
    char *device_name   = NULL;
    char *device_secret = NULL;

    char *username     = NULL;
    int   username_len = 0;
    char  conn_id[MAX_CONN_ID_LEN];

    char password[51]      = {0};
    char username_sign[41] = {0};

    char   psk_base64decode[DECODE_PSK_LENGTH];
    size_t psk_base64decode_len = 0;

    long cur_timestamp = 0;

    if (argc != 4) {
        printf("please ./qcloud-mqtt-sign product_id device_name device_secret\r\n");
        return -1;
    }

    product_id    = argv[1];
    device_name   = argv[2];
    device_secret = argv[3];

    /* first device_secret base64 decode */
    qcloud_iot_utils_base64decode((unsigned char *)psk_base64decode, DECODE_PSK_LENGTH, &psk_base64decode_len,(unsigned char *)device_secret, strlen(device_secret));
    //base64_decode(device_secret, psk_base64decode);
    printf("device_secret base64 decode:");


    HexDump(psk_base64decode, psk_base64decode_len);

    /* second create mqtt username
     * [productdevicename;appid;randomconnid;timestamp] */
    cur_timestamp = time(NULL) + MAX_ACCESS_EXPIRE_TIMEOUT / 1000;
    if (cur_timestamp <= 0 || MAX_ACCESS_EXPIRE_TIMEOUT <= 0) {
        cur_timestamp = LONG_MAX;
    }

    // 20 for timestampe length & delimiter
    username_len = strlen(product_id) + strlen(device_name) + QCLOUD_IOT_DEVICE_SDK_APPID_LEN + MAX_CONN_ID_LEN + 20;
    username     = (char *)malloc(username_len);
    if (username == NULL) {
        printf("malloc username failed!\r\n");
        return -1;
    }

    snprintf(username, username_len, "%s%s;%s;%s;%ld", product_id, device_name, QCLOUD_IOT_DEVICE_SDK_APPID,"HD3CI", cur_timestamp);  //connid設定的固定值HD3CI

    /* third use psk_base64decode hamc_sha1 calc mqtt username sign crate mqtt
     * password */
    utils_hmac_sha1(username, strlen(username), username_sign, psk_base64decode, psk_base64decode_len);

    //Hmacsha256_enc(psk_base64decode,psk_base64decode_len,username,strlen(username),username_sign);
    printf("username sign: %s\r\n", username_sign);
    snprintf(password, 51, "%s;hmacsha1", username_sign);

    printf("Client ID: %s%s\r\n", product_id, device_name);
    printf("username : %s\r\n", username);
    printf("password : %s\r\n", password);

    free(username);

    return 0;
}

utils_base64.c

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifdef __cplusplus
extern "C" {
#endif

#include "utils_base64.h"

#include <stdint.h>
#include <stdlib.h>

static const unsigned char base64_enc_map[64] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
    'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
    's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

static const unsigned char base64_dec_map[128] = {
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 62,
    127, 127, 127, 63,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  127, 127, 127, 64,  127, 127, 127, 0,
    1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,
    23,  24,  25,  127, 127, 127, 127, 127, 127, 26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
    39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  127, 127, 127, 127, 127};

#define BASE64_SIZE_T_MAX ((size_t)-1) /* SIZE_T_MAX is not standard */

int qcloud_iot_utils_base64encode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen)
{
    size_t         i, n;
    unsigned char *p;

    if (slen == 0) {
        *olen = 0;
        return (0);
    }

    n = slen / 3 + (slen % 3 != 0);

    if (n > (BASE64_SIZE_T_MAX - 1) / 4) {
        *olen = BASE64_SIZE_T_MAX;
        return (QCLOUD_ERR_FAILURE);
    }

    n *= 4;

    if ((dlen < n + 1) || (NULL == dst)) {
        *olen = n + 1;
        return (QCLOUD_ERR_FAILURE);
    }

    n = (slen / 3) * 3;

    int C1, C2, C3;
    for (i = 0, p = dst; i < n; i += 3) {
        C1 = *src++;
        C2 = *src++;
        C3 = *src++;

        *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
        *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
        *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
        *p++ = base64_enc_map[C3 & 0x3F];
    }

    if (i < slen) {
        C1 = *src++;
        C2 = ((i + 1) < slen) ? *src++ : 0;

        *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
        *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];

        if ((i + 1) < slen)
            *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
        else
            *p++ = '=';

        *p++ = '=';
    }

    *olen = p - dst;
    *p    = 0;

    return (0);
}

int qcloud_iot_utils_base64decode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen)
{
    size_t         i, n;
    uint32_t       j, x;
    unsigned char *p;

    /* First pass: check for validity and get output length */
    for (i = n = j = 0; i < slen; i++) {
        /* Skip spaces before checking for EOL */
        x = 0;
        while (i < slen && src[i] == ' ') {
            ++i;
            ++x;
        }

        /* Spaces at end of buffer are OK */
        if (i == slen)
            break;

        if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n')
            continue;

        if (src[i] == '\n')
            continue;

        /* Space inside a line is an error */
        if (x != 0)
            return (QCLOUD_ERR_FAILURE);

        if (src[i] == '=' && ++j > 2)
            return (QCLOUD_ERR_FAILURE);

        if (src[i] > 127 || base64_dec_map[src[i]] == 127)
            return (QCLOUD_ERR_FAILURE);

        if (base64_dec_map[src[i]] < 64 && j != 0)
            return (QCLOUD_ERR_FAILURE);

        n++;
    }

    if (n == 0) {
        *olen = 0;
        return (0);
    }

    n = ((n * 6) + 7) >> 3;
    n -= j;

    if (dst == NULL || dlen < n) {
        *olen = n;
        return (QCLOUD_ERR_FAILURE);
    }

    for (j = 3, n = x = 0, p = dst; i > 0; i--, src++) {
        if (*src == '\r' || *src == '\n' || *src == ' ')
            continue;

        j -= (base64_dec_map[*src] == 64);
        x = (x << 6) | (base64_dec_map[*src] & 0x3F);

        if (++n == 4) {
            n = 0;
            if (j > 0)
                *p++ = (unsigned char)(x >> 16);
            if (j > 1)
                *p++ = (unsigned char)(x >> 8);
            if (j > 2)
                *p++ = (unsigned char)(x);
        }
    }

    *olen = p - dst;

    return (0);
}

#ifdef __cplusplus
}
#endif

utils_base64.h

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifndef QCLOUD_IOT_UTILS_BASE64_H_
#define QCLOUD_IOT_UTILS_BASE64_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>

#include "qcloud_iot_export_error.h"

int qcloud_iot_utils_base64encode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen);

int qcloud_iot_utils_base64decode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen);

#ifdef __cplusplus
}
#endif
#endif /* QCLOUD_IOT_UTILS_BASE64_H_ */

qcloud_iot_export_error.h

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifndef QCLOUD_IOT_EXPORT_ERROR_H_
#define QCLOUD_IOT_EXPORT_ERROR_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
 * IOT SDK return/error code
 * Enumeration of return code in QCloud IoT C-SDK.
 * Values less than 0 are specific error codes
 * Value of 0 is successful return
 * Values greater than 0 are specific non-error return codes
 */
typedef enum {
    QCLOUD_RET_MQTT_ALREADY_CONNECTED           = 4,  // Already connected with MQTT server
    QCLOUD_RET_MQTT_CONNACK_CONNECTION_ACCEPTED = 3,  // MQTT connection accepted by server
    QCLOUD_RET_MQTT_MANUALLY_DISCONNECTED       = 2,  // Manually disconnected with MQTT server
    QCLOUD_RET_MQTT_RECONNECTED                 = 1,  // Reconnected with MQTT server successfully

    QCLOUD_RET_SUCCESS = 0,  // Successful return

    QCLOUD_ERR_FAILURE  = -1001,  // Generic failure return
    QCLOUD_ERR_INVAL    = -1002,  // Invalid parameter
    QCLOUD_ERR_DEV_INFO = -1003,  // Fail to get device info
    QCLOUD_ERR_MALLOC   = -1004,  // Fail to malloc memory

    QCLOUD_ERR_HTTP_CLOSED         = -3,   // HTTP server close the connection
    QCLOUD_ERR_HTTP                = -4,   // HTTP unknown error
    QCLOUD_ERR_HTTP_PRTCL          = -5,   // HTTP protocol error
    QCLOUD_ERR_HTTP_UNRESOLVED_DNS = -6,   // HTTP DNS resolve failed
    QCLOUD_ERR_HTTP_PARSE          = -7,   // HTTP URL parse failed
    QCLOUD_ERR_HTTP_CONN           = -8,   // HTTP connect failed
    QCLOUD_ERR_HTTP_AUTH           = -9,   // HTTP auth failed
    QCLOUD_ERR_HTTP_NOT_FOUND      = -10,  // HTTP 404
    QCLOUD_ERR_HTTP_TIMEOUT        = -11,  // HTTP timeout

    QCLOUD_ERR_MQTT_PUSH_TO_LIST_FAILED                   = -102,  // Fail to push node to MQTT waiting list
    QCLOUD_ERR_MQTT_NO_CONN                               = -103,  // Not connected with MQTT server
    QCLOUD_ERR_MQTT_UNKNOWN                               = -104,  // MQTT unknown error
    QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT                  = -105,  // Reconnecting with MQTT server
    QCLOUD_ERR_MQTT_RECONNECT_TIMEOUT                     = -106,  // MQTT reconnect timeout
    QCLOUD_ERR_MQTT_MAX_SUBSCRIPTIONS                     = -107,  // MQTT topic subscription out of range
    QCLOUD_ERR_MQTT_SUB                                   = -108,  // MQTT topic subscription fail
    QCLOUD_ERR_MQTT_NOTHING_TO_READ                       = -109,  // MQTT nothing to read
    QCLOUD_ERR_MQTT_PACKET_READ                           = -110,  // Something wrong when reading MQTT packet
    QCLOUD_ERR_MQTT_REQUEST_TIMEOUT                       = -111,  // MQTT request timeout
    QCLOUD_ERR_MQTT_CONNACK_UNKNOWN                       = -112,  // MQTT connection refused: unknown error
    QCLOUD_ERR_MQTT_CONNACK_UNACCEPTABLE_PROTOCOL_VERSION = -113,  // MQTT connection refused: protocol version invalid
    QCLOUD_ERR_MQTT_CONNACK_IDENTIFIER_REJECTED           = -114,  // MQTT connection refused: identifier rejected
    QCLOUD_ERR_MQTT_CONNACK_SERVER_UNAVAILABLE            = -115,  // MQTT connection refused: service not available
    QCLOUD_ERR_MQTT_CONNACK_BAD_USERDATA                  = -116,  // MQTT connection refused: bad user name or password
    QCLOUD_ERR_MQTT_CONNACK_NOT_AUTHORIZED                = -117,  // MQTT connection refused: not authorized
    QCLOUD_ERR_RX_MESSAGE_INVAL                           = -118,  // MQTT received invalid msg
    QCLOUD_ERR_BUF_TOO_SHORT                              = -119,  // MQTT recv buffer not enough
    QCLOUD_ERR_MQTT_QOS_NOT_SUPPORT                       = -120,  // MQTT QoS level not supported
    QCLOUD_ERR_MQTT_UNSUB_FAIL                            = -121,  // MQTT unsubscribe failed

    QCLOUD_ERR_JSON_PARSE            = -132,  // JSON parsing error
    QCLOUD_ERR_JSON_BUFFER_TRUNCATED = -133,  // JSON buffer truncated
    QCLOUD_ERR_JSON_BUFFER_TOO_SMALL = -134,  // JSON parsing buffer not enough
    QCLOUD_ERR_JSON                  = -135,  // JSON generation error
    QCLOUD_ERR_MAX_JSON_TOKEN        = -136,  // JSON token out of range

    QCLOUD_ERR_MAX_APPENDING_REQUEST = -137,  // appending request out of range
    QCLOUD_ERR_MAX_TOPIC_LENGTH      = -138,  // Topic length oversize

    QCLOUD_ERR_COAP_NULL              = -150,  // COAP null pointer
    QCLOUD_ERR_COAP_DATA_SIZE         = -151,  // COAP data size out of range
    QCLOUD_ERR_COAP_INTERNAL          = -152,  // COAP interval error
    QCLOUD_ERR_COAP_BADMSG            = -153,  // COAP bad msg
    QCLOUD_ERR_DTLS_PEER_CLOSE_NOTIFY = -160,  // DTLS connection is closed

    QCLOUD_ERR_PROPERTY_EXIST     = -201,  // property already exist
    QCLOUD_ERR_NOT_PROPERTY_EXIST = -202,  // property not exist
    QCLOUD_ERR_REPORT_TIMEOUT     = -203,  // update timeout
    QCLOUD_ERR_REPORT_REJECTED    = -204,  // update rejected by server
    QCLOUD_ERR_GET_TIMEOUT        = -205,  // get timeout
    QCLOUD_ERR_GET_REJECTED       = -206,  // get rejected by server

    QCLOUD_ERR_ACTION_EXIST     = -210,  // acion already exist
    QCLOUD_ERR_NOT_ACTION_EXIST = -211,  // acion not exist

    QCLOUD_ERR_GATEWAY_CREATE_SESSION_FAIL = -221,  // Gateway fail to create sub-device session
    QCLOUD_ERR_GATEWAY_SESSION_NO_EXIST    = -222,  // Gateway sub-device session not exist
    QCLOUD_ERR_GATEWAY_SESSION_TIMEOUT     = -223,  // Gateway sub-device session timeout
    QCLOUD_ERR_GATEWAY_SUBDEV_ONLINE       = -224,  // Gateway sub-device online
    QCLOUD_ERR_GATEWAY_SUBDEV_OFFLINE      = -225,  // Gateway sub-device offline

    QCLOUD_ERR_TCP_SOCKET_FAILED   = -601,  // TLS TCP socket connect fail
    QCLOUD_ERR_TCP_UNKNOWN_HOST    = -602,  // TCP unknown host (DNS fail)
    QCLOUD_ERR_TCP_CONNECT         = -603,  // TCP/UDP socket connect fail
    QCLOUD_ERR_TCP_READ_TIMEOUT    = -604,  // TCP read timeout
    QCLOUD_ERR_TCP_WRITE_TIMEOUT   = -605,  // TCP write timeout
    QCLOUD_ERR_TCP_READ_FAIL       = -606,  // TCP read error
    QCLOUD_ERR_TCP_WRITE_FAIL      = -607,  // TCP write error
    QCLOUD_ERR_TCP_PEER_SHUTDOWN   = -608,  // TCP server close connection
    QCLOUD_ERR_TCP_NOTHING_TO_READ = -609,  // TCP socket nothing to read

    QCLOUD_ERR_SSL_INIT            = -701,  // TLS/SSL init fail
    QCLOUD_ERR_SSL_CERT            = -702,  // TLS/SSL certificate issue
    QCLOUD_ERR_SSL_CONNECT         = -703,  // TLS/SSL connect fail
    QCLOUD_ERR_SSL_CONNECT_TIMEOUT = -704,  // TLS/SSL connect timeout
    QCLOUD_ERR_SSL_WRITE_TIMEOUT   = -705,  // TLS/SSL write timeout
    QCLOUD_ERR_SSL_WRITE           = -706,  // TLS/SSL write error
    QCLOUD_ERR_SSL_READ_TIMEOUT    = -707,  // TLS/SSL read timeout
    QCLOUD_ERR_SSL_READ            = -708,  // TLS/SSL read error
    QCLOUD_ERR_SSL_NOTHING_TO_READ = -709,  // TLS/SSL nothing to read

    QCLOUD_ERR_BIND_PARA_ERR        = -801,  // bind sub device param error
    QCLOUD_ERR_BIND_SUBDEV_ERR      = -802,  // sub device not exist or illegal
    QCLOUD_ERR_BIND_SIGN_ERR        = -803,  // signature check err
    QCLOUD_ERR_BIND_SIGN_METHOD_RRR = -804,  // signmethod not supporte
    QCLOUD_ERR_BIND_SIGN_EXPIRED    = -805,  // signature expired
    QCLOUD_ERR_BIND_BEEN_BINDED     = -806,  // sub device has been binded by other gateway
    QCLOUD_ERR_BIND_SUBDEV_FORBID   = -807,  // sub device not allow to bind
    QCLOUD_ERR_BIND_OP_FORBID       = -808,  // operation not permit
    QCLOUD_ERR_BIND_REPEATED_REQ    = -809,  // repeated bind request,has been binded
} IoT_Return_Code;

#ifdef __cplusplus
}
#endif

#endif /* QCLOUD_IOT_EXPORT_ERROR_H_ */

utils_hmac.c

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */
#include <stdint.h>
#include "utils_hmac.h"

#include <string.h>

#include "utils_md5.h"
#include "utils_sha1.h"

#define KEY_IOPAD_SIZE 64

#define MD5_DIGEST_SIZE  16
#define SHA1_DIGEST_SIZE 20

void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
    if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
        //Log_e("parameter is Null,failed!");
        return;
    }

    if (key_len > KEY_IOPAD_SIZE) {
        //Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
        return;
    }

    iot_md5_context context;
    unsigned char   k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
    unsigned char   k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
    unsigned char   out[MD5_DIGEST_SIZE];
    int             i;

    /* start out by storing key in pads */
    memset(k_ipad, 0, sizeof(k_ipad));
    memset(k_opad, 0, sizeof(k_opad));
    memcpy(k_ipad, key, key_len);
    memcpy(k_opad, key, key_len);

    /* XOR key with ipad and opad values */
    for (i = 0; i < KEY_IOPAD_SIZE; i++) {
        k_ipad[i] ^= 0x36;
        k_opad[i] ^= 0x5c;
    }

    /* perform inner MD5 */
    utils_md5_init(&context);                                  /* init context for 1st pass */
    utils_md5_starts(&context);                                /* setup context for 1st pass */
    utils_md5_update(&context, k_ipad, KEY_IOPAD_SIZE);        /* start with inner pad */
    utils_md5_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
    utils_md5_finish(&context, out);                           /* finish up 1st pass */

    /* perform outer MD5 */
    utils_md5_init(&context);                           /* init context for 2nd pass */
    utils_md5_starts(&context);                         /* setup context for 2nd pass */
    utils_md5_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
    utils_md5_update(&context, out, MD5_DIGEST_SIZE);   /* then results of 1st hash */
    utils_md5_finish(&context, out);                    /* finish up 2nd pass */

    for (i = 0; i < MD5_DIGEST_SIZE; ++i) {
        digest[i * 2]     = utils_hb2hex(out[i] >> 4);
        digest[i * 2 + 1] = utils_hb2hex(out[i]);
    }
}

void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
    if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
        //Log_e("parameter is Null,failed!");
        return;
    }

    if (key_len > KEY_IOPAD_SIZE) {
        //Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
        return;
    }

    iot_sha1_context context;
    unsigned char    k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
    unsigned char    k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
    unsigned char    out[SHA1_DIGEST_SIZE];
    int              i;

    /* start out by storing key in pads */
    memset(k_ipad, 0, sizeof(k_ipad));
    memset(k_opad, 0, sizeof(k_opad));
    memcpy(k_ipad, key, key_len);
    memcpy(k_opad, key, key_len);

    /* XOR key with ipad and opad values */
    for (i = 0; i < KEY_IOPAD_SIZE; i++) {
        k_ipad[i] ^= 0x36;
        k_opad[i] ^= 0x5c;
    }

    /* perform inner SHA */
    utils_sha1_init(&context);                                  /* init context for 1st pass */
    utils_sha1_starts(&context);                                /* setup context for 1st pass */
    utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE);        /* start with inner pad */
    utils_sha1_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
    utils_sha1_finish(&context, out);                           /* finish up 1st pass */

    /* perform outer SHA */
    utils_sha1_init(&context);                           /* init context for 2nd pass */
    utils_sha1_starts(&context);                         /* setup context for 2nd pass */
    utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
    utils_sha1_update(&context, out, SHA1_DIGEST_SIZE);  /* then results of 1st hash */
    utils_sha1_finish(&context, out);                    /* finish up 2nd pass */

    for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
        digest[i * 2]     = utils_hb2hex(out[i] >> 4);
        digest[i * 2 + 1] = utils_hb2hex(out[i]);
    }
}

int utils_hmac_sha1_hex(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
    if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
        //Log_e("parameter is Null,failed!");
        return 0;
    }

    if (key_len > KEY_IOPAD_SIZE) {
        //Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
        return 0;
    }

    iot_sha1_context context;
    unsigned char    k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
    unsigned char    k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
    unsigned char    out[SHA1_DIGEST_SIZE];
    int              i;

    /* start out by storing key in pads */
    memset(k_ipad, 0, sizeof(k_ipad));
    memset(k_opad, 0, sizeof(k_opad));
    memcpy(k_ipad, key, key_len);
    memcpy(k_opad, key, key_len);

    /* XOR key with ipad and opad values */
    for (i = 0; i < KEY_IOPAD_SIZE; i++) {
        k_ipad[i] ^= 0x36;
        k_opad[i] ^= 0x5c;
    }

    /* perform inner SHA */
    utils_sha1_init(&context);                                  /* init context for 1st pass */
    utils_sha1_starts(&context);                                /* setup context for 1st pass */
    utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE);        /* start with inner pad */
    utils_sha1_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
    utils_sha1_finish(&context, out);                           /* finish up 1st pass */

    /* perform outer SHA */
    utils_sha1_init(&context);                           /* init context for 2nd pass */
    utils_sha1_starts(&context);                         /* setup context for 2nd pass */
    utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
    utils_sha1_update(&context, out, SHA1_DIGEST_SIZE);  /* then results of 1st hash */
    utils_sha1_finish(&context, out);                    /* finish up 2nd pass */

    memcpy(digest, out, SHA1_DIGEST_SIZE);

    return SHA1_DIGEST_SIZE;
}

utils_hmac.h

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifndef QCLOUD_IOT_UTILS_HMAC_H_
#define QCLOUD_IOT_UTILS_HMAC_H_

#include <string.h>

void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len);

void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len);

int utils_hmac_sha1_hex(const char *msg, int msg_len, char *digest, const char *key, int key_len);

#endif

utils_md5.c

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifdef __cplusplus
extern "C" {
#endif

#include "utils_md5.h"

#include <stdlib.h>
#include <string.h>

#define MD5_DIGEST_SIZE 16

/* Implementation that should never be optimized out by the compiler */
static void _utils_md5_zeroize(void *v, size_t n)
{
    volatile unsigned char *p = v;
    while (n--) *p++ = 0;
}

/*
 * 32-bit integer manipulation macros (little endian)
 */
#ifndef IOT_MD5_GET_UINT32_LE
#define IOT_MD5_GET_UINT32_LE(n, b, i)                                                                \
    {                                                                                                 \
        (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | ((uint32_t)(b)[(i) + 2] << 16) | \
              ((uint32_t)(b)[(i) + 3] << 24);                                                         \
    }
#endif

#ifndef IOT_MD5_PUT_UINT32_LE
#define IOT_MD5_PUT_UINT32_LE(n, b, i)                      \
    {                                                       \
        (b)[(i)]     = (unsigned char)(((n)) & 0xFF);       \
        (b)[(i) + 1] = (unsigned char)(((n) >> 8) & 0xFF);  \
        (b)[(i) + 2] = (unsigned char)(((n) >> 16) & 0xFF); \
        (b)[(i) + 3] = (unsigned char)(((n) >> 24) & 0xFF); \
    }
#endif

void utils_md5_init(iot_md5_context *ctx)
{
    memset(ctx, 0, sizeof(iot_md5_context));
}

void utils_md5_free(iot_md5_context *ctx)
{
    if (ctx == NULL) {
        return;
    }

    _utils_md5_zeroize(ctx, sizeof(iot_md5_context));
}

void utils_md5_clone(iot_md5_context *dst, const iot_md5_context *src)
{
    *dst = *src;
}

/*
 * MD5 context setup
 */
void utils_md5_starts(iot_md5_context *ctx)
{
    ctx->total[0] = 0;
    ctx->total[1] = 0;

    ctx->state[0] = 0x67452301;
    ctx->state[1] = 0xEFCDAB89;
    ctx->state[2] = 0x98BADCFE;
    ctx->state[3] = 0x10325476;
}

void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64])
{
    uint32_t X[16], A, B, C, D;

    IOT_MD5_GET_UINT32_LE(X[0], data, 0);
    IOT_MD5_GET_UINT32_LE(X[1], data, 4);
    IOT_MD5_GET_UINT32_LE(X[2], data, 8);
    IOT_MD5_GET_UINT32_LE(X[3], data, 12);
    IOT_MD5_GET_UINT32_LE(X[4], data, 16);
    IOT_MD5_GET_UINT32_LE(X[5], data, 20);
    IOT_MD5_GET_UINT32_LE(X[6], data, 24);
    IOT_MD5_GET_UINT32_LE(X[7], data, 28);
    IOT_MD5_GET_UINT32_LE(X[8], data, 32);
    IOT_MD5_GET_UINT32_LE(X[9], data, 36);
    IOT_MD5_GET_UINT32_LE(X[10], data, 40);
    IOT_MD5_GET_UINT32_LE(X[11], data, 44);
    IOT_MD5_GET_UINT32_LE(X[12], data, 48);
    IOT_MD5_GET_UINT32_LE(X[13], data, 52);
    IOT_MD5_GET_UINT32_LE(X[14], data, 56);
    IOT_MD5_GET_UINT32_LE(X[15], data, 60);

#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))

#define P(a, b, c, d, k, s, t)      \
    {                               \
        a += F(b, c, d) + X[k] + t; \
        a = S(a, s) + b;            \
    }

    A = ctx->state[0];
    B = ctx->state[1];
    C = ctx->state[2];
    D = ctx->state[3];

#define F(x, y, z) (z ^ (x & (y ^ z)))

    P(A, B, C, D, 0, 7, 0xD76AA478);
    P(D, A, B, C, 1, 12, 0xE8C7B756);
    P(C, D, A, B, 2, 17, 0x242070DB);
    P(B, C, D, A, 3, 22, 0xC1BDCEEE);
    P(A, B, C, D, 4, 7, 0xF57C0FAF);
    P(D, A, B, C, 5, 12, 0x4787C62A);
    P(C, D, A, B, 6, 17, 0xA8304613);
    P(B, C, D, A, 7, 22, 0xFD469501);
    P(A, B, C, D, 8, 7, 0x698098D8);
    P(D, A, B, C, 9, 12, 0x8B44F7AF);
    P(C, D, A, B, 10, 17, 0xFFFF5BB1);
    P(B, C, D, A, 11, 22, 0x895CD7BE);
    P(A, B, C, D, 12, 7, 0x6B901122);
    P(D, A, B, C, 13, 12, 0xFD987193);
    P(C, D, A, B, 14, 17, 0xA679438E);
    P(B, C, D, A, 15, 22, 0x49B40821);

#undef F

#define F(x, y, z) (y ^ (z & (x ^ y)))

    P(A, B, C, D, 1, 5, 0xF61E2562);
    P(D, A, B, C, 6, 9, 0xC040B340);
    P(C, D, A, B, 11, 14, 0x265E5A51);
    P(B, C, D, A, 0, 20, 0xE9B6C7AA);
    P(A, B, C, D, 5, 5, 0xD62F105D);
    P(D, A, B, C, 10, 9, 0x02441453);
    P(C, D, A, B, 15, 14, 0xD8A1E681);
    P(B, C, D, A, 4, 20, 0xE7D3FBC8);
    P(A, B, C, D, 9, 5, 0x21E1CDE6);
    P(D, A, B, C, 14, 9, 0xC33707D6);
    P(C, D, A, B, 3, 14, 0xF4D50D87);
    P(B, C, D, A, 8, 20, 0x455A14ED);
    P(A, B, C, D, 13, 5, 0xA9E3E905);
    P(D, A, B, C, 2, 9, 0xFCEFA3F8);
    P(C, D, A, B, 7, 14, 0x676F02D9);
    P(B, C, D, A, 12, 20, 0x8D2A4C8A);

#undef F

#define F(x, y, z) (x ^ y ^ z)

    P(A, B, C, D, 5, 4, 0xFFFA3942);
    P(D, A, B, C, 8, 11, 0x8771F681);
    P(C, D, A, B, 11, 16, 0x6D9D6122);
    P(B, C, D, A, 14, 23, 0xFDE5380C);
    P(A, B, C, D, 1, 4, 0xA4BEEA44);
    P(D, A, B, C, 4, 11, 0x4BDECFA9);
    P(C, D, A, B, 7, 16, 0xF6BB4B60);
    P(B, C, D, A, 10, 23, 0xBEBFBC70);
    P(A, B, C, D, 13, 4, 0x289B7EC6);
    P(D, A, B, C, 0, 11, 0xEAA127FA);
    P(C, D, A, B, 3, 16, 0xD4EF3085);
    P(B, C, D, A, 6, 23, 0x04881D05);
    P(A, B, C, D, 9, 4, 0xD9D4D039);
    P(D, A, B, C, 12, 11, 0xE6DB99E5);
    P(C, D, A, B, 15, 16, 0x1FA27CF8);
    P(B, C, D, A, 2, 23, 0xC4AC5665);

#undef F

#define F(x, y, z) (y ^ (x | ~z))

    P(A, B, C, D, 0, 6, 0xF4292244);
    P(D, A, B, C, 7, 10, 0x432AFF97);
    P(C, D, A, B, 14, 15, 0xAB9423A7);
    P(B, C, D, A, 5, 21, 0xFC93A039);
    P(A, B, C, D, 12, 6, 0x655B59C3);
    P(D, A, B, C, 3, 10, 0x8F0CCC92);
    P(C, D, A, B, 10, 15, 0xFFEFF47D);
    P(B, C, D, A, 1, 21, 0x85845DD1);
    P(A, B, C, D, 8, 6, 0x6FA87E4F);
    P(D, A, B, C, 15, 10, 0xFE2CE6E0);
    P(C, D, A, B, 6, 15, 0xA3014314);
    P(B, C, D, A, 13, 21, 0x4E0811A1);
    P(A, B, C, D, 4, 6, 0xF7537E82);
    P(D, A, B, C, 11, 10, 0xBD3AF235);
    P(C, D, A, B, 2, 15, 0x2AD7D2BB);
    P(B, C, D, A, 9, 21, 0xEB86D391);

#undef F

    ctx->state[0] += A;
    ctx->state[1] += B;
    ctx->state[2] += C;
    ctx->state[3] += D;
}

/*
 * MD5 process buffer
 */
void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, size_t ilen)
{
    size_t   fill;
    uint32_t left;

    if (ilen == 0) {
        return;
    }

    left = ctx->total[0] & 0x3F;
    fill = 64 - left;

    ctx->total[0] += (uint32_t)ilen;
    ctx->total[0] &= 0xFFFFFFFF;

    if (ctx->total[0] < (uint32_t)ilen) {
        ctx->total[1]++;
    }

    if (left && ilen >= fill) {
        memcpy((void *)(ctx->buffer + left), input, fill);
        utils_md5_process(ctx, ctx->buffer);
        input += fill;
        ilen -= fill;
        left = 0;
    }

    while (ilen >= 64) {
        utils_md5_process(ctx, input);
        input += 64;
        ilen -= 64;
    }

    if (ilen > 0) {
        memcpy((void *)(ctx->buffer + left), input, ilen);
    }
}

static const unsigned char iot_md5_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                  0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                  0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

/*
 * MD5 final digest
 */
void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16])
{
    uint32_t      last, padn;
    uint32_t      high, low;
    unsigned char msglen[8];

    high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
    low  = (ctx->total[0] << 3);

    IOT_MD5_PUT_UINT32_LE(low, msglen, 0);
    IOT_MD5_PUT_UINT32_LE(high, msglen, 4);

    last = ctx->total[0] & 0x3F;
    padn = (last < 56) ? (56 - last) : (120 - last);

    utils_md5_update(ctx, iot_md5_padding, padn);
    utils_md5_update(ctx, msglen, 8);

    IOT_MD5_PUT_UINT32_LE(ctx->state[0], output, 0);
    IOT_MD5_PUT_UINT32_LE(ctx->state[1], output, 4);
    IOT_MD5_PUT_UINT32_LE(ctx->state[2], output, 8);
    IOT_MD5_PUT_UINT32_LE(ctx->state[3], output, 12);
}

/*
 * output = MD5( input buffer )
 */
void utils_md5(const unsigned char *input, size_t ilen, unsigned char output[16])
{
    iot_md5_context ctx;

    utils_md5_init(&ctx);
    utils_md5_starts(&ctx);
    utils_md5_update(&ctx, input, ilen);
    utils_md5_finish(&ctx, output);
    utils_md5_free(&ctx);
}

int8_t utils_hb2hex(uint8_t hb)
{
    hb = hb & 0xF;
    return (int8_t)(hb < 10 ? '0' + hb : hb - 10 + 'a');
}

void utils_md5_str(const unsigned char *input, size_t ilen, unsigned char *output)
{
    int           i;
    unsigned char buf_out[16];
    utils_md5(input, ilen, buf_out);

    // hex to string
    for (i = 0; i < 16; ++i) {
        output[i * 2]     = utils_hb2hex(buf_out[i] >> 4);
        output[i * 2 + 1] = utils_hb2hex(buf_out[i]);
    }
    output[32] = '\0';
}

/*
 * MD5 create dynamicly
 */
void *utils_md5_create(void)
{
    iot_md5_context *ctx = malloc(sizeof(iot_md5_context));
    if (NULL == ctx) {
        return NULL;
    }

    utils_md5_init(ctx);
    utils_md5_starts(ctx);
    return ctx;
}

void utils_md5_finish_str(void *ctx, char *output_str)
{
    int           i;
    unsigned char buf_out[16];
    utils_md5_finish(ctx, buf_out);

    for (i = 0; i < 16; ++i) {
        output_str[i * 2]     = utils_hb2hex(buf_out[i] >> 4);
        output_str[i * 2 + 1] = utils_hb2hex(buf_out[i]);
    }
    output_str[32] = '\0';
}

void utils_md5_delete(void *ctx)
{
    free(ctx);
}

void utils_md5_reset(void *ctx)
{
    iot_md5_context *pCtx = (iot_md5_context *)ctx;

    if (!pCtx) {
        //Log_e("invalid md5 pointer");
        return;
    }

    utils_md5_init(pCtx);
    utils_md5_starts(pCtx);
    return;
}

#ifdef __cplusplus
}
#endif

utils_md5.h

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifndef QCLOUD_IOT_UTILS_MD5_H_
#define QCLOUD_IOT_UTILS_MD5_H_
#include <stdint.h>
#include <stddef.h>

typedef struct {
    uint32_t      total[2];   /*!< number of bytes processed  */
    uint32_t      state[4];   /*!< intermediate digest state  */
    unsigned char buffer[64]; /*!< data block being processed */
} iot_md5_context;

/**
 * @brief init MD5 context
 *
 * @param ctx   MD5 context
 */
void utils_md5_init(iot_md5_context *ctx);

/**
 * @brief free MD5 context
 *
 * @param ctx   MD5 context
 */
void utils_md5_free(iot_md5_context *ctx);

/**
 * @brief clone MD5 context
 *
 * @param dst   destination MD5 context
 * @param src   source MD5 context
 */
void utils_md5_clone(iot_md5_context *dst, const iot_md5_context *src);

/**
 * @brief start MD5 calculation
 *
 * @param ctx   MD5 context
 */
void utils_md5_starts(iot_md5_context *ctx);

/**
 * @brief MD5 update
 *
 * @param ctx MD5 context
 * @param input    input data
 * @param ilen     data length
 */
void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, size_t ilen);

/**
 * @brief          finish MD5 calculation
 *
 * @param ctx      MD5 context
 * @param output   MD5 result
 */
void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16]);

/* MD5 internal process */
void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64]);

/**
 * @brief          Output = MD5( input buffer )
 *
 * @param input    data input
 * @param ilen     data length
 * @param output   MD5 result
 */
void utils_md5(const unsigned char *input, size_t ilen, unsigned char output[16]);

/**
 * @brief          Output = MD5( input buffer )
 *
 * @param input    data input
 * @param ilen     data length
 * @param output   string MD5 result
 */
void utils_md5_str(const unsigned char *input, size_t ilen, unsigned char *output);

int8_t utils_hb2hex(uint8_t hb);

/**
 * @brief          create md5 context dynamic
 *
 * @return         MD5 context
 */
void *utils_md5_create(void);

/**
 * @brief          Output = str(md5)
 *
 * @param ctx      MD5 ctx
 * @param output_str   string MD5 result
 */
void utils_md5_finish_str(void *ctx, char *output_str);

/**
 * @brief          free MD5 context
 *
 * @param ctx      MD5 ctx
 */
void utils_md5_delete(void *ctx);

/**
 * @brief          reset MD5 context
 *
 * @param ctx      MD5 ctx
 */
void utils_md5_reset(void *ctx);

#endif

utils_sha1.c

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#include "utils_sha1.h"

#include <stdlib.h>
#include <string.h>

/* Implementation that should never be optimized out by the compiler */
static void utils_sha1_zeroize(void *v, size_t n)
{
    volatile unsigned char *p = v;
    while (n--) {
        *p++ = 0;
    }
}

/*
 * 32-bit integer manipulation macros (big endian)
 */
#ifndef IOT_SHA1_GET_UINT32_BE
#define IOT_SHA1_GET_UINT32_BE(n, b, i)                                                                     \
    {                                                                                                       \
        (n) = ((uint32_t)(b)[(i)] << 24) | ((uint32_t)(b)[(i) + 1] << 16) | ((uint32_t)(b)[(i) + 2] << 8) | \
              ((uint32_t)(b)[(i) + 3]);                                                                     \
    }
#endif

#ifndef IOT_SHA1_PUT_UINT32_BE
#define IOT_SHA1_PUT_UINT32_BE(n, b, i)            \
    {                                              \
        (b)[(i)]     = (unsigned char)((n) >> 24); \
        (b)[(i) + 1] = (unsigned char)((n) >> 16); \
        (b)[(i) + 2] = (unsigned char)((n) >> 8);  \
        (b)[(i) + 3] = (unsigned char)((n));       \
    }
#endif

void utils_sha1_init(iot_sha1_context *ctx)
{
    memset(ctx, 0, sizeof(iot_sha1_context));
}

void utils_sha1_free(iot_sha1_context *ctx)
{
    if (ctx == NULL) {
        return;
    }

    utils_sha1_zeroize(ctx, sizeof(iot_sha1_context));
}

void utils_sha1_clone(iot_sha1_context *dst, const iot_sha1_context *src)
{
    *dst = *src;
}

/*
 * SHA-1 context setup
 */
void utils_sha1_starts(iot_sha1_context *ctx)
{
    ctx->total[0] = 0;
    ctx->total[1] = 0;

    ctx->state[0] = 0x67452301;
    ctx->state[1] = 0xEFCDAB89;
    ctx->state[2] = 0x98BADCFE;
    ctx->state[3] = 0x10325476;
    ctx->state[4] = 0xC3D2E1F0;
}

void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64])
{
    uint32_t temp, W[16], A, B, C, D, E;

    IOT_SHA1_GET_UINT32_BE(W[0], data, 0);
    IOT_SHA1_GET_UINT32_BE(W[1], data, 4);
    IOT_SHA1_GET_UINT32_BE(W[2], data, 8);
    IOT_SHA1_GET_UINT32_BE(W[3], data, 12);
    IOT_SHA1_GET_UINT32_BE(W[4], data, 16);
    IOT_SHA1_GET_UINT32_BE(W[5], data, 20);
    IOT_SHA1_GET_UINT32_BE(W[6], data, 24);
    IOT_SHA1_GET_UINT32_BE(W[7], data, 28);
    IOT_SHA1_GET_UINT32_BE(W[8], data, 32);
    IOT_SHA1_GET_UINT32_BE(W[9], data, 36);
    IOT_SHA1_GET_UINT32_BE(W[10], data, 40);
    IOT_SHA1_GET_UINT32_BE(W[11], data, 44);
    IOT_SHA1_GET_UINT32_BE(W[12], data, 48);
    IOT_SHA1_GET_UINT32_BE(W[13], data, 52);
    IOT_SHA1_GET_UINT32_BE(W[14], data, 56);
    IOT_SHA1_GET_UINT32_BE(W[15], data, 60);

#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))

#define R(t) \
    (temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], (W[t & 0x0F] = S(temp, 1)))

#define P(a, b, c, d, e, x)                \
    {                                      \
        e += S(a, 5) + F(b, c, d) + K + x; \
        b = S(b, 30);                      \
    }

    A = ctx->state[0];
    B = ctx->state[1];
    C = ctx->state[2];
    D = ctx->state[3];
    E = ctx->state[4];

#define F(x, y, z) (z ^ (x & (y ^ z)))
#define K          0x5A827999

    P(A, B, C, D, E, W[0]);
    P(E, A, B, C, D, W[1]);
    P(D, E, A, B, C, W[2]);
    P(C, D, E, A, B, W[3]);
    P(B, C, D, E, A, W[4]);
    P(A, B, C, D, E, W[5]);
    P(E, A, B, C, D, W[6]);
    P(D, E, A, B, C, W[7]);
    P(C, D, E, A, B, W[8]);
    P(B, C, D, E, A, W[9]);
    P(A, B, C, D, E, W[10]);
    P(E, A, B, C, D, W[11]);
    P(D, E, A, B, C, W[12]);
    P(C, D, E, A, B, W[13]);
    P(B, C, D, E, A, W[14]);
    P(A, B, C, D, E, W[15]);
    P(E, A, B, C, D, R(16));
    P(D, E, A, B, C, R(17));
    P(C, D, E, A, B, R(18));
    P(B, C, D, E, A, R(19));

#undef K
#undef F

#define F(x, y, z) (x ^ y ^ z)
#define K          0x6ED9EBA1

    P(A, B, C, D, E, R(20));
    P(E, A, B, C, D, R(21));
    P(D, E, A, B, C, R(22));
    P(C, D, E, A, B, R(23));
    P(B, C, D, E, A, R(24));
    P(A, B, C, D, E, R(25));
    P(E, A, B, C, D, R(26));
    P(D, E, A, B, C, R(27));
    P(C, D, E, A, B, R(28));
    P(B, C, D, E, A, R(29));
    P(A, B, C, D, E, R(30));
    P(E, A, B, C, D, R(31));
    P(D, E, A, B, C, R(32));
    P(C, D, E, A, B, R(33));
    P(B, C, D, E, A, R(34));
    P(A, B, C, D, E, R(35));
    P(E, A, B, C, D, R(36));
    P(D, E, A, B, C, R(37));
    P(C, D, E, A, B, R(38));
    P(B, C, D, E, A, R(39));

#undef K
#undef F

#define F(x, y, z) ((x & y) | (z & (x | y)))
#define K          0x8F1BBCDC

    P(A, B, C, D, E, R(40));
    P(E, A, B, C, D, R(41));
    P(D, E, A, B, C, R(42));
    P(C, D, E, A, B, R(43));
    P(B, C, D, E, A, R(44));
    P(A, B, C, D, E, R(45));
    P(E, A, B, C, D, R(46));
    P(D, E, A, B, C, R(47));
    P(C, D, E, A, B, R(48));
    P(B, C, D, E, A, R(49));
    P(A, B, C, D, E, R(50));
    P(E, A, B, C, D, R(51));
    P(D, E, A, B, C, R(52));
    P(C, D, E, A, B, R(53));
    P(B, C, D, E, A, R(54));
    P(A, B, C, D, E, R(55));
    P(E, A, B, C, D, R(56));
    P(D, E, A, B, C, R(57));
    P(C, D, E, A, B, R(58));
    P(B, C, D, E, A, R(59));

#undef K
#undef F

#define F(x, y, z) (x ^ y ^ z)
#define K          0xCA62C1D6

    P(A, B, C, D, E, R(60));
    P(E, A, B, C, D, R(61));
    P(D, E, A, B, C, R(62));
    P(C, D, E, A, B, R(63));
    P(B, C, D, E, A, R(64));
    P(A, B, C, D, E, R(65));
    P(E, A, B, C, D, R(66));
    P(D, E, A, B, C, R(67));
    P(C, D, E, A, B, R(68));
    P(B, C, D, E, A, R(69));
    P(A, B, C, D, E, R(70));
    P(E, A, B, C, D, R(71));
    P(D, E, A, B, C, R(72));
    P(C, D, E, A, B, R(73));
    P(B, C, D, E, A, R(74));
    P(A, B, C, D, E, R(75));
    P(E, A, B, C, D, R(76));
    P(D, E, A, B, C, R(77));
    P(C, D, E, A, B, R(78));
    P(B, C, D, E, A, R(79));

#undef K
#undef F

    ctx->state[0] += A;
    ctx->state[1] += B;
    ctx->state[2] += C;
    ctx->state[3] += D;
    ctx->state[4] += E;
}

/*
 * SHA-1 process buffer
 */
void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input, size_t ilen)
{
    size_t   fill;
    uint32_t left;

    if (ilen == 0) {
        return;
    }

    left = ctx->total[0] & 0x3F;
    fill = 64 - left;

    ctx->total[0] += (uint32_t)ilen;
    ctx->total[0] &= 0xFFFFFFFF;

    if (ctx->total[0] < (uint32_t)ilen) {
        ctx->total[1]++;
    }

    if (left && ilen >= fill) {
        memcpy((void *)(ctx->buffer + left), input, fill);
        utils_sha1_process(ctx, ctx->buffer);
        input += fill;
        ilen -= fill;
        left = 0;
    }

    while (ilen >= 64) {
        utils_sha1_process(ctx, input);
        input += 64;
        ilen -= 64;
    }

    if (ilen > 0) {
        memcpy((void *)(ctx->buffer + left), input, ilen);
    }
}

static const unsigned char iot_sha1_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                   0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                   0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

/*
 * SHA-1 final digest
 */
void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20])
{
    uint32_t      last, padn;
    uint32_t      high, low;
    unsigned char msglen[8];

    high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
    low  = (ctx->total[0] << 3);

    IOT_SHA1_PUT_UINT32_BE(high, msglen, 0);
    IOT_SHA1_PUT_UINT32_BE(low, msglen, 4);

    last = ctx->total[0] & 0x3F;
    padn = (last < 56) ? (56 - last) : (120 - last);

    utils_sha1_update(ctx, iot_sha1_padding, padn);
    utils_sha1_update(ctx, msglen, 8);

    IOT_SHA1_PUT_UINT32_BE(ctx->state[0], output, 0);
    IOT_SHA1_PUT_UINT32_BE(ctx->state[1], output, 4);
    IOT_SHA1_PUT_UINT32_BE(ctx->state[2], output, 8);
    IOT_SHA1_PUT_UINT32_BE(ctx->state[3], output, 12);
    IOT_SHA1_PUT_UINT32_BE(ctx->state[4], output, 16);
}

/*
 * output = SHA-1( input buffer )
 */
void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
{
    iot_sha1_context ctx;

    utils_sha1_init(&ctx);
    utils_sha1_starts(&ctx);
    utils_sha1_update(&ctx, input, ilen);
    utils_sha1_finish(&ctx, output);
    utils_sha1_free(&ctx);
}

utils_sha1.h

/*
 * Tencent is pleased to support the open source community by making IoT Hub
 available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.

 * Licensed under the MIT License (the "License"); you may not use this file
 except in
 * compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT

 * Unless required by applicable law or agreed to in writing, software
 distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND,
 * either express or implied. See the License for the specific language
 governing permissions and
 * limitations under the License.
 *
 */

#ifndef QCLOUD_IOT_UTILS_SHA1_H_
#define QCLOUD_IOT_UTILS_SHA1_H_

#include <stdint.h>
#include <stddef.h>

/**
 * \brief          SHA-1 context structure
 */
typedef struct {
    uint32_t      total[2];   /*!< number of bytes processed  */
    uint32_t      state[5];   /*!< intermediate digest state  */
    unsigned char buffer[64]; /*!< data block being processed */
} iot_sha1_context;

/**
 * \brief          Initialize SHA-1 context
 *
 * \param ctx      SHA-1 context to be initialized
 */
void utils_sha1_init(iot_sha1_context *ctx);

/**
 * \brief          Clear SHA-1 context
 *
 * \param ctx      SHA-1 context to be cleared
 */
void utils_sha1_free(iot_sha1_context *ctx);

/**
 * \brief          Clone (the state of) a SHA-1 context
 *
 * \param dst      The destination context
 * \param src      The context to be cloned
 */
void utils_sha1_clone(iot_sha1_context *dst, const iot_sha1_context *src);

/**
 * \brief          SHA-1 context setup
 *
 * \param ctx      context to be initialized
 */
void utils_sha1_starts(iot_sha1_context *ctx);

/**
 * \brief          SHA-1 process buffer
 *
 * \param ctx      SHA-1 context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input, size_t ilen);

/**
 * \brief          SHA-1 final digest
 *
 * \param ctx      SHA-1 context
 * \param output   SHA-1 checksum result
 */
void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20]);

/* Internal use */
void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64]);

/**
 * \brief          Output = SHA-1( input buffer )
 *
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   SHA-1 checksum result
 */
void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]);

#endif

編譯:gcc -o test test.c utils_base64.c utils_sha1.c utils_md5.c utils_hmac.c
執行:./test “IAYFFH3EO2” “dev3” “xjOShbtCetQmvEaJ75RJ1g==”
結果:

這里只是生成對應的引數,如需應用到自己的工程里還需做相應的移植,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347211.html

標籤:其他

上一篇:智慧水務網關解決方案

下一篇:物聯網平臺thingsboard搭建學習記錄

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more