使用PHP获得相同的签名

使用PHP获得相同的签名,php,Php,下面是.net或java中的代码,我不太清楚,但它正在生成一个签名。如何使用php添加相同的值来获得相同的签名 long timestamp = 1499084258; String nonce= "37822614634975090106662"; String httpMethod = "GET"; // HTTP Method of the resource that is being called String encodedResourceUrl = "https://sandbox.

下面是.net或java中的代码,我不太清楚,但它正在生成一个签名。如何使用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=
我想用php获得相同的签名'bqfzzde8jjowjr9/0cY8Lh3Uik=' 我尝试了许多同时使用这些值base 64、sha1和两者的方法,但它生成的签名与此签名不同

正确答案

由于结果不是“bqfzzde8jjowjr9/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);
    }
}
$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);
并按如下方式运行代码:

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());
        }
   }
}
echo bin2hex(base64_decode("bqqFzzDe8jJOwjR9/0cY8Lh3Uik="));
// result -> 6eaa85cf30def2324ec2347dff4718f0b8775229
java代码的输出是

c1iTcvkG2zh5/jCWpsr/VlynCe4=

然后我切换到PHP并编写了以下代码:

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);
    }
}
$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=
这两个输出是相同的,你正在做一些事情 错误,因为bqfzzde8jjowjr9/0cY8Lh3Uik=“不能是 Java代码


我以前的回答

您发布的代码缺少名为
nonce

我首先将结果转换为十六进制,如下所示:

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());
        }
   }
}
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);

这一点也没有被测试过,因为我们发现缺少一个变量

什么是
MessageDigest.digest
?还有什么是
signatureMethod
?请求签名。必须以基数64表示。签名是从由“&”字符分隔的组合定义的数据元素中计算出来的。signtaureMethod是下面的sha1代码:1)获取一组数据(httpMethod、clientId、clientSecretKey和encodedResourceUrl)2)将它们连接起来3)像sha1一样对它们应用哈希算法。4) 将哈希算法生成的签名转换为Base64。请提供您的PHP代码。通常PHP散列函数输出十六进制而不是字节。您是否尝试将十六进制转换为字节,然后对其应用base64。对不起,我的错误是nonce 37822614634975090106662