[技术博客]阿里云签名机制字符串的C语言实现

[技术博客]阿里云签名机制字符串的C语言实现

问题描述见:阿里云签名机制

话不多说,上字符串函数转化函数代码

bool AlicloudRequest::sendV2Request() {
    if( query_parameters.find( "Action" ) == query_parameters.end() ) {
        this->errorCode = "E_INTERNAL";
        this->errorMessage = "No action specified in request.";
        dprintf( D_ALWAYS, "No action specified in request, failing.\n" );
        return false;
    }
    std::string protocol, host, httpRequestURI;
    if(! parseURL( serviceURL, protocol, host, httpRequestURI )) {
        this->errorCode = "E_INVALID_SERVICE_URL";
        this->errorMessage = "Failed to parse service URL.";
        dprintf( D_ALWAYS, "Failed to match regex against service URL '%s'.\n", serviceURL.c_str() );
        return false;
    }

    if( (protocol != "http" && protocol != "https" && protocol != "x509" && protocol != "euca3" && protocol != "euca3s" ) ) {
        this->errorCode = "E_INVALID_SERVICE_URL";
        this->errorMessage = "Service URL not of a known protocol (http[s]|x509|euca3[s]).";
        dprintf( D_ALWAYS, "Service URL '%s' not of a known protocol (http[s]|x509|euca3[s]).\n", serviceURL.c_str() );
        return false;
    }
    std::string hostAndPath = host + httpRequestURI;
    std::transform( host.begin(), host.end(), host.begin(), & tolower );
    if( httpRequestURI.empty() ) { httpRequestURI = "/"; }
    if( protocol == "euca3" || protocol == "euca3s" ) {
        query_parameters.erase( "InstanceInitiatedShutdownBehavior" );
    }
    std::string keyID;
    if( protocol != "x509" ) {
        if( ! readShortFile( this->accessKeyFile, keyID ) ) {
            this->errorCode = "E_FILE_IO";
            this->errorMessage = "Unable to read from accesskey file '" + this->accessKeyFile + "'.";
            dprintf( D_ALWAYS, "Unable to read accesskey file '%s', failing.\n", this->accessKeyFile.c_str() );
            return false;
        }
        trim( keyID );
        query_parameters.insert( std::make_pair( "AccessKeyId", keyID ) );
    }
    std::stringstream ss;
    ss<<rand();
    std::string randnum = ss.str();

    query_parameters.insert( std::make_pair( "SignatureMethod", "HMAC-SHA1" ) );
    query_parameters.insert( std::make_pair( "SignatureNonce", randnum ) );
    query_parameters.insert( std::make_pair( "SignatureVersion", "1.0" ) );

    Throttle::now( & signatureTime );
    time_t now; time( & now );
    //now+=28800;
    struct tm brokenDownTime; gmtime_r( & now, & brokenDownTime );
    char iso8601[32];
    strftime(iso8601, sizeof(iso8601), "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
    query_parameters.insert( std::make_pair( "Timestamp", iso8601 ) );

    std::string stringToSign = "POST&%2F&"
                             //+ host + "\n"
                             //+ httpRequestURI + "\n"
                             + alicloudURLEncode(canonicalQueryString);

    std::string saKey;
    if( protocol == "x509" ) {
        saKey = std::string( "not-the-DN" );
        dprintf( D_FULLDEBUG, "Using '%s' as secret key for x.509\n", saKey.c_str() );
    } else {
        if( ! readShortFile( this->secretKeyFile, saKey ) ) {
            this->errorCode = "E_FILE_IO";
            this->errorMessage = "Unable to read from secretkey file '" + this->secretKeyFile + "'.";
            dprintf( D_ALWAYS, "Unable to read secretkey file '%s', failing.\n", this->secretKeyFile.c_str() );
            return false;
        }
        trim( saKey );
    }

    unsigned int mdLength = 0;
    unsigned char messageDigest[EVP_MAX_MD_SIZE];

    const unsigned char * hmac = HMAC( EVP_sha1(), saKey.c_str(), saKey.length(),
        (const unsigned char *)stringToSign.c_str(), stringToSign.length(), messageDigest, & mdLength );
    if( hmac == NULL ) {
        this->errorCode = "E_INTERNAL";
        this->errorMessage = "Unable to calculate query signature (SHA1 HMAC).";
        dprintf( D_ALWAYS, "Unable to calculate SHA1 HMAC to sign query, failing.\n" );
        return false;
    }

    char * base64Encoded = condor_base64_encode( messageDigest, mdLength );
    std::string signatureInBase64 = base64Encoded;
    free( base64Encoded );

    std::string postURI;
    if( protocol == "x509" ) {
        postURI = "https://" + hostAndPath;
    } else if( protocol == "euca3" ) {
        postURI = "http://" + hostAndPath;
    } else if( protocol == "euca3s" ) {
        postURI = "https://" + hostAndPath;
    } else {
        postURI = this->serviceURL;
    }
    dprintf( D_FULLDEBUG, "Request URI is '%s'\n", postURI.c_str() );

    size_t index = canonicalQueryString.find( "AccessKeyId=" );
    if( index != std::string::npos ) {
        size_t skipLast = canonicalQueryString.find( "&", index + 14 );
        char swap = canonicalQueryString[ index + 15 ];
        canonicalQueryString[ index + 15 ] = '\0';
        char const * cqs = canonicalQueryString.c_str();
        if( skipLast == std::string::npos ) {
            dprintf( D_FULLDEBUG, "Post body is '%s...'\n", cqs );
        } else {
            dprintf( D_FULLDEBUG, "Post body is '%s...%s'\n", cqs, cqs + skipLast );
        }
        canonicalQueryString[ index + 15 ] = swap;
    } else {
        dprintf( D_FULLDEBUG, "Post body is '%s'\n", canonicalQueryString.c_str() );
    }
    return sendPreparedRequest( protocol, postURI, canonicalQueryString );
}

原文地址:https://www.cnblogs.com/ws-1st/p/10994960.html

时间: 2024-08-11 21:53:47

[技术博客]阿里云签名机制字符串的C语言实现的相关文章

几个阿里技术博客URL

阿里核心系统团队博客:http://csrd.aliapp.com/ 阿里中间件团队博客:http://jm-blog.aliapp.com/?p=2968 阿里个人技术博客:http://blog.csdn.net/fenglibing/article/details/6193127 阿里技术嘉年华官网:http://www.cnblogs.com/liping13599168/archive/2012/06/07/2540878.html http://www.csdn.net/articl

国内各大互联网公司相关技术博客3.0版 (集合腾讯、阿里、百度、搜狐、新浪、网易、360等共29个)

在2013-07-15 整理了一份国内各大互联网公司相关技术站点2.0版 (集合腾讯.阿里.百度.搜狐.新浪.360等共49个) 近日重新整理了一番,希望能对大家有所帮助 2013年 腾讯系列(13)  阿里系列(18)  百度系列(3)  搜狐系列(3)  新浪系列(2)  360系列(2)   其他(9) 2016年 腾讯系列(9)  阿里系列(5)  百度系列(6)  搜狐系列(1)  新浪系列(1)  360系列(2)   其他(3) 新增 网易(2) 腾讯系列(9) 1.财付通设计中心

[转]有哪些值得关注的技术博客(Java篇)

有哪些值得关注的技术博客(Java篇) 大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1.总结知识点相对比较全面 2.一般来说讲解知识点通俗易懂 3.路线比较清晰,不会有太多的冗余内容. 这样一来,对于自学的朋友来说,一些专业的博客不但大量的缩减了你得学习时间.提高了学习效率.更重要的是这些博客能培养你对编程的兴趣. 于是,这几期文章会主要推荐一些技术博客,目前计划打算主要分为: (已完) 

最值得收藏的java技术博客(Java篇)

第一个:java_my_life 作者介绍:找不到原作者信息.大概做了翻阅全部是2012年的博客. 博客主要内容:主要内容是关于Java设计模式的一些讲解和学习笔记,在相信对学习设计模式的同学帮助很大.在一望无际的Java博文中有那么一两篇JavaScript文章,就算两篇Javascript的质量不是很高,其它教程也是能很好的帮助到在学习中的朋友. 博客关键词:JAVA与模式.简单工厂模式.工厂方法.抽象工厂.单例.建造.原型.适配器.合成.迭代.观察者.模板方法.策略.不变.桥梁.门面.代理

IT公司技术博客地址

IT公司技术博客 美团点评技术团队今日头条技术博客Tencent ISUX DesignTGideas-腾讯互动娱乐创意设计团队>AlloyTeam | 腾讯全端 AlloyTeam 团队 Blog阿里巴巴国际UED团队阿里巴巴(中国站)用户体验设计部博客U一点设计 UED团队优质博文集锦-云栖社区-阿里云 原文地址:https://www.cnblogs.com/yang75n/p/10510474.html

一系列国内外顶尖互联网公司的技术博客,晋升程序员必备!

顶尖公司是大多数程序员都梦寐以求进入的地方,但是技术不行怎么办?学啊!这里收集了一系列国内外顶尖互联网公司的技术博客,好好给自己充充电! 国内技术博客列表 腾讯游戏 DBA 团队 http://tencentdba.com 腾讯互娱游戏 DBA 团队一直致力于为游戏提供稳定.高效的 DB 运营服务.现有的 MySQL 社区版已经不能满足快速发展中的游戏业务需求.于是该团队在 MySQL 5.5 基础上,开发和维护了 MySQL 的新分支 TenDB. 腾讯 AlloyTeam http://ww

转: BAT等研发团队的技术博客

BAT 技术团队博客 1. 美团技术团队博客:  地址: http://tech.meituan.com/ 2. 腾讯社交用户体验设计(ISUX) 地址:http://isux.tencent.com/ 3. 京东设计中心 地址:http://jdc.jd.com 4. QQ游戏设计中心 地址:http://gdc.qq.com 5. 百度搜索用户体验中心 地址:http://ued.baidu.com/ 6. 一淘UX 地址:http://ux.etao.com 7. 腾讯CDC 地址:htt

价值博客们,技术博客

www.raychase.net http://mindhacks.cn 程序员博客墙blogwall.us http://www.cppblog.com/vczh MacTalk-池建强的随想录 Fenng DBA Notes | 闲思录robbin的自言自语风雪之隅-Laruence的博客 blog.vgod.tw 张琮翔的Blog:愛好電腦.科技.程式設計,目前在MIT電腦科學與人工智慧實驗室就讀博士班,尋找人機互動與程式設計交會的創新火花. MIT CSAIL的PhD,现在毕业开始创业了

春哥技术博客来谈谈php源码加密那点事

今天春哥技术博客给大家谈谈源码加密的那些事.加密就是阻碍进步的,asp.net就是因为都编译后发布,导致优秀源码少. php源码比较常用有 Zend公司的ZendGuard 和 ionCube公司的ionCube PHP Encode.这两个加密都不能独立运行.要分别在ZendOptimizer和 ionCube PHP Accelerator的PHP引擎下才能正常解码运行.如果不想在第三方引擎下加密可以用下面方法:看代码 function encode_file_contents($filen