[技术博客]阿里云签名机制字符串的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. Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx)

    Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx) 一丶集群和Nginx反向代理 ...

  2. 左右对齐Justify遇到的坑

    遇到的问题 这两天在开发一个病历的对外展示页面,设计稿上label是左右拉伸对齐的,显示效果如下: 怎么实现这种效果呢? 首先想到的是文字中间加空格,但是这种方式太low了,而且不太容易控制.网上查资 ...

  3. HTML Web Workers

    Web worker 是运行在后台的 JavaScript,不会影响页面的性能. 什么是 Web Worker? 当在 HTML 页面中执行脚本时,页面是不可响应的,直到脚本已完成. Web work ...

  4. Object.entries和Object.fromEntries

    语法 Object.entries(obj) 参数 obj 可以返回其可枚举属性的键值对的对象. 返回值 给定对象自身可枚举属性的键值对数组 语法 Object.fromEntries(iterabl ...

  5. scrapy-redis 0.6.8 配置信息

    很多博客的db参数配置都不能用,所以记录一下该版本可用的配置 #启用Redis调度存储请求队列 SCHEDULER = "scrapy_redis.scheduler.Scheduler&q ...

  6. 【前端_js】jQuery动态设置元素的样式

    1.用css()方法查询元素的某个样式 $("div").css("padding-left")); 2.用css()方法设置元素的样式 法一: $(" ...

  7. docker上启动mysql镜像,mysq中记录乱码解决方法

    在docker上启动一个mysql, 1. docker pull mysql 2. docker run --name mysql_dev -p 3306:3306 -e MYSQL_ROOT_PA ...

  8. Alipay 支付宝SDK PHP Fatal error: Class 'LtObjectUtil' not found in。

    在linux 上运行报错:PHP Fatal error:  Class 'LtObjectUtil' not found in. 这是因为人家用的lotusphp框架,和你正在使用的框架冲突了!比如 ...

  9. JavaScript模拟call和apply的实现

    参考: call和apply的模拟实现 1. call:调用一个对象的一个方法,用另一个对象替换当前对象.例如:B.call(A, args1,args2);即A对象调用B对象的方法. /*call( ...

  10. JDK8 Steam流操作

    原文:https://github.com/niumoo/jdk-feature/blob/master/src/main/java/net/codingme/feature/jdk8/Jdk8Str ...