gpt4 book ai didi

Java 消息摘要 [MD5] 未生成预期的哈希值

转载 作者:行者123 更新时间:2023-12-01 18:41:12 25 4
gpt4 key购买 nike

我尝试使用以下代码生成 MD5 十六进制哈希:

String password = "password";

MessageDigest digest = MessageDigest.getInstance("MD5");

ByteArrayInputStream bais = new ByteArrayInputStream(password.getBytes());

int size = 16;
byte[] bytes = new byte[size];
while ((bais.read(bytes, 0, size)) != -1)
{
digest.update(bytes);
}

byte[] hash = digest.digest();
StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash)
{
sb.append(String.format("%02x", b & 0xff));
}

System.out.println("MD5:/ " + sb.toString());

输出应该是5f4dcc3b5aa765d61d8327deb882cf99(用md5sum检查),但我看不出错误在哪里。我做错了什么?

最佳答案

我不知道你的有什么问题,但这应该有效:

byte[] array = MessageDigest.getInstance("MD5").digest("password".getBytes("UTF-8"));              
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
}
System.out.println(sb.toString());

关于Java 消息摘要 [MD5] 未生成预期的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com