MD5 Digest : Message Digest « Security « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Security » Message DigestScreenshots 
MD5 Digest
 
/******************************************************************************
 * Copyright (c) 2000 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/


/**
 * implementation of MD5 as outlined in "Handbook of Applied Cryptography", pages 346 - 347.
 */
public class MD5Digest
    extends GeneralDigest
{
    private static final int    DIGEST_LENGTH = 16;

    private int     H1, H2, H3, H4;         // IV's

    private int[]   X = new int[16];
    private int     xOff;

    /**
     * Standard constructor
     */
    public MD5Digest()
    {
        reset();
    }

    /**
     * Copy constructor.  This will copy the state of the provided
     * message digest.
     */
    public MD5Digest(MD5Digest t)
    {
        super(t);

        H1 = t.H1;
        H2 = t.H2;
        H3 = t.H3;
        H4 = t.H4;

        System.arraycopy(t.X, 0, X, 0, t.X.length);
        xOff = t.xOff;
    }

    public String getAlgorithmName()
    {
        return "MD5";
    }

    public int getDigestSize()
    {
        return DIGEST_LENGTH;
    }

    protected void processWord(
        byte[]  in,
        int     inOff)
    {
        X[xOff++(in[inOff0xff((in[inOff + 10xff<< 8)
            ((in[inOff + 20xff<< 16((in[inOff + 30xff<< 24)

        if (xOff == 16)
        {
            processBlock();
        }
    }

    protected void processLength(
        long    bitLength)
    {
        if (xOff > 14)
        {
            processBlock();
        }

        X[14(int)(bitLength & 0xffffffff);
        X[15(int)(bitLength >>> 32);
    }

    private void unpackWord(
        int     word,
        byte[]  out,
        int     outOff)
    {
        out[outOff]     (byte)word;
        out[outOff + 1(byte)(word >>> 8);
        out[outOff + 2(byte)(word >>> 16);
        out[outOff + 3(byte)(word >>> 24);
    }

    public int doFinal(
        byte[]  out,
        int     outOff)
    {
        finish();

        unpackWord(H1, out, outOff);
        unpackWord(H2, out, outOff + 4);
        unpackWord(H3, out, outOff + 8);
        unpackWord(H4, out, outOff + 12);

        reset();

        return DIGEST_LENGTH;
    }

    /**
     * reset the chaining variables to the IV values.
     */
    public void reset()
    {
        super.reset();

        H1 = 0x67452301;
        H2 = 0xefcdab89;
        H3 = 0x98badcfe;
        H4 = 0x10325476;

        xOff = 0;

        for (int i = 0; i != X.length; i++)
        {
            X[i0;
        }
    }

    //
    // round 1 left rotates
    //
    private static final int S11 = 7;
    private static final int S12 = 12;
    private static final int S13 = 17;
    private static final int S14 = 22;

    //
    // round 2 left rotates
    //
    private static final int S21 = 5;
    private static final int S22 = 9;
    private static final int S23 = 14;
    private static final int S24 = 20;

    //
    // round 3 left rotates
    //
    private static final int S31 = 4;
    private static final int S32 = 11;
    private static final int S33 = 16;
    private static final int S34 = 23;

    //
    // round 4 left rotates
    //
    private static final int S41 = 6;
    private static final int S42 = 10;
    private static final int S43 = 15;
    private static final int S44 = 21;

    /*
     * rotate int x left n bits.
     */
    private int rotateLeft(
        int x,
        int n)
    {
        return (x << n(x >>> (32 - n));
    }

    /*
     * F, G, H and I are the basic MD5 functions.
     */
    private int F(
        int u,
        int v,
        int w)
    {
        return (u & v(~u & w);
    }

    private int G(
        int u,
        int v,
        int w)
    {
        return (u & w(v & ~w);
    }

    private int H(
        int u,
        int v,
        int w)
    {
        return u ^ v ^ w;
    }

    private int K(
        int u,
        int v,
        int w)
    {
        return v ^ (u | ~w);
    }

    protected void processBlock()
    {
        int a = H1;
        int b = H2;
        int c = H3;
        int d = H4;

        //
        // Round 1 - F cycle, 16 times.
        //
        a = rotateLeft(a + F(b, c, d+ X00xd76aa478, S11+ b;
        d = rotateLeft(d + F(a, b, c+ X10xe8c7b756, S12+ a;
        c = rotateLeft(c + F(d, a, b+ X20x242070db, S13+ d;
        b = rotateLeft(b + F(c, d, a+ X30xc1bdceee, S14+ c;
        a = rotateLeft(a + F(b, c, d+ X40xf57c0faf, S11+ b;
        d = rotateLeft(d + F(a, b, c+ X50x4787c62a, S12+ a;
        c = rotateLeft(c + F(d, a, b+ X60xa8304613, S13+ d;
        b = rotateLeft(b + F(c, d, a+ X70xfd469501, S14+ c;
        a = rotateLeft(a + F(b, c, d+ X80x698098d8, S11+ b;
        d = rotateLeft(d + F(a, b, c+ X90x8b44f7af, S12+ a;
        c = rotateLeft(c + F(d, a, b+ X[100xffff5bb1, S13+ d;
        b = rotateLeft(b + F(c, d, a+ X[110x895cd7be, S14+ c;
        a = rotateLeft(a + F(b, c, d+ X[120x6b901122, S11+ b;
        d = rotateLeft(d + F(a, b, c+ X[130xfd987193, S12+ a;
        c = rotateLeft(c + F(d, a, b+ X[140xa679438e, S13+ d;
        b = rotateLeft(b + F(c, d, a+ X[150x49b40821, S14+ c;

        //
        // Round 2 - G cycle, 16 times.
        //
        a = rotateLeft(a + G(b, c, d+ X10xf61e2562, S21+ b;
        d = rotateLeft(d + G(a, b, c+ X60xc040b340, S22+ a;
        c = rotateLeft(c + G(d, a, b+ X[110x265e5a51, S23+ d;
        b = rotateLeft(b + G(c, d, a+ X00xe9b6c7aa, S24+ c;
        a = rotateLeft(a + G(b, c, d+ X50xd62f105d, S21+ b;
        d = rotateLeft(d + G(a, b, c+ X[100x02441453, S22+ a;
        c = rotateLeft(c + G(d, a, b+ X[150xd8a1e681, S23+ d;
        b = rotateLeft(b + G(c, d, a+ X40xe7d3fbc8, S24+ c;
        a = rotateLeft(a + G(b, c, d+ X90x21e1cde6, S21+ b;
        d = rotateLeft(d + G(a, b, c+ X[140xc33707d6, S22+ a;
        c = rotateLeft(c + G(d, a, b+ X30xf4d50d87, S23+ d;
        b = rotateLeft(b + G(c, d, a+ X80x455a14ed, S24+ c;
        a = rotateLeft(a + G(b, c, d+ X[130xa9e3e905, S21+ b;
        d = rotateLeft(d + G(a, b, c+ X20xfcefa3f8, S22+ a;
        c = rotateLeft(c + G(d, a, b+ X70x676f02d9, S23+ d;
        b = rotateLeft(b + G(c, d, a+ X[120x8d2a4c8a, S24+ c;

        //
        // Round 3 - H cycle, 16 times.
        //
        a = rotateLeft(a + H(b, c, d+ X50xfffa3942, S31+ b;
        d = rotateLeft(d + H(a, b, c+ X80x8771f681, S32+ a;
        c = rotateLeft(c + H(d, a, b+ X[110x6d9d6122, S33+ d;
        b = rotateLeft(b + H(c, d, a+ X[140xfde5380c, S34+ c;
        a = rotateLeft(a + H(b, c, d+ X10xa4beea44, S31+ b;
        d = rotateLeft(d + H(a, b, c+ X40x4bdecfa9, S32+ a;
        c = rotateLeft(c + H(d, a, b+ X70xf6bb4b60, S33+ d;
        b = rotateLeft(b + H(c, d, a+ X[100xbebfbc70, S34+ c;
        a = rotateLeft(a + H(b, c, d+ X[130x289b7ec6, S31+ b;
        d = rotateLeft(d + H(a, b, c+ X00xeaa127fa, S32+ a;
        c = rotateLeft(c + H(d, a, b+ X30xd4ef3085, S33+ d;
        b = rotateLeft(b + H(c, d, a+ X60x04881d05, S34+ c;
        a = rotateLeft(a + H(b, c, d+ X90xd9d4d039, S31+ b;
        d = rotateLeft(d + H(a, b, c+ X[120xe6db99e5, S32+ a;
        c = rotateLeft(c + H(d, a, b+ X[150x1fa27cf8, S33+ d;
        b = rotateLeft(b + H(c, d, a+ X20xc4ac5665, S34+ c;

        //
        // Round 4 - K cycle, 16 times.
        //
        a = rotateLeft(a + K(b, c, d+ X00xf4292244, S41+ b;
        d = rotateLeft(d + K(a, b, c+ X70x432aff97, S42+ a;
        c = rotateLeft(c + K(d, a, b+ X[140xab9423a7, S43+ d;
        b = rotateLeft(b + K(c, d, a+ X50xfc93a039, S44+ c;
        a = rotateLeft(a + K(b, c, d+ X[120x655b59c3, S41+ b;
        d = rotateLeft(d + K(a, b, c+ X30x8f0ccc92, S42+ a;
        c = rotateLeft(c + K(d, a, b+ X[100xffeff47d, S43+ d;
        b = rotateLeft(b + K(c, d, a+ X10x85845dd1, S44+ c;
        a = rotateLeft(a + K(b, c, d+ X80x6fa87e4f, S41+ b;
        d = rotateLeft(d + K(a, b, c+ X[150xfe2ce6e0, S42+ a;
        c = rotateLeft(c + K(d, a, b+ X60xa3014314, S43+ d;
        b = rotateLeft(b + K(c, d, a+ X[130x4e0811a1, S44+ c;
        a = rotateLeft(a + K(b, c, d+ X40xf7537e82, S41+ b;
        d = rotateLeft(d + K(a, b, c+ X[110xbd3af235, S42+ a;
        c = rotateLeft(c + K(d, a, b+ X20x2ad7d2bb, S43+ d;
        b = rotateLeft(b + K(c, d, a+ X90xeb86d391, S44+ c;

        H1 += a;
        H2 += b;
        H3 += c;
        H4 += d;

        //
        // reset the offset and clean out the word buffer.
        //
        xOff = 0;
        for (int i = 0; i != X.length; i++)
        {
            X[i0;
        }
    }
}

/**
 * base implementation of MD4 family style digest as outlined in
 * "Handbook of Applied Cryptography", pages 344 - 347.
 *
 @version $Id: GeneralDigest.java,v 1.2 2004/06/27 17:00:55 bheineman Exp $
 */
abstract class GeneralDigest
//    implements Digest - mdb: we don't care about this interface
{
    private byte[]  xBuf;
    private int     xBufOff;

    private long    byteCount;

  /**
   * Standard constructor
   */
  protected GeneralDigest()
  {
    xBuf = new byte[4];
    xBufOff = 0;
  }

  /**
   * Copy constructor.  We are using copy constructors in place
   * of the Object.clone() interface as this interface is not
   * supported by J2ME.
   */
  protected GeneralDigest(GeneralDigest t)
  {
        xBuf = new byte[t.xBuf.length];
    System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);

    xBufOff = t.xBufOff;
    byteCount = t.byteCount;
  }

    public void update(
        byte in)
    {
        xBuf[xBufOff++= in;

        if (xBufOff == xBuf.length)
        {
            processWord(xBuf, 0);
            xBufOff = 0;
        }

        byteCount++;
    }

    public void update(
        byte[]  in,
        int     inOff,
        int     len)
    {
        //
        // fill the current word
        //
        while ((xBufOff != 0&& (len > 0))
        {
            update(in[inOff]);

            inOff++;
            len--;
        }

        //
        // process whole words.
        //
        while (len > xBuf.length)
        {
            processWord(in, inOff);

            inOff += xBuf.length;
            len -= xBuf.length;
            byteCount += xBuf.length;
        }

        //
        // load in the remainder.
        //
        while (len > 0)
        {
            update(in[inOff]);

            inOff++;
            len--;
        }
    }

    public void finish()
    {
        long    bitLength = (byteCount << 3);

        //
        // add the pad bytes.
        //
        update((byte)128);

        while (xBufOff != 0)
        {
            update((byte)0);
        }

        processLength(bitLength);

        processBlock();
    }

    public void reset()
    {
        byteCount = 0;

        xBufOff = 0;
    for int i = 0; i < xBuf.length; i++ ) {
      xBuf[i0;
    }
    }

    protected abstract void processWord(byte[] in, int inOff);

    protected abstract void processLength(long bitLength);

    protected abstract void processBlock();
}

   
  
Related examples in the same category
1. MessageDigest with SHA-1
2. Create a checksum
3. Operations to simplifiy common java.security.MessageDigest tasks.
4. Digest string
5. MD4 Digest
6. Implements the MD4 message digest algorithm in Java
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.