【问题标题】:PHP equivalent of Java MessageDigest MD5相当于 Java MessageDigest MD5 的 PHP
【发布时间】:2012-03-06 12:11:34
【问题描述】:

我在下面的 java 中有这个方法,并且需要在 PHP 中完全等效,因为正在比较两个哈希..

Java 方法是:

public String getMD5(String inStr)
{
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
    }
    char[] charArray = inStr.toCharArray();
    byte[] byteArray = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++)
        byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
    }

我目前正在使用php的crypt方法。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: java php hash md5


    【解决方案1】:

    这不适合你吗?

    $str = 'apple';
    $hash = md5($str);
    

    这将在 php.ini 中生成一个 md5 哈希。两个函数的输出不相等吗?

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2010-12-09
      • 1970-01-01
      • 2021-04-05
      • 2013-03-07
      相关资源
      最近更新 更多