CRC32和MD5算法傻瓜 [英] CRC32 and MD5 algorithms for dummies

查看:348
本文介绍了CRC32和MD5算法傻瓜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我自己的CRC32和MD5算法,但我还是想环绕我关于这个问题找到了不同的来源我的头。可能有人乐于助人点我去的ressource,说明在一个简单的格式算法或邮寄的不同步骤的项目符号列表,以便我可以尝试,以填补他们。TIA。

I'd like to implement the CRC32 and MD5 algorithms on my own but I'm still trying to wrap my head around the different sources I've found on the subject. Could someone helpful point me to a ressource that explains the algorithms in a simple format or post a bullet list of the different steps so I can attempt to fill them in. TIA.

下面是每个相应的维基百科页面。我了解正在采取哪些措施年代,但按位运算的东西,我有困难的一部分。这和数学是不是我的专长。

Here's the respective wikipedia pages on each. I understand part of what's being done but bitwise operations are something I have difficulty with. That and mathematics isn't my forte.

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

推荐答案

RFC-1321规范关于MD5还包含算法中的详细说明。有关CRC维基文章是pretty的清楚。

The RFC-1321 spec about MD5 also contains a detailed explanation of the algo. The Wiki article about CRC is pretty clear enough.

毕竟,你的主要问题显然是实际上对双星系统和位运算符的无知。以下是有关二元体系的几个优秀导游员和所涉及的运营商:

After all, your major problem is apparently actually the ignorance about the binary system and the bitwise operators. Here are several excellent guides about the binary system and the involved operators:

  • Guide: The Binary System
  • Wikipedia: Bitwise operation
  • Javaranch: Bit Shifting

这应该让你开始。

修改:如果你想homegrow一个MD5函数的实际原因是,你实际上似乎无法找到Java中的现有功能,那么你会发现这个片段很有用:

Edit: if the actual reason that you wanted to homegrow a MD5 function is that you actually can't seem to find an existing function in Java, then you may find this snippet useful:

/**
 * 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();
}

这篇关于CRC32和MD5算法傻瓜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