gpt4 book ai didi

algorithm - 虚拟机的 CRC32 和 MD5 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:36 24 4
gpt4 key购买 nike

我想自己实现 CRC32 和 MD5 算法,但我仍在努力思考我在该主题上找到的不同来源。有人可以帮我找到一个以简单格式解释算法的资源,或者发布不同步骤的项目符号列表,以便我可以尝试填写它们。TIA。

这是各自的维基百科页面。我了解正在完成的部分工作,但按位运算是我遇到的困难。那和数学不是我的强项。

http://en.wikipedia.org/wiki/Cyclic_redundancy_check
http://en.wikipedia.org/wiki/MD5

最佳答案

RFC-1321 spec关于MD5算法也有详细解释。关于 CRC 的 Wiki 文章非常清楚。

毕竟,您的主要问题显然实际上是对二进制系统和按位运算符的无知。这里有一些关于二进制系统和相关操作符的优秀指南:

这必须让您入门。

编辑:如果您想要自行开发 MD5 函数的真正原因是您实际上似乎无法在 Java 中找到现有函数,那么您可能会发现此代码段很有用:

/**
* Generate MD5 hash for the given String.
* @param string The String to generate the MD5 hash for.
* @return The 32-char hexadecimal MD5 hash of the given String.
*/
public static String hashMD5(String string) {
byte[] hash;

try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
// Unexpected exception. "MD5" is just hardcoded and supported.
throw new RuntimeException("MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
// Unexpected exception. "UTF-8" is just hardcoded and supported.
throw new RuntimeException("UTF-8 should be supported?", e);
}

StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xff) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xff));
}
return hex.toString();
}

关于algorithm - 虚拟机的 CRC32 和 MD5 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1914461/

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