首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >虚拟人的CRC32和MD5算法

虚拟人的CRC32和MD5算法
EN

Stack Overflow用户
提问于 2009-12-16 20:47:21
回答 3查看 1.4K关注 0票数 2

我想自己实现CRC32和MD5算法,但我仍然在努力理解我在这个主题上找到的不同来源。有没有人能帮我找到一个用简单的格式解释算法的资源,或者发布一个不同步骤的列表,这样我就可以尝试填写它们了。蒂娅。

下面是各自的维基百科页面。我理解正在做的事情的一部分,但是按位操作是我遇到困难的地方。数学不是我的强项。

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

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

EN

回答 3

Stack Overflow用户

发布于 2009-12-16 21:02:52

关于MD5的RFC-1321 spec还包含对算法的详细解释。Wiki上关于CRC的文章已经很清楚了。

毕竟,您的主要问题显然是对二进制和按位运算符的无知。下面是一些关于二进制系统和相关运算符的优秀指南:

这一定能让你入门。

Java :如果您想要生成一个MD5函数的实际原因是,您实际上似乎无法在中找到现有的函数,那么您可能会发现以下代码片段很有用:

代码语言:javascript
复制
/**
 * 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();
}
票数 2
EN

Stack Overflow用户

发布于 2009-12-16 20:57:01

根据DRY的说法,你应该这样做

代码语言:javascript
复制
final public class Security {

    synchronized public static String MD5(String msg) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(msg.getBytes());
            byte[] digest = md.digest();
            return new BigInteger(1, digest).toString(16);
        } catch (NoSuchAlgorithmException ex) {
            return "" + msg.hashCode();
        }
    }
}

但是如果你真的想弄清楚md5 / sha1等是怎么回事,你可能应该学习一门安全课程,我试过了,但失败了:(祝你好运!

票数 1
EN

Stack Overflow用户

发布于 2011-01-12 03:14:59

对于那些感兴趣的人,第一个链接是md5上的rfc文档。第二个是下载Java实现的链接:

http://www.ietf.org/rfc/rfc1321.txt

http://www.freevbcode.com/ShowCode.Asp?ID=741

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1914461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档