[技术博客]阿里云签名机制字符串的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 );
}
[技术博客]阿里云签名机制字符串的C语言实现的更多相关文章
- [技术博客] 用户验证码验证机制---redis缓存数据库的使用
目录 问题引入 初识redis 实际应用 作者:马振亚 问题引入 在这次的开发过程中,我们的需求中有一个是普通用户可以通过特定的机制申请成为社长.因为只有部分人才能验证成功,所以这个最开始想了两种思路 ...
- 【技术博客】JWT的认证机制Django项目中应用
开发组在开发过程中,都不可避免地遇到了一些困难或问题,但都最终想出办法克服了.我们认为这样的经验是有必要记录下来的,因此就有了[技术博客]. JWT的认证机制Django项目中应用 这篇技术博客基于软 ...
- [转]有哪些值得关注的技术博客(Java篇)
有哪些值得关注的技术博客(Java篇) 大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1. ...
- 最值得收藏的java技术博客(Java篇)
第一个:java_my_life 作者介绍:找不到原作者信息.大概做了翻阅全部是2012年的博客. 博客主要内容:主要内容是关于Java设计模式的一些讲解和学习笔记,在相信对学习设计模式的同学帮助很大 ...
- 技术人如何利用 github+Jekyll ,搭建一个独立免费的技术博客
上次有人留言说,技术博客是程序员的标配,但据我所知绝大部分技术同学到现在仍然没有自己的技术博客.原因有很多,有的是懒的写,有的是怕写不好,还有的是一直想憋个大招,幻想做到完美再发出来,结果一直胎死腹中 ...
- IT公司技术博客地址
IT公司技术博客 美团点评技术团队今日头条技术博客Tencent ISUX DesignTGideas-腾讯互动娱乐创意设计团队>AlloyTeam | 腾讯全端 AlloyTeam 团队 Bl ...
- 不可思议的hexo,五分钟教你免费搭一个高逼格技术博客
引言 作为程序员拥有一个属于自己的个人技术博客,绝对是百利无一害的事,不仅方便出门装b,面试时亮出博客地址也会让面试官对你的好感度倍增.经常能在很多大佬的技术文章的文末,看到这样一句话: " ...
- 技术博客——微信小程序的架构与原理
技术博客--微信小程序的架构与原理 在两个月的微信小程序开发过程中,我曾走了不少弯路,也曾被很多现在看来十分可笑的问题所困扰.这些弯路与困扰,基本上都是由于当时对小程序的架构理解不够充分,对小程序的原 ...
- [技术博客] 敏捷软工——JavaScript踩坑记
[技术博客] 敏捷软工--JavaScript踩坑记 一.一个令人影响深刻的坑 1.脚本语言的面向对象 面向对象特性是现代编程语言的基本特性,JavaScript中当然集成了面向对象特性.但是Java ...
随机推荐
- 【05】Jenkins:用户权限管理
写在前面的话 在一个企业研发部门内部,可能存在多个运维人员,而这些运维人员往往负责不同的项目,但是有可能他们用的又是同一个 Jenkins 的不同用户.那么我们就希望实现一个需求,能够不同的用户登录 ...
- Spring Boot 运行原理 - 核心注解
https://www.jianshu.com/p/23f504713b94 核心注解 打开上面任意一个AutoConfiguration文件,一般都有下面的条件注解,在spring-boot-aut ...
- idea跳转到指定行列快捷键
快捷键 Ctrl + G :
- Promise 封装 ajax
Promise 封装ajax 成链式结构: var url = 'http'; function(method, url) { return new Promise(function(res, ret ...
- B端产品需求文档怎么写?
B端,或者2B,一般指的是英文中的 to busniss,中文即面向企业的含义.与B端相对应的,是C端,或者2C,同样指的是英文中的 to customer,即面向消费者的意思.因此,人们平常所说的B ...
- OL8.0静默安装Oracle 19C
首先在edelivery中下载Oracle Linux 8.0 然后就默认安装系统 环境准备工具目前不支持OL8,所以需要手动安装,首先设置内核参数,在/etc/sysctl.conf追加 [root ...
- CentOS7升级内核kernel5.0
升级过程: 原系统:CentOS7.3 [root@my-e450 ~]# uname -r3.10.0-514.el7.x86_64 安装必需的软件包: # yum update# yum inst ...
- Mysql读写分离操作
环境:两台centos环境,安装mysql(mariadb) web网站的优化: 缓存技术 数据库缓存 redis 文件缓存 图片 fastdfs 负载均衡 nginx 数据库主从备份,读写分离 图解 ...
- Linux shell 函数应用示例01
函数Function的使用 定义函数 (1) 函数名称() { ... ... } (2) function 函数名称{ ... ... } 调用函数 ...
- springboot中配置urlrewrite实现url伪静态强化网站seo
关于urlrewrite urlrewrite使用强大的自定义规则来使用用户更容易记住.搜索引擎更容易找到的URL(对于seo比较重要).通过使用规则模板.重写映射,Web管理员可以轻松地设置规则,根 ...