2017-07-03 74 views
-1

你好下面是代碼.NET或Java我並不確切地知道,但它是生成簽名我如何通過使用PHP獲取相同的簽名使用PHP

long timestamp = 1499084258; 
String nonce= "37822614634975090106662"; 
String httpMethod = "GET"; // HTTP Method of the resource that is being called 
String encodedResourceUrl = "https://sandbox.interswitchng.com/api/v2/quickteller/categorys"; // put the resource URL here 
String clientId = "IKIA9D981C53698A71925002C81E09104959B975G5C41E1"; // put your client Id here 
String clientSecretKey = "d5uAr+U8QhSvYu0809vQtKop3kRslRBC5Q+SwIt+/r4nk+y0="; // put your client secret here 
String signatureCipher = httpMethod + "&" + encodedResourceUrl + "&" + timestamp + "&" + nonce + "&" + clientId + "&" + clientSecretKey; 
MessageDigest messageDigest = MessageDigest.getInstance(signatureMethod); 
byte[] signatureBytes = messageDigest.digest(signatureCipher.getBytes()); 
String signature = new String(Base64.encodeBase64(signatureBytes)); 

signature = bqqFzzDe8jJOwjR9/0cY8Lh3Uik= 

我想加入相同的價值觀得到相同的簽名以獲得相同的簽名「bqqFzzDe8jJOwjR9/0cY8Lh3Uik =」用PHP 我試圖使用這些值底座64,SHA1和二者同時許多方法,但它會產生從該簽名

+0

什麼是'MessageDigest.digest'?又是什麼'signatureMethod'? – Justinas

+0

請求籤名。必須用基數64表示。簽名是由用'&'字符分隔的組合定義的數據元素計算得出的。 –

+0

和signtaureMethod是SHA1 –

回答

0

正確答案不同

你有一個錯誤的地方如結果不"bqqFzzDe8jJOwjR9/0cY8Lh3Uik="

我剛剛解僱了我的Java IDE,並寫了下面的代碼:

package com.company; 

import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.Base64; 

public class HashingExample { 

    public void run() throws NoSuchAlgorithmException { 

     MessageDigest md = MessageDigest.getInstance("SHA-1"); 

     long timestamp = 1499084258; 
     String nonce= "37822614634975090106662"; 
     String httpMethod = "GET"; // HTTP Method of the resource that is being called 
     String encodedResourceUrl = "https://sandbox.interswitchng.com/api/v2/quickteller/categorys"; // put the resource URL here 
     String clientId = "IKIA9D981C53698A71925002C81E09104959B975G5C41E1"; // put your client Id here 
     String clientSecretKey = "d5uAr+U8QhSvYu0809vQtKop3kRslRBC5Q+SwIt+/r4nk+y0="; // put your client secret here 
     String signatureCipher = httpMethod + "&" + encodedResourceUrl + "&" + timestamp + "&" + nonce + "&" + clientId + "&" + clientSecretKey; 

     byte[] signatureBytes = md.digest(signatureCipher.getBytes()); 
     String signature = new String(Base64.getEncoder().encode(signatureBytes)); 

     System.out.print(signature); 
    } 
} 

和運行如下代碼:

package com.company; 
public class Main { 

    public static void main(String[] args) { 

     HashingExample hashingExample = new HashingExample(); 
     try { 
      hashingExample.run(); 
     }catch(Exception e){ 
      System.out.print(e.getMessage()); 
     } 
    } 
} 

java代碼的輸出是

c1iTcvkG2zh5/jCWpsr/VlynCe4 =

然後我切換到PHP和寫以下代碼:

$timestamp = 1499084258; 
$nonce = "37822614634975090106662"; 
$httpMethod = "GET"; // HTTP Method of the resource that is being called 
$encodedResourceUrl = "https://sandbox.interswitchng.com/api/v2/quickteller/categorys"; // put the resource URL here 
$clientId = "IKIA9D981C53698A71925002C81E09104959B975G5C41E1"; // put your client Id here 
$clientSecretKey = "d5uAr+U8QhSvYu0809vQtKop3kRslRBC5Q+SwIt+/r4nk+y0="; // put your client secret here 
$signatureCipher = $httpMethod . "&" . $encodedResourceUrl . "&" . $timestamp . "&" . $nonce . "&" . $clientId . "&" . $clientSecretKey; 
$messageDigest = sha1($signatureCipher, true); 

echo "\n"; 
echo "My hex: " . bin2hex($messageDigest); 
echo "\n"; 
echo "My base64: " . base64_encode($messageDigest); 

和輸出是

//My hex: 73589372f906db3879fe3096a6caff565ca709ee 
//My base64: c1iTcvkG2zh5/jCWpsr/VlynCe4= 

兩個輸出是相同的,因爲你正在做一些事情 錯誤,因爲"bqqFzzDe8jJOwjR9/0cY8Lh3Uik="不能是 的Java代碼的結果。


我以前的答覆

你貼缺少稱爲nonce

我已經開始變的轉換你的結果爲十六進制的代碼如下:

echo bin2hex(base64_decode("bqqFzzDe8jJOwjR9/0cY8Lh3Uik=")); 
// result -> 6eaa85cf30def2324ec2347dff4718f0b8775229 

由於我們缺少名爲nonce的變量,因此不可能獲得相同的簽名。然而,這是我會怎麼做:

$timestamp = 1499084258; 

$httpMethod = "GET"; // HTTP Method of the resource that is being called 
$encodedResourceUrl = "https://sandbox.interswitchng.com/api/v2/quickteller/categorys"; // put the resource URL here 
$clientId = "IKIA9D981C53698A71925002C81E09104959B975G5C41E1"; // put your client Id here 
$clientSecretKey = "d5uAr+U8QhSvYu0809vQtKop3kRslRBC5Q+SwIt+/r4nk+y0="; // put your client secret here 

$signatureCipher = $httpMethod + "&" + $encodedResourceUrl + "&" + $timestamp + "&" + $nonce + "&" + $clientId + "&" + $clientSecretKey; 

$messageDigest = hash('sha1', $signatureCipher); 
// this will give a hex value. Without `nonce` we will get 
// 9edc77e0745da0f98b71251435c90cb51a4a8840 

$bytes = hex2bin($messageDigest); 

echo base64_encode($bytes); 

同樣,這沒有經過測試,因爲我們一個變量缺失

+0

對不起,我的錯誤是現成的 37822614634975090106662 –