douzou0073 2018-09-25 14:44
浏览 63

纠结于在PHP中使用哈希函数生成xml文件的哈希值

I'm trying writing PHP equivalent of JAVA code:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
private static final String ALGORITHM = "HmacSHA256";
private static String generateHash(final InputStream is, final int iteration, final String key) throws IOException,
NoSuchAlgorithmException, InvalidKeyException {
Mac sha256_HMAC;
sha256_HMAC = Mac.getInstance(ALGORITHM);
final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
sha256_HMAC.init(secret_key);
byte[] bytesBuffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(bytesBuffer)) != -1) {
sha256_HMAC.update(bytesBuffer, 0, bytesRead);
}
byte[] digestValue = sha256_HMAC.doFinal();
for (int i = 0; i < iteration; i++) {
sha256_HMAC.reset();
digestValue = sha256_HMAC.doFinal(digestValue);
}
final String generatedHash = Base64.encodeBase64String(digestValue);
return generatedHash;
}

So far I've generated following code which is not generating the expected output:

$ihash = hash_init('SHA256',1,$key);
$filehandle  =fopen('path/to/xml/file','r');
while ($buffer=fread($filehandle,"2048")) {
hash_update($inithash, $buffer);
}

$hash = hash_final($inithash);
for($i=0;$i<$iteration;$i++)
{
$inithash=hash_init('SHA256');
$inithash = hash_final($inithash);
}

echo base64_encode($inithash);

But this is not generating the output expected, I would share the xml file upon asking.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料