最近在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生成规则的更多相关文章

  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. NVIDIA安倍架构

    NVIDIA安倍架构 NVIDIA Ampere ArchitectureNVIDIA The Heart of the World's Highest-Performing, Elastic Dat ...

  2. nvGRAPH API参考分析(二)

    nvGRAPH API参考分析(二) nvGRAPH Code Examples 本文提供了简单的示例. 1. nvGRAPH convert topology example void check( ...

  3. Spring Cloud08: Hystrix 容错机制与数据监控

    一.概述 容错机制是指的是在一个分布式系统中,每个微服务之间是相互调用的,并且他们之间相互依赖,而实际的运行情况中,可能会因为各种原因导致某个微服务不可用,那么依赖于这个微服务的其他微服务就可能出现响 ...

  4. Spring Cloud03: Eureka Client 服务提供者

    一.创建一个子工程并引入配置如下: <dependency> <groupId>org.springframework.cloud</groupId> <ar ...

  5. 「题解」NWRRC2017 Joker

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 P7028.gym101612J. 题意概述 有一个长度为 \(n\) 的数列,第 \(i\) 个元素的值为 \(a ...

  6. 超赞!IDEA 最新版本,支持免打扰和轻量模式!

    IntelliJ IDEA 2020.1 的第二个早期访问版本已发布,新的 EAP 构建对调试器和事件探查器(Profiler)进行了改进,并引入了新的提交工具窗口(Commit toolwindow ...

  7. 题解 P6622 [省选联考 2020 A/B 卷] 信号传递

    洛谷 P6622 [省选联考 2020 A/B 卷] 信号传递 题解 某次模拟赛的T2,考场上懒得想正解 (其实是不会QAQ), 打了个暴力就骗了\(30pts\) 就火速溜了,参考了一下某位强者的题 ...

  8. NOIP模拟测试18「引子·可爱宝贝精灵·相互再归的鹅妈妈」

    待补 引子 题解 大模拟,注意细节 代码1 #include<bits/stdc++.h> using namespace std; int n,m;char a[1005][1005]; ...

  9. Java语言实现二维码的生成

    众所周知,现在生活中二维码已经是无处不见.走在街道上,随处可见广告标语旁有二维码,手机上QQ,微信加个好友都能通过二维码的方式,我不知道是什么时候兴起的二维码浪潮,但是我知道,这在我小时候可是见不到的 ...

  10. 在vs中调试关闭之后不关闭页面

    在vs中调试api时会自动打开一个新的浏览器窗口,在关闭这个浏览器窗口时,会关闭调试.关闭调试时也会关闭浏览器窗口. 设置成调试时在已有的浏览器中打开调试页面,关闭调试也不会关掉浏览器窗口,反之亦然 ...