最近在Oracle上发现使用hash_hmac()报找不到此函数。为此特意查到oracle的文档。详细请看官网回答:https://cx.rightnow.com/app/answers/detail/a_id/9825/~/cannot-use-the-hash_hmac-function-in-php

原因是:此扩展在 Oracle B2C 服务中未启用,无法启用。就很无语。但是它家自己推出个  Crypto API 也是加密的一个类

下面是 sha256 简单是例子:

oracle云api直通车:https://documentation.custhelp.com/euf/assets/devdocs/cloud21a/Connect_PHP/Default.htm  里面 有详细的  Crypto API 介绍

  1. <?php
  2.  
  3. /**************Agent Authentication**************/
  4. require_once(get_cfg_var("doc_root") . "/ConnectPHP/Connect_init.php" );
  5. initConnectAPI("admin", "adminpwd");
  6.  
  7. /******Use the “Crypto” versioned namespace*********/
  8. use RightNow\Connect\Crypto\v1_4 as Crypto; //这里引入
  9.  
  10. try{
  11. $md = new Crypto\MessageDigest();
  12. $md->Algorithm->ID = 3; //SHA256
  13. $md->Text = "This is a message to be digested using SHA256";
  14. echo "Data : " .$md->Text . "<br>";
  15. $md->hash();
  16. $hashed_text = $md->HashText;
  17. echo "Output : " .bin2Hex($hashed_text)."<br>";
  18. }
  19. catch (Exception $err ){
  20. echo $err->getMessage();
  21. }
  22. ?>

坑①:默认情况下,Crypto API 的 SHA-256 API 没有实现密钥。为了获得与 PHP 的 HASH_HMAC 相同的功能,但是官网给出了解决办法。

  1. 可以使用以下代码在 PHP 中复制此功能:
  2.  
  3. /*
  4. The sample code in this document or accessed through this document is not
  5. certified or supported by Oracle. It is intended for educational or testing
  6. purposes only. Use of this sample code implies acceptance of the License Agreement
  7. at https://www.oracle.com/downloads/licenses/standard-license.html .
  8. */
  9.  
  10. function standard_crypt($msg){
  11. try{
  12. $md = new Crypto\MessageDigest();
  13. $md->Algorithm->ID = 3; // SHA-256
  14. $md->Text = $msg;
  15. $md->hash();
  16. $hashed_text = $md->HashText;
  17. return ($hashed_text);
  18. } catch (Exception $err ){
  19. echo $err->getMessage();
  20. }
  21. }
  22. // Create Signature Hash
  23. function custom_hmac($algo, $data, $key)
  24. {
  25. $size = 64;
  26. $pack = chr(0x00);
  27. if (strlen($key) > $size) {
  28. $key = $algo($key);
  29. } else {
  30. $key = $key . str_repeat(chr(0x00), $size - strlen($key));
  31. }
  32. // Outter and Inner pad
  33. $opad = str_repeat(chr(0x5C), $size);
  34. $ipad = str_repeat(chr(0x36), $size);
  35.  
  36. $k_ipad = $ipad ^ $key;
  37. $k_opad = $opad ^ $key;
  38.  
  39. return $algo($k_opad.$algo($k_ipad.$data));
  40. }
  41.  
  42. $data = "foo";
  43. $secret = "bar";
  44. $bin_hash = custom_hmac('standard_crypt', $data, $secret, false);
  45. echo "HASH: ".bin2hex($bin_hash); //最后从二进制转换成十六进制,但是一般需要的是 base64_encode(),把bin2hex替换就好。

但是我自己也找到了实现秘钥加密的方法,更为简单 (个人推荐这种)并且本人测试过和 php原函数 hash_hmac() 加密效果一样!

  1.  function oauth_hmacsha1($key, $data) {
  2. return base64_encode(hmacsha1($key, $data));
  3.   }
  4. function hmacsha1($key,$data) {
  5. $blocksize=64;
  6. $hashfunc='sha1';
  7. if (strlen($key)>$blocksize)
  8. $key=pack('H*', $hashfunc($key));
  9. $key=str_pad($key,$blocksize,chr(0x00));
  10. $ipad=str_repeat(chr(0x36),$blocksize);
  11. $opad=str_repeat(chr(0x5c),$blocksize);
  12. $hmac = pack(
  13. 'H*',$hashfunc(
  14. ($key^$opad).pack(
  15. 'H*',$hashfunc(
  16. ($key^$ipad).$data
  17. )
  18. )
  19. )
  20. );
  21. return $hmac;
  22. }

坑②:那就是oauth1.0的 oauth_signature 的生成规则 (这里用的sha1加密)其它也类似

  重点1:源串由3部分内容用“&”拼接起来

  HTTP请求方式(对应 GET | POST ) & urlencode(uri) & urlencode(a=x&b=y&...)

  示例代码:

  

  ps:重点中的重点来了!!!!

    post:GET | POST 一定要大写;

    uri也就是例中 $cur: url也就是你的发送请求的 例如:https://5035664-sb2.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=456&deploy=1

    如果请求后面 ?号是携带参数的 script=456&deploy=1像这样的,千万记得 $cur 的值不需要加上参数  https://5035664-sb2.restlets.api.netsuite.com/app/site/hosting/restlet.nl 这样就行。

    script=456&deploy=1 需要放到下面 $paramstring 进行拼接。

    $paramstring :就是 oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1299143758&oauth_nonce=1606024431&oauth_consumer_key=200001。

    ps:$paramstring 拼接的话是一定要按照字典排序从低到高的(切记 !)没有额外参数的话就不用 加上,以  oauth_consumer_key 开头那样!

  1. $paramstring = "deploy=1&oauth_consumer_key=" . $oauth_consumer_key . "&oauth_nonce=" . $oauth_nonce . "&oauth_signature_method=HMAC-SHA1" . "&oauth_timestamp=" . $oauth_timestamp . "&oauth_token=" . $oauth_token ."&oauth_version=1.0&script=456";

  1. //密钥
    $secret = urlencode($oauth_consumer_secret) ."&".urlencode($oauth_token_secret);

    $oauth_signature = base64_encode(hmacsha1($secret,$paramstring )); //带入上面自定义加密函数

    以上就是生成 $oauth_signature 的注意事项了!

    最后,其实在这个问题上我卡了好好几天,菜的抠脚,而且能找的文章少之又少。所以才写了这么一篇博客来希望后面的人能快速找到问题!如果对你有帮助的话,记得点个赞再走^_^!!!

