Java Program to Implement the MD5 Algorithm

This is a java program to implement MD5 algorithm. The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.

Here is the source code of the Java Program to Implement the MD5 Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. public class MD5
  5. {
  6.     private static final int   INIT_A     = 0x67452301;
  7.     private static final int   INIT_B     = (int) 0xEFCDAB89L;
  8.     private static final int   INIT_C     = (int) 0x98BADCFEL;
  9.     private static final int   INIT_D     = 0x10325476;
  10.     private static final int[] SHIFT_AMTS = { 7, 12, 17, 22, 5, 9, 14, 20, 4,
  11.             11, 16, 23, 6, 10, 15, 21    };
  12.     private static final int[] TABLE_T    = new int[64];
  13.     static
  14.     {
  15.         for (int i = 0; i < 64; i++)
  16.             TABLE_T[i] = (int) (long) ((1L << 32) * Math.abs(Math.sin(i + 1)));
  17.     }
  18.  
  19.     public static byte[] computeMD5(byte[] message)
  20.     {
  21.         int messageLenBytes = message.length;
  22.         int numBlocks = ((messageLenBytes + 8) >>> 6) + 1;
  23.         int totalLen = numBlocks << 6;
  24.         byte[] paddingBytes = new byte[totalLen - messageLenBytes];
  25.         paddingBytes[0] = (byte) 0x80;
  26.         long messageLenBits = (long) messageLenBytes << 3;
  27.         for (int i = 0; i < 8; i++)
  28.         {
  29.             paddingBytes[paddingBytes.length - 8 + i] = (byte) messageLenBits;
  30.             messageLenBits >>>= 8;
  31.         }
  32.         int a = INIT_A;
  33.         int b = INIT_B;
  34.         int c = INIT_C;
  35.         int d = INIT_D;
  36.         int[] buffer = new int[16];
  37.         for (int i = 0; i < numBlocks; i++)
  38.         {
  39.             int index = i << 6;
  40.             for (int j = 0; j < 64; j++, index++)
  41.                 buffer[j >>> 2] = ((int) ((index < messageLenBytes) ? message[index]
  42.                         : paddingBytes[index - messageLenBytes]) << 24)
  43.                         | (buffer[j >>> 2] >>> 8);
  44.             int originalA = a;
  45.             int originalB = b;
  46.             int originalC = c;
  47.             int originalD = d;
  48.             for (int j = 0; j < 64; j++)
  49.             {
  50.                 int div16 = j >>> 4;
  51.                 int f = 0;
  52.                 int bufferIndex = j;
  53.                 switch (div16)
  54.                 {
  55.                     case 0:
  56.                         f = (b & c) | (~b & d);
  57.                         break;
  58.                     case 1:
  59.                         f = (b & d) | (c & ~d);
  60.                         bufferIndex = (bufferIndex * 5 + 1) & 0x0F;
  61.                         break;
  62.                     case 2:
  63.                         f = b ^ c ^ d;
  64.                         bufferIndex = (bufferIndex * 3 + 5) & 0x0F;
  65.                         break;
  66.                     case 3:
  67.                         f = c ^ (b | ~d);
  68.                         bufferIndex = (bufferIndex * 7) & 0x0F;
  69.                         break;
  70.                 }
  71.                 int temp = b
  72.                         + Integer.rotateLeft(a + f + buffer[bufferIndex]
  73.                                 + TABLE_T[j],
  74.                                 SHIFT_AMTS[(div16 << 2) | (j & 3)]);
  75.                 a = d;
  76.                 d = c;
  77.                 c = b;
  78.                 b = temp;
  79.             }
  80.             a += originalA;
  81.             b += originalB;
  82.             c += originalC;
  83.             d += originalD;
  84.         }
  85.         byte[] md5 = new byte[16];
  86.         int count = 0;
  87.         for (int i = 0; i < 4; i++)
  88.         {
  89.             int n = (i == 0) ? a : ((i == 1) ? b : ((i == 2) ? c : d));
  90.             for (int j = 0; j < 4; j++)
  91.             {
  92.                 md5[count++] = (byte) n;
  93.                 n >>>= 8;
  94.             }
  95.         }
  96.         return md5;
  97.     }
  98.  
  99.     public static String toHexString(byte[] b)
  100.     {
  101.         StringBuilder sb = new StringBuilder();
  102.         for (int i = 0; i < b.length; i++)
  103.         {
  104.             sb.append(String.format("%02X", b[i] & 0xFF));
  105.         }
  106.         return sb.toString();
  107.     }
  108.  
  109.     public static void main(String[] args)
  110.     {
  111.         String[] testStrings = { "", "Sanfoundry", "Message Digest",
  112.                 "abcdefghijklmnopqrstuvwxyz" };
  113.         for (String s : testStrings)
  114.             System.out.println("0x" + toHexString(computeMD5(s.getBytes()))
  115.                     + " <== \"" + s + "\"");
  116.         return;
  117.     }
  118. }

Output:

$ javac MD5.java
$ java MD5
 
0xD41D8CD98F00B204E9800998ECF8427E <== ""
0x123EC1617559F98A4C86AF629FEF21E6 <== "Sanfoundry"
0xBBD9D8CC4AD8AD2599DBF623E7E5282E <== "Message Digest"
0xC3FCD3D76192E4007DFB496CCA67E13B <== "abcdefghijklmnopqrstuvwxyz"

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.