本文介绍了如何在PHP中重现Java MessageDigest SHA-256哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已获得以下Java代码段,以作为如何基于提供的'in'和'salt'变量计算哈希值的示例.在这些示例中,变量经过硬编码以进行测试:

package generatehash;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

public class GenerateHash {

    public static void main(String[] args)
    {
        String in = "abcdef12345";
        String salt = "test1";
        try {
            MessageDigest hash = MessageDigest.getInstance("SHA-256");
            byte[] digest = hash.digest((in + salt).getBytes());
            String out = new BASE64Encoder().encode(digest);
            System.out.println("Calculated: " + out);
        } catch(java.security.NoSuchAlgorithmException e) {
            System.err.println("SHA-256 is not a valid message digest algorithm. " + e.toString());
        }
    }
}

这里的输出是:

当尝试运行等效的PHP时,我尝试了以下操作:

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);

$in = 'abcdef12345';
$salt = 'test1';

$out = hash('sha256', $in.$salt);

echo 'Calculated: ' . $out;

这里的输出完全不同:

我尝试了多种变体,但没有达到目标.这里有我想念的东西吗?任何帮助将不胜感激.

解决方案

Java结果在base64中,而php结果在十六进制中.您错过了对 raw PHP结果进行base64编码的步骤.

$in = 'abcdef12345';
$salt = 'test1';

$out = hash('sha256', $in.$salt,true); //3rd parameter says return raw result
echo 'Calculated: ' . base64_encode($out);

输出:

示例: http://sandbox.onlinephpfunctions.com/code/bb12ed98c16e2b732f29292da75aeebc36da2d48>

I currently have been given the following java code snippet as an example of how to calculate a hash based on a supplied 'in' and 'salt' variable. In these examples the variables are hardcoded for testing:

package generatehash;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

public class GenerateHash {

    public static void main(String[] args)
    {
        String in = "abcdef12345";
        String salt = "test1";
        try {
            MessageDigest hash = MessageDigest.getInstance("SHA-256");
            byte[] digest = hash.digest((in + salt).getBytes());
            String out = new BASE64Encoder().encode(digest);
            System.out.println("Calculated: " + out);
        } catch(java.security.NoSuchAlgorithmException e) {
            System.err.println("SHA-256 is not a valid message digest algorithm. " + e.toString());
        }
    }
}

The output here is:

When attempting to run the PHP equivalent I tried the following:

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);

$in = 'abcdef12345';
$salt = 'test1';

$out = hash('sha256', $in.$salt);

echo 'Calculated: ' . $out;

The output here is completely different:

I've tried a number of variations but not hitting the mark. Is there something I'm missing here? Any help would be greatly appreciated.

The Java result is in base64 while the php result is in hex. You missed the step of base64 encoding the raw PHP result .

$in = 'abcdef12345';
$salt = 'test1';

$out = hash('sha256', $in.$salt,true); //3rd parameter says return raw result
echo 'Calculated: ' . base64_encode($out);

Outputs:

Example:http://sandbox.onlinephpfunctions.com/code/bb12ed98c16e2b732f29292da75aeebc36da2d48

这篇关于如何在PHP中重现Java MessageDigest SHA-256哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 13:00