Oracle中使用hash_hmac() 函数报错问题/以及Oracle遇到Oauth1.0授权和oauth_signature生成规则的更多相关文章

  1. Oracle中建立物化视图报错

    Oracle中建立物化视图报错 今天在建立视图的时候,报了一个错:ORA-01723: zero-length columns are not allowed. 建视图的语句: create mate ...

  2. Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function

    因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...

  3. 解决VS2017中使用scanf函数报错的问题

    我们在VS2017中如果使用C语言的scanf输入函数,编译的时候编译器会报error C4996: 'scanf': This function or variable may be unsafe. ...

  4. FrameWork模型中引入宏函数报错解决方法

    如下图在Framework的一个简单维度中加入宏函数 解决办法如下图 step1: step2: PS :Cognos 10.1.1中 在cognos connection中创建数据源,为什么没有od ...

  5. Pychram中使用reduce()函数报错:Unresolved reference 'reduce'

    python3不能直接使用reduce()函数,因为reduce() 函数已经被从全局名字空间里移除了,它现在被放置在fucntools 模块里,所以要使用reduce函数得先饮用fucntools ...

  6. oracle中的trim()函数详解

    1.先看一下Oracle TRIM函数的完整语法描述 TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} ...

  7. oracle中的greatest 函数和 least函数

    oracle中的greatest 函数和 least函数 原文地址:https://blog.csdn.net/sinat_32023305/article/details/78778596    g ...

  8. RedHat中敲sh-copy-id命令报错:-bash: ssh-copy-id: command not found

    RedHat中敲sh-copy-id命令报错:-bash: ssh-copy-id: command not found 在多台Linux服务器SSH相互访问无需密码, 其中进入一台Linus中,对其 ...

  9. Oracle存储过程跨用户执行查询报错

    在Oracle中,在USERA下编写一个存储过程,该存储过程中引用了另一个用户USERB下的表或视图对象.编译该存储过程,出现编译错误.报ORA-00942: table or view does n ...

随机推荐

  1. Steam游戏《Northgard(北境之地)》修改器制作

    日期:2021.06.07 博客期:181 星期一 [温馨提示]: 我现在把资源先放到开头,不想研究学习的就直接取用.如果修改器失效了,你们可以在博客园本页直接评论,也可以给我发邮件告诉我,就是不要到 ...

  2. mybatis-plus 分页查询+ dao层抽象

    1.配置文件添加paginationInterceptor @Configuration @MapperScan("fama.cost.*.mapper") public clas ...

  3. YOLO v1到YOLO v4(上)

    YOLO v1到YOLO v4(上) 一.  YOLO v1 这是继RCNN,fast-RCNN和faster-RCNN之后,rbg(RossGirshick)针对DL目标检测速度问题提出的另外一种框 ...

  4. MinkowskiEngine多GPU训练

    MinkowskiEngine多GPU训练 目前,MinkowskiEngine通过数据并行化支持Multi-GPU训练.在数据并行化中,有一组微型批处理,这些微型批处理将被送到到网络的一组副本中. ...

  5. CodeGen API分析

    CodeGen API分析 作为使用命令行界面的替代方法,开发人员可以使用核心CodeGen环境编写自定义工具或实用程序来生成代码,从而将CodeGen更紧密地集成到开发环境中. 为了实现这一点,Co ...

  6. Docker App应用

    Docker App应用 这是一个实验特性. 实验性功能提供了对未来产品功能的早期访问.这些特性仅用于测试和反馈,因为它们可能在没有警告的情况下在不同版本之间更改,或者可以从将来的版本中完全删除.在生 ...

  7. Kaggle上的犬种识别(ImageNet Dogs)

    Kaggle上的犬种识别(ImageNet Dogs) Dog Breed Identification (ImageNet Dogs) on Kaggle 在本节中,将解决在Kaggle竞赛中的犬种 ...

  8. CUDA刷新器:CUDA编程模型

    CUDA刷新器:CUDA编程模型 CUDA Refresher: The CUDA Programming Model CUDA,CUDA刷新器,并行编程 这是CUDA更新系列的第四篇文章,它的目标是 ...

  9. 【NX二次开发】开发好几年,还只会用ufusr?其他用户出口函数介绍

    用户出口(User Exit)是NX Open 中的一个重要概念.NX在运行过程中某些特定的位置存在规定的出口,当进程执行到这些出口时,NX会自动检查用户是否在此处已定义了指向内部程序位置的环境变量: ...

  10. C# 24点游戏求解算法

    经常跟儿子玩24点,有时候比较难算的,算一会儿,两人算不出来,就收了,当作没法算. 以我的数学能力,一般来说,算不出来的,大概率确实是算不出来的. 但是遇到比较变态的,当作算不出来是可能的,所以一直想 ...