`
Huaqingfly
  • 浏览: 56062 次
社区版块
存档分类
最新评论

MD5摘要算法源码

阅读更多

public class MD5 {
    private static final byte[] PADDING = {
            -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        };
    private static final int S11 = 7;
    private static final int S12 = 12;
    private static final int S13 = 17;
    private static final int S14 = 22;
    private static final int S21 = 5;
    private static final int S22 = 9;
    private static final int S23 = 14;
    private static final int S24 = 20;
    private static final int S31 = 4;
    private static final int S32 = 11;
    private static final int S33 = 16;
    private static final int S34 = 23;
    private static final int S41 = 6;
    private static final int S42 = 10;
    private static final int S43 = 15;
    private static final int S44 = 21;
    private long[] state = new long[4]; // state(ABCD)
    private long[] count = new long[2]; // number of bits, modulo 2^64
    private byte[] buffer = new byte[64]; // input buffer
    private String digestHexStr;
    private byte[] digest = new byte[16];

    public MD5() {
        md5Init();

        return;
    }

    public String getMD5ofString(byte[] inbuf, int buflen) {
        getMD5ofByte(inbuf, buflen);
        digestHexStr = "";

        for (int i = 0; i < 16; i++) {
            digestHexStr += byteHEX(digest[i]);
        }

        return digestHexStr;
    }

    public byte[] getMD5ofByte(byte[] inbuf, int buflen) {
        md5Init();
        md5Update(inbuf, buflen);
        md5Final();

        return digest;
    }

    private void md5Init() {
        count[0] = 0L;
        count[1] = 0L;
        state[0] = 0x67452301L;
        state[1] = 0xefcdab89L;
        state[2] = 0x98badcfeL;
        state[3] = 0x10325476L;

        return;
    }

    private long F(long x, long y, long z) {
        return (x & y) | ((~x) & z);
    }

    private long G(long x, long y, long z) {
        return (x & z) | (y & (~z));
    }

    private long H(long x, long y, long z) {
        return x ^ y ^ z;
    }

    private long I(long x, long y, long z) {
        return y ^ (x | (~z));
    }

    private long FF(long a, long b, long c, long d, long x, long s, long ac) {
        a += (F(b, c, d) + x + ac);
        a = ((int) a << s) | ((int) a >>> (32 - s));
        a += b;

        return a;
    }

    private long GG(long a, long b, long c, long d, long x, long s, long ac) {
        a += (G(b, c, d) + x + ac);
        a = ((int) a << s) | ((int) a >>> (32 - s));
        a += b;

        return a;
    }

    private long HH(long a, long b, long c, long d, long x, long s, long ac) {
        a += (H(b, c, d) + x + ac);
        a = ((int) a << s) | ((int) a >>> (32 - s));
        a += b;

        return a;
    }

    private long II(long a, long b, long c, long d, long x, long s, long ac) {
        a += (I(b, c, d) + x + ac);
        a = ((int) a << s) | ((int) a >>> (32 - s));
        a += b;

        return a;
    }

    private void md5Update(byte[] inbuf, int inputLen) {
        int i;
        int index;
        int partLen;
        byte[] block = new byte[64];
        index = (int) (count[0] >>> 3) & 0x3F;

        // Update number of bits
        if ((count[0] += (inputLen << 3)) < (inputLen << 3)) {
            count[1]++;
        }

        count[1] += (inputLen >>> 29);
        partLen = 64 - index;

        // Transform as many times as possible.
        if (inputLen >= partLen) {
            md5Memcpy(buffer, inbuf, index, 0, partLen);
            md5Transform(buffer);

            for (i = partLen; (i + 63) < inputLen; i += 64) {
                md5Memcpy(block, inbuf, 0, i, 64);
                md5Transform(block);
            }

            index = 0;
        } else {
            i = 0;
        }

        // Buffer remaining input
        md5Memcpy(buffer, inbuf, index, i, inputLen - i);
    }

    private void md5Final() {
        byte[] bits = new byte[8];


分享到:
评论

相关推荐

    MD5摘要计算算法(C++版)源代码

    C++语言实现的MD5加密算法,附有详细的注释说明。

    MD5摘要算法源码(java版)

    一个MD5加密类,Java可直接调用,用于数据加密

    MD5 报文摘要算法 源码

    MD5 报文摘要算法 源码 并带有详细的说明

    md5 信息摘要算法源码

    c语言 md5信息摘要算法源码

    md5摘要算法的C++实现源码

    完整的md5算法实现,readme.txt中有详细的使用说明,移植进项目非常简单。

    实例MD5加密算法VB源代码

    MD5加密算法VB源代码 MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。...

    MD5加密算法-c源代码

    MD5加密算法-c源代码 MD5即Message-Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致。 MD5是输入不定长度信息,输出固定长度128-bits的算法。经过程序流程,生成四个32位数据,最后联合起来成为...

    VC/C++源码,加密解密,MD5,加密算法

    摘要:VC/C++源码,加密解密,MD...VC++源代码写的用于md5加密算法的一个DLL组件源码,虽然是半成品,不过学习一下VC++中编写DLL也是不错的选择,另外还可以研究MD5加密算法具体实施代码。 运行环境:Windows/Visual C/C++

    c++MD5hash摘要源代码

    本程序是c++程序,实现MD5的hash摘要

    linux md5 算法封装

    给文件封装了linux的md5摘要算法,实现了,对字符串和文件求MD5值的算法封装,其中包括源代码和可执行文件,

    MD5 报文摘要算法源码.pdf

    。。。

    MD5 报文摘要算法源码.docx

    。。。

    C# .net MD5算法源代码.rar

    Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and ...

    HMAC-MD5 C语言算法

    HMAC-MD5的c语言实现算法。安全摘要算法。

    HMAC-MD5的c语言实现算法 安全摘要算法

    HMAC-MD5的c语言实现算法。安全摘要算法。

    MD5源码(C++)

    MD5的全称是Message-digest Algorithm 5(信息-摘要算法),用于确保信息传输完整一致。在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc,的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展...

    MD5算法研究Message-Digest Algorithm 5

    MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。它的作用是让大容量...

    MD5的加密解密c#代码

    MD5加密解密工具,就我所知,MD5的目标是生成摘要。严格来说不是一种加密算法。 不管多长的信息都能生成固定长度的MD5编码的话,必然会有信息丢失。那么光有MD5编码的话是绝对不可能还原信息的。 那网上那些MD5解密...

    MD5码 C++算法源代码

    MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。本代码为C++源代码

    易语言源码数据摘要模块(MD5算法实现)飞扬工作室

    易语言源码数据摘要模块(MD5算法实现) 飞扬工作室提取方式是百度网盘分享地址

Global site tag (gtag.js) - Google Analytics