[技术博客]阿里云签名机制字符串的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 );
}

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

  1. [技术博客] 用户验证码验证机制---redis缓存数据库的使用

    目录 问题引入 初识redis 实际应用 作者:马振亚 问题引入 在这次的开发过程中,我们的需求中有一个是普通用户可以通过特定的机制申请成为社长.因为只有部分人才能验证成功,所以这个最开始想了两种思路 ...

  2. 【技术博客】JWT的认证机制Django项目中应用

    开发组在开发过程中,都不可避免地遇到了一些困难或问题,但都最终想出办法克服了.我们认为这样的经验是有必要记录下来的,因此就有了[技术博客]. JWT的认证机制Django项目中应用 这篇技术博客基于软 ...

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

    有哪些值得关注的技术博客(Java篇)   大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1. ...

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

    第一个:java_my_life 作者介绍:找不到原作者信息.大概做了翻阅全部是2012年的博客. 博客主要内容:主要内容是关于Java设计模式的一些讲解和学习笔记,在相信对学习设计模式的同学帮助很大 ...

  5. 技术人如何利用 github+Jekyll ,搭建一个独立免费的技术博客

    上次有人留言说,技术博客是程序员的标配,但据我所知绝大部分技术同学到现在仍然没有自己的技术博客.原因有很多,有的是懒的写,有的是怕写不好,还有的是一直想憋个大招,幻想做到完美再发出来,结果一直胎死腹中 ...

  6. IT公司技术博客地址

    IT公司技术博客 美团点评技术团队今日头条技术博客Tencent ISUX DesignTGideas-腾讯互动娱乐创意设计团队>AlloyTeam | 腾讯全端 AlloyTeam 团队 Bl ...

  7. 不可思议的hexo,五分钟教你免费搭一个高逼格技术博客

    引言 作为程序员拥有一个属于自己的个人技术博客,绝对是百利无一害的事,不仅方便出门装b,面试时亮出博客地址也会让面试官对你的好感度倍增.经常能在很多大佬的技术文章的文末,看到这样一句话: " ...

  8. 技术博客——微信小程序的架构与原理

    技术博客--微信小程序的架构与原理 在两个月的微信小程序开发过程中,我曾走了不少弯路,也曾被很多现在看来十分可笑的问题所困扰.这些弯路与困扰,基本上都是由于当时对小程序的架构理解不够充分,对小程序的原 ...

  9. [技术博客] 敏捷软工——JavaScript踩坑记

    [技术博客] 敏捷软工--JavaScript踩坑记 一.一个令人影响深刻的坑 1.脚本语言的面向对象 面向对象特性是现代编程语言的基本特性,JavaScript中当然集成了面向对象特性.但是Java ...

随机推荐

  1. Java中转换为二进制的几种实现

    public class HexUtil { private static final String[] DIGITS_UPPER = {"0", "1", & ...

  2. c# 类实例序列化反序列化json文件 (原发布 csdn 2017-10-01 20:02:12)

    前言 前段时间使用了net.json保存对象数据.添加完成后,测试发现300多实例数据保存加载json文件,速度比原方式(BinaryFormatter)慢.但是功能加上后也懒再删掉代码了,索性就采用 ...

  3. asp.net webapi 随笔

    第一次写博客,文笔有限,记录下学习的过程 话不多说,直接开干 首先用vs2017建立一个空网站项目,然后只勾选api 项目建立后,如下结构 其中WebApiConfig类配置了路由相关信息 publi ...

  4. 【转】Visual Studio Code(VS code)你们都在用吗?或许你们需要看一下这篇博文

    写在前面 在前端开发中,有一个非常好用的工具,Visual Studio Code,简称VS code. 都不用我安利VS code,大家就会乖乖的去用,无数个大言不惭的攻城狮,都被VS code比德 ...

  5. disconf的简单使用与远程配置更改为使用本地配置

    这几天因为阿里云迁移到腾讯云的原因,原来服务器上的disconf不再使用了.在这段时间里,系统出现的bug很难寻找原因(项目起不来),现在想要把disconf远程配置更改成直接使用本地配置.首先,了解 ...

  6. windows 2012 IIS 部署 .net core HTTP Error 502.5 - Process Failure 错误解决办法

    安装系统补丁: 2012 R2对应>Windows8.1-KB2999226-x64.msu 下载地址: https://www.microsoft.com/zh-CN/download/det ...

  7. 学习CSS Grid布局

    一. 重要术语: CSS Grid(网格) 布局(又称为 "Grid(网格)" ),是一个二维的基于网格的布局系统,它的目标是完全改变我们基于网格的用户界面的布局方式. FlexB ...

  8. 微服务架构 ------ 插曲 hikari连接池的配置

    开胃菜:据说hikari连接池很快,快到让另一个连接池的作者抛弃对自己连接池的维护,并且强烈推荐使用hikari 连接池目前我们项目使用的有两个 一个是Druid , 一个是 Hikari, 其中Dr ...

  9. 【开发笔记】- Grails框架定义一个不是数据库字段得属性

    实体类 class Book{ String name String author // myfiled 我不想他在数据库中生成book表的字段 String myfield } 添加声明 class ...

  10. H5新增form控件和表单属性

    第一个知识点:表单的属性及总结 第二个知识点:H5新增的表单控件和属性以及总结 第一个知识点: 我们常见的表单验证有哪些呢 text 文本框标签 password 密码框 checkbox 多选框 r ...