Oracle中使用hash_hmac() 函数报错问题/以及Oracle遇到Oauth1.0授权和oauth_signature生成规则
最近在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 介绍
- <?php
- /**************Agent Authentication**************/
- require_once(get_cfg_var("doc_root") . "/ConnectPHP/Connect_init.php" );
- initConnectAPI("admin", "adminpwd");
- /******Use the “Crypto” versioned namespace*********/
- use RightNow\Connect\Crypto\v1_4 as Crypto; //这里引入
- try{
- $md = new Crypto\MessageDigest();
- $md->Algorithm->ID = 3; //SHA256
- $md->Text = "This is a message to be digested using SHA256";
- echo "Data : " .$md->Text . "<br>";
- $md->hash();
- $hashed_text = $md->HashText;
- echo "Output : " .bin2Hex($hashed_text)."<br>";
- }
- catch (Exception $err ){
- echo $err->getMessage();
- }
- ?>
坑①:默认情况下,Crypto API 的 SHA-256 API 没有实现密钥。为了获得与 PHP 的 HASH_HMAC 相同的功能,但是官网给出了解决办法。
- 可以使用以下代码在 PHP 中复制此功能:
- /*
- The sample code in this document or accessed through this document is not
- certified or supported by Oracle. It is intended for educational or testing
- purposes only. Use of this sample code implies acceptance of the License Agreement
- at https://www.oracle.com/downloads/licenses/standard-license.html .
- */
- function standard_crypt($msg){
- try{
- $md = new Crypto\MessageDigest();
- $md->Algorithm->ID = 3; // SHA-256
- $md->Text = $msg;
- $md->hash();
- $hashed_text = $md->HashText;
- return ($hashed_text);
- } catch (Exception $err ){
- echo $err->getMessage();
- }
- }
- // Create Signature Hash
- function custom_hmac($algo, $data, $key)
- {
- $size = 64;
- $pack = chr(0x00);
- if (strlen($key) > $size) {
- $key = $algo($key);
- } else {
- $key = $key . str_repeat(chr(0x00), $size - strlen($key));
- }
- // Outter and Inner pad
- $opad = str_repeat(chr(0x5C), $size);
- $ipad = str_repeat(chr(0x36), $size);
- $k_ipad = $ipad ^ $key;
- $k_opad = $opad ^ $key;
- return $algo($k_opad.$algo($k_ipad.$data));
- }
- $data = "foo";
- $secret = "bar";
- $bin_hash = custom_hmac('standard_crypt', $data, $secret, false);
- echo "HASH: ".bin2hex($bin_hash); //最后从二进制转换成十六进制,但是一般需要的是 base64_encode(),把bin2hex替换就好。
但是我自己也找到了实现秘钥加密的方法,更为简单 (个人推荐这种)并且本人测试过和 php原函数 hash_hmac() 加密效果一样!
- function oauth_hmacsha1($key, $data) {
- return base64_encode(hmacsha1($key, $data));
- }
- function hmacsha1($key,$data) {
- $blocksize=64;
- $hashfunc='sha1';
- if (strlen($key)>$blocksize)
- $key=pack('H*', $hashfunc($key));
- $key=str_pad($key,$blocksize,chr(0x00));
- $ipad=str_repeat(chr(0x36),$blocksize);
- $opad=str_repeat(chr(0x5c),$blocksize);
- $hmac = pack(
- 'H*',$hashfunc(
- ($key^$opad).pack(
- 'H*',$hashfunc(
- ($key^$ipad).$data
- )
- )
- )
- );
- return $hmac;
- }
坑②:那就是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 开头那样!
- $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";
//密钥
$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生成规则的更多相关文章
- Oracle中建立物化视图报错
Oracle中建立物化视图报错 今天在建立视图的时候,报了一个错:ORA-01723: zero-length columns are not allowed. 建视图的语句: create mate ...
- Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function
因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...
- 解决VS2017中使用scanf函数报错的问题
我们在VS2017中如果使用C语言的scanf输入函数,编译的时候编译器会报error C4996: 'scanf': This function or variable may be unsafe. ...
- FrameWork模型中引入宏函数报错解决方法
如下图在Framework的一个简单维度中加入宏函数 解决办法如下图 step1: step2: PS :Cognos 10.1.1中 在cognos connection中创建数据源,为什么没有od ...
- Pychram中使用reduce()函数报错:Unresolved reference 'reduce'
python3不能直接使用reduce()函数,因为reduce() 函数已经被从全局名字空间里移除了,它现在被放置在fucntools 模块里,所以要使用reduce函数得先饮用fucntools ...
- oracle中的trim()函数详解
1.先看一下Oracle TRIM函数的完整语法描述 TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} ...
- oracle中的greatest 函数和 least函数
oracle中的greatest 函数和 least函数 原文地址:https://blog.csdn.net/sinat_32023305/article/details/78778596 g ...
- 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中,对其 ...
- Oracle存储过程跨用户执行查询报错
在Oracle中,在USERA下编写一个存储过程,该存储过程中引用了另一个用户USERB下的表或视图对象.编译该存储过程,出现编译错误.报ORA-00942: table or view does n ...
随机推荐
- Steam游戏《Northgard(北境之地)》修改器制作
日期:2021.06.07 博客期:181 星期一 [温馨提示]: 我现在把资源先放到开头,不想研究学习的就直接取用.如果修改器失效了,你们可以在博客园本页直接评论,也可以给我发邮件告诉我,就是不要到 ...
- mybatis-plus 分页查询+ dao层抽象
1.配置文件添加paginationInterceptor @Configuration @MapperScan("fama.cost.*.mapper") public clas ...
- YOLO v1到YOLO v4(上)
YOLO v1到YOLO v4(上) 一. YOLO v1 这是继RCNN,fast-RCNN和faster-RCNN之后,rbg(RossGirshick)针对DL目标检测速度问题提出的另外一种框 ...
- MinkowskiEngine多GPU训练
MinkowskiEngine多GPU训练 目前,MinkowskiEngine通过数据并行化支持Multi-GPU训练.在数据并行化中,有一组微型批处理,这些微型批处理将被送到到网络的一组副本中. ...
- CodeGen API分析
CodeGen API分析 作为使用命令行界面的替代方法,开发人员可以使用核心CodeGen环境编写自定义工具或实用程序来生成代码,从而将CodeGen更紧密地集成到开发环境中. 为了实现这一点,Co ...
- Docker App应用
Docker App应用 这是一个实验特性. 实验性功能提供了对未来产品功能的早期访问.这些特性仅用于测试和反馈,因为它们可能在没有警告的情况下在不同版本之间更改,或者可以从将来的版本中完全删除.在生 ...
- Kaggle上的犬种识别(ImageNet Dogs)
Kaggle上的犬种识别(ImageNet Dogs) Dog Breed Identification (ImageNet Dogs) on Kaggle 在本节中,将解决在Kaggle竞赛中的犬种 ...
- CUDA刷新器:CUDA编程模型
CUDA刷新器:CUDA编程模型 CUDA Refresher: The CUDA Programming Model CUDA,CUDA刷新器,并行编程 这是CUDA更新系列的第四篇文章,它的目标是 ...
- 【NX二次开发】开发好几年,还只会用ufusr?其他用户出口函数介绍
用户出口(User Exit)是NX Open 中的一个重要概念.NX在运行过程中某些特定的位置存在规定的出口,当进程执行到这些出口时,NX会自动检查用户是否在此处已定义了指向内部程序位置的环境变量: ...
- C# 24点游戏求解算法
经常跟儿子玩24点,有时候比较难算的,算一会儿,两人算不出来,就收了,当作没法算. 以我的数学能力,一般来说,算不出来的,大概率确实是算不出来的. 但是遇到比较变态的,当作算不出来是可能的,所以一直想 ...