最近在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. CVPR2020:基于自适应采样的非局部神经网络鲁棒点云处理(PointASNL)

    CVPR2020:基于自适应采样的非局部神经网络鲁棒点云处理(PointASNL) PointASNL: Robust Point Clouds Processing Using Nonlocal N ...

  2. 如何在小型pcb的移动设备上获得更好的无线性能

    如何在小型pcb的移动设备上获得更好的无线性能 How to get better wireless performance for mobile devices with small PCBs 小型 ...

  3. 重新整理 mysql 基础篇————— 介绍mysql日志[二]

    前言 对于后端开发来说,打交道最多的应该是数据库了,因为你总得把东西存起来. 或是mongodb或者redis又或是mysql.然后你发现一个问题,就是他们都有日志系统,那么这些日志用来干什么的呢? ...

  4. vue3.0的变化

    初涉vue3.0,下面是我在demo中遇到的一些问题(我是用的vue-cli进行开发) [1]main.js中配置  第一个变化 vue2.x ===  Vue.prototype.$baseURL= ...

  5. 密码学系列之:feistel cipher

    密码学系列之:feistel cipher 简介 feistel cipher也叫做Luby–Rackoff分组密码,是用来构建分组加密算法的对称结构.它是由德籍密码学家Horst Feistel在I ...

  6. 如何使用perf进行程序分析

    1.安装. sudo apt-get install linux-tools 如果提示没有可安装候选.请输入: sudo apt-get install linux-perf-version 其中ve ...

  7. echarts迁移图动态加载

    迁移图 获取迁移城市的经纬度 可以调用高德的接口,实现根据地名找寻经纬度的方法 #!/usr/bin/env python3 #-*- coding:utf-8 -*- ''' 利用高德地图api实现 ...

  8. 1.5w字 + 24张图肝翻 TCP。

    TCP 是一种面向连接的单播协议,在 TCP 中,并不存在多播.广播的这种行为,因为 TCP 报文段中能明确发送方和接受方的 IP 地址. 在发送数据前,相互通信的双方(即发送方和接受方)需要建立一条 ...

  9. 让Github畅通无阻,FastGithub1.0.0发布

    前言 我近半年来被github的抽风虐得没脾气了,虽然我有代理的方式来上网,但代理速度并不理想,而且有时代理服务一起跟着抽风.这时候,我会搜索"github访问不了"相关题材,其中 ...

  10. Pytest学习笔记5-conftest.py的用法

    前言 在之前介绍fixture的文章中,我们使用到了conftest.py文件,那么conftest.py文件到底该如何使用呢,下面我们就来详细了解一下conftest.py文件的特点和使用方法吧 什 ...