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

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

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

  1. bool AlicloudRequest::sendV2Request() {
  2. if( query_parameters.find( "Action" ) == query_parameters.end() ) {
  3. this->errorCode = "E_INTERNAL";
  4. this->errorMessage = "No action specified in request.";
  5. dprintf( D_ALWAYS, "No action specified in request, failing.\n" );
  6. return false;
  7. }
  8. std::string protocol, host, httpRequestURI;
  9. if(! parseURL( serviceURL, protocol, host, httpRequestURI )) {
  10. this->errorCode = "E_INVALID_SERVICE_URL";
  11. this->errorMessage = "Failed to parse service URL.";
  12. dprintf( D_ALWAYS, "Failed to match regex against service URL '%s'.\n", serviceURL.c_str() );
  13. return false;
  14. }
  15. if( (protocol != "http" && protocol != "https" && protocol != "x509" && protocol != "euca3" && protocol != "euca3s" ) ) {
  16. this->errorCode = "E_INVALID_SERVICE_URL";
  17. this->errorMessage = "Service URL not of a known protocol (http[s]|x509|euca3[s]).";
  18. dprintf( D_ALWAYS, "Service URL '%s' not of a known protocol (http[s]|x509|euca3[s]).\n", serviceURL.c_str() );
  19. return false;
  20. }
  21. std::string hostAndPath = host + httpRequestURI;
  22. std::transform( host.begin(), host.end(), host.begin(), & tolower );
  23. if( httpRequestURI.empty() ) { httpRequestURI = "/"; }
  24. if( protocol == "euca3" || protocol == "euca3s" ) {
  25. query_parameters.erase( "InstanceInitiatedShutdownBehavior" );
  26. }
  27. std::string keyID;
  28. if( protocol != "x509" ) {
  29. if( ! readShortFile( this->accessKeyFile, keyID ) ) {
  30. this->errorCode = "E_FILE_IO";
  31. this->errorMessage = "Unable to read from accesskey file '" + this->accessKeyFile + "'.";
  32. dprintf( D_ALWAYS, "Unable to read accesskey file '%s', failing.\n", this->accessKeyFile.c_str() );
  33. return false;
  34. }
  35. trim( keyID );
  36. query_parameters.insert( std::make_pair( "AccessKeyId", keyID ) );
  37. }
  38. std::stringstream ss;
  39. ss<<rand();
  40. std::string randnum = ss.str();
  41. query_parameters.insert( std::make_pair( "SignatureMethod", "HMAC-SHA1" ) );
  42. query_parameters.insert( std::make_pair( "SignatureNonce", randnum ) );
  43. query_parameters.insert( std::make_pair( "SignatureVersion", "1.0" ) );
  44. Throttle::now( & signatureTime );
  45. time_t now; time( & now );
  46. //now+=28800;
  47. struct tm brokenDownTime; gmtime_r( & now, & brokenDownTime );
  48. char iso8601[32];
  49. strftime(iso8601, sizeof(iso8601), "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
  50. query_parameters.insert( std::make_pair( "Timestamp", iso8601 ) );
  51. std::string stringToSign = "POST&%2F&"
  52. //+ host + "\n"
  53. //+ httpRequestURI + "\n"
  54. + alicloudURLEncode(canonicalQueryString);
  55. std::string saKey;
  56. if( protocol == "x509" ) {
  57. saKey = std::string( "not-the-DN" );
  58. dprintf( D_FULLDEBUG, "Using '%s' as secret key for x.509\n", saKey.c_str() );
  59. } else {
  60. if( ! readShortFile( this->secretKeyFile, saKey ) ) {
  61. this->errorCode = "E_FILE_IO";
  62. this->errorMessage = "Unable to read from secretkey file '" + this->secretKeyFile + "'.";
  63. dprintf( D_ALWAYS, "Unable to read secretkey file '%s', failing.\n", this->secretKeyFile.c_str() );
  64. return false;
  65. }
  66. trim( saKey );
  67. }
  68. unsigned int mdLength = 0;
  69. unsigned char messageDigest[EVP_MAX_MD_SIZE];
  70. const unsigned char * hmac = HMAC( EVP_sha1(), saKey.c_str(), saKey.length(),
  71. (const unsigned char *)stringToSign.c_str(), stringToSign.length(), messageDigest, & mdLength );
  72. if( hmac == NULL ) {
  73. this->errorCode = "E_INTERNAL";
  74. this->errorMessage = "Unable to calculate query signature (SHA1 HMAC).";
  75. dprintf( D_ALWAYS, "Unable to calculate SHA1 HMAC to sign query, failing.\n" );
  76. return false;
  77. }
  78. char * base64Encoded = condor_base64_encode( messageDigest, mdLength );
  79. std::string signatureInBase64 = base64Encoded;
  80. free( base64Encoded );
  81. std::string postURI;
  82. if( protocol == "x509" ) {
  83. postURI = "https://" + hostAndPath;
  84. } else if( protocol == "euca3" ) {
  85. postURI = "http://" + hostAndPath;
  86. } else if( protocol == "euca3s" ) {
  87. postURI = "https://" + hostAndPath;
  88. } else {
  89. postURI = this->serviceURL;
  90. }
  91. dprintf( D_FULLDEBUG, "Request URI is '%s'\n", postURI.c_str() );
  92. size_t index = canonicalQueryString.find( "AccessKeyId=" );
  93. if( index != std::string::npos ) {
  94. size_t skipLast = canonicalQueryString.find( "&", index + 14 );
  95. char swap = canonicalQueryString[ index + 15 ];
  96. canonicalQueryString[ index + 15 ] = '\0';
  97. char const * cqs = canonicalQueryString.c_str();
  98. if( skipLast == std::string::npos ) {
  99. dprintf( D_FULLDEBUG, "Post body is '%s...'\n", cqs );
  100. } else {
  101. dprintf( D_FULLDEBUG, "Post body is '%s...%s'\n", cqs, cqs + skipLast );
  102. }
  103. canonicalQueryString[ index + 15 ] = swap;
  104. } else {
  105. dprintf( D_FULLDEBUG, "Post body is '%s'\n", canonicalQueryString.c_str() );
  106. }
  107. return sendPreparedRequest( protocol, postURI, canonicalQueryString );
  108. }

[技术博客]阿里云签名机制字符串的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. 【05】Jenkins:用户权限管理

    写在前面的话 在一个企业研发部门内部,可能存在多个运维人员,而这些运维人员往往负责不同的项目,但是有可能他们用的又是同一个 Jenkins 的不同用户.那么我们就希望实现一个需求,能够不同的用户登录 ...

  2. Spring Boot 运行原理 - 核心注解

    https://www.jianshu.com/p/23f504713b94 核心注解 打开上面任意一个AutoConfiguration文件,一般都有下面的条件注解,在spring-boot-aut ...

  3. idea跳转到指定行列快捷键

    快捷键 Ctrl + G :

  4. Promise 封装 ajax

    Promise 封装ajax 成链式结构: var url = 'http'; function(method, url) { return new Promise(function(res, ret ...

  5. B端产品需求文档怎么写?

    B端,或者2B,一般指的是英文中的 to busniss,中文即面向企业的含义.与B端相对应的,是C端,或者2C,同样指的是英文中的 to customer,即面向消费者的意思.因此,人们平常所说的B ...

  6. OL8.0静默安装Oracle 19C

    首先在edelivery中下载Oracle Linux 8.0 然后就默认安装系统 环境准备工具目前不支持OL8,所以需要手动安装,首先设置内核参数,在/etc/sysctl.conf追加 [root ...

  7. CentOS7升级内核kernel5.0

    升级过程: 原系统:CentOS7.3 [root@my-e450 ~]# uname -r3.10.0-514.el7.x86_64 安装必需的软件包: # yum update# yum inst ...

  8. Mysql读写分离操作

    环境:两台centos环境,安装mysql(mariadb) web网站的优化: 缓存技术 数据库缓存 redis 文件缓存 图片 fastdfs 负载均衡 nginx 数据库主从备份,读写分离 图解 ...

  9. Linux shell 函数应用示例01

    函数Function的使用 定义函数 (1) 函数名称() {     ...     ... } (2) function 函数名称{     ...     ... } 调用函数         ...

  10. springboot中配置urlrewrite实现url伪静态强化网站seo

    关于urlrewrite urlrewrite使用强大的自定义规则来使用用户更容易记住.搜索引擎更容易找到的URL(对于seo比较重要).通过使用规则模板.重写映射,Web管理员可以轻松地设置规则,根 ...