将Java代码翻译成Php代码

时间:2016-01-12 11:19:01

标签: java php

我正在尝试编写一个生成签名的示例代码到php,示例是在java上,但我做错了。任何人都可以帮助我吗?

样本是:

String userId; 
String applicationKey; // E.g. "196087a1-e815-4bc4-8984-60d8d8a43f1d";
String applicationSecret; // E.g. "oYdgGRXoxEuJhGDY2KQ/HQ==";
long sequence; // fetch and increment last used sequence
String toSign = userId + applicationKey + sequence + applicationSecret;
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] hash = messageDigest.digest(toSign.getBytes("UTF-8"));
String signature = Base64.encodeBase64String(hash).trim();

我的代码是:

$userId="User";
$applicationKey="MyappKey";
$applicationSecret="MySecretKey";
$sequence=1;
$stringToSign=$userId.$applicationKey.$sequence.$applicationSecret;
$hash = sha1($stringToSign,true);
$signature = trim(base64_encode($hash));

在Java中

MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] hash = messageDigest.digest(toSign.getBytes("UTF-8"));

PHP中的相同

$hash1 = sha1($stringToSign,true);

Base64.encodeBase64String(hash).trim();

中的PHP trim(base64_encode($hash));是相同的

问题是当我执行java代码并获得输出与php代码不同时。

1 个答案:

答案 0 :(得分:0)

尝试

$signature = trim(base64_encode(**$hash**));