• 【MQTT】阿里云MQTT C++ 版本 登录三元组计算获取username、password


    使用了hash-library开源哈希算法库,https://conan.io/center/hash-library?tab=overview&os=Linux

    全部代码:

    git clone https://github.com/MisakaMikoto128/MqttSignTool.git
    
    • 1

    main.cpp

    #include 
    #include "aliyunmqttpasswordgenerator.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        std::string host = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
        std::string productKey = "a1w78T9VVmM";
        std::string deviceName = "dev1";
        std::string deviceSecret = "1041d8898a6cdc5a851074c1543c98db";
        std::string SN = "SN";
    
        const AliyunMqttPasswordGenerator gen(host,productKey,deviceName,deviceSecret,SN);
        std::cout << "hostName:" << gen.hostName() << std::endl;
        std::cout << "clientId:" << gen.clientId() << std::endl;
        std::cout << "userName:" << gen.userName() << std::endl;
        std::cout << "password:" << gen.password() << std::endl;
        std::cout << "SupportSignMethod:" << gen.getSupportSignMethod()[0] << std::endl;
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    aliyunmqttpasswordgenerator.h

    #ifndef ALIYUNMQTTPASSWORDGENERATOR_H
    #define ALIYUNMQTTPASSWORDGENERATOR_H
    
    #include 
    #include "hash-library/sha1.h"
    #include "hash-library/md5.h"
    #include "hash-library/sha256.h"
    #include "hash-library/hmac.h"
    #include 
    class AliyunMqttPasswordGenerator
    {
    private:
        std::string host;
        std::string productKey;
        std::string deviceName;
        std::string deviceSecret;
        std::string deviceIdentifierString;
    
        std::string timestamp;
        std::string signContent;
        std::string signMethod = "hmacsha256";
    
        static const std::vector<std::string> supportSignMethod;
        int secureMode = 2;
    
        std::string getSignContent();
    public:
        AliyunMqttPasswordGenerator(const std::string&host,const std::string&productKey,const std::string&deviceName,\
                                    const std::string&deviceSecret,const std::string&deviceIdentifierString,\
                                    const std::string& signMethod = "hmacsha256");
    
        void updateTimestamp();
        std::string clientId() const;
        std::string hostName() const;
        std::string userName() const;
        std::string password() const;
        static const std::vector<std::string> &getSupportSignMethod();
        bool signMethodIsSupported(const std::string& signMethod);
    };
    
    #endif // ALIYUNMQTTPASSWORDGENERATOR_H
    
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    aliyunmqttpasswordgenerator.cpp

    #include "aliyunmqttpasswordgenerator.h"
    
    #if defined(_WIN32) || defined(_WIN64)
    //define something for Windows (64-bit only)
    #include 
    #include 
    static uint64_t getCurrentSecTimestamp(){
        time_t t;
        time(&t);
        return t;
    }
    #elif __APPLE__
    #if TARGET_IPHONE_SIMULATOR
    // iOS Simulator
    #elif TARGET_OS_IPHONE
    // iOS device
    #elif TARGET_OS_MAC
    // Other kinds of Mac OS
    #endif
    #elif __ANDROID__
    // android
    #elif __linux__
    // linux
    #include 
    #include 
    static uint64_t g getCurrentSecTimestamp(){
        struct timeval tv;
        gettimeofday(&tv, NULL);
        return tv.tv_sec;
    }
    #elif __unix__ // all unices not caught above
    // Unix
    #elif defined(_POSIX_VERSION)
    // POSIX
    #else
    #error "Unknown"
    #endif
    
    
    
    const std::vector<std::string> AliyunMqttPasswordGenerator::supportSignMethod = {"hmacsha1","hmacmd5","hmacsha256"};
    
    const std::vector<std::string> &AliyunMqttPasswordGenerator::getSupportSignMethod()
    {
        return supportSignMethod;
    }
    
    bool AliyunMqttPasswordGenerator::signMethodIsSupported(const std::string &signMethod)
    {
        bool ret = false;
        for(std::vector<std::string>::const_iterator it = this->supportSignMethod.begin(); it != this->supportSignMethod.end(); it++){
            if(*it == signMethod){
                ret = true;
            }
        }
        return ret;
    }
    
    std::string AliyunMqttPasswordGenerator::getSignContent()
    {
    
        std::string signContent = \
                "clientId"+this->deviceIdentifierString+\
                "deviceName"+this->deviceName+ \
                "productKey"+this->productKey+ \
                "timestamp" +this->timestamp;
        return signContent;
    }
    
    AliyunMqttPasswordGenerator::AliyunMqttPasswordGenerator(const std::string&host,const std::string&productKey,const std::string&deviceName,\
                                                             const std::string&deviceSecret,const std::string&deviceIdentifierString,\
                                                             const std::string& signMethod)
    {
        updateTimestamp();
        this->host = host;
        this->productKey = productKey;
        this->deviceName = deviceName;
        this->deviceSecret = deviceSecret;
    
        this->deviceIdentifierString = deviceIdentifierString;
        if(signMethodIsSupported(signMethod))
        {
            this->signMethod = signMethod;
        }
        //else{ //have a default value.}
    
        this->signContent = getSignContent();
    
    }
    
    void AliyunMqttPasswordGenerator::updateTimestamp()
    {
        timestamp = std::to_string(getCurrentSecTimestamp());
        std::cout << "timestamp" <<getCurrentSecTimestamp()<<std::endl;
    }
    
    std::string AliyunMqttPasswordGenerator::clientId() const
    {
        std::string clientId =
                this->deviceIdentifierString+ \
                "|securemode="  + std::to_string(this->secureMode) +\
                ",signmethod="  + signMethod+\
                ",timestamp="   + timestamp+"|";
        return clientId;
    }
    
    std::string AliyunMqttPasswordGenerator::hostName() const
    {
        return productKey+"."+host;
    }
    
    std::string AliyunMqttPasswordGenerator::userName() const
    {
        return deviceName+"&"+productKey;
    }
    
    std::string AliyunMqttPasswordGenerator::password() const
    {
        std::string sha1hmac;
        if(this->signMethod == this->supportSignMethod[0])
        {
            sha1hmac = hmac<SHA1>(this->signContent, this->deviceSecret);
        }else if(this->signMethod == this->supportSignMethod[1])
        {
            sha1hmac = hmac<MD5>(this->signContent, this->deviceSecret);
        }else if(this->signMethod == this->supportSignMethod[2])
        {
            sha1hmac = hmac<SHA256>(this->signContent, this->deviceSecret);
        }
        return sha1hmac;
    }
    
    
    • 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
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
  • 相关阅读:
    【JAVA并发】一、并发问题产生的根源
    瑞吉外卖项目:新增菜品与菜品分页查询
    12 医疗挂号系统_【 手机登录】
    落地好OKR的8大关键步骤及常用辅助工具
    OpenShift 介绍
    在SQL中修改数据
    大数据学习1.4-xShell配置Hadoop
    程序设计思路-球连球组成的群
    什么是DOM(Document Object Model)?如何使用JavaScript操作DOM元素?
    javaWeb项目-邮票鉴赏系统功能介绍
  • 原文地址:https://blog.csdn.net/qq_42820594/article/details/126283071