Pages

MD5 in Java

MD5 (Message Digest algorithm 5) is message-digest algorithm, which takes as input a message of arbitrary length and produces as output a 128-bit (32 hexadecimal character)  "message digest" of the input.

MD5 is one of various strategies for distinguishing, securing and checking information. Cryptographic hashing is a crucial part ever, and keeping things covered up.

You can use the MD5 algorithm for securing password and to check the  integrity of your file and data.

You can also use the  MD5 algorithm to check the Duplicate File. MD5 can also be used for checksum.

When storing user details in a repository, you should avoid storing user passwords in clear text, because that makes them vulnerable to hackers. Instead, you should always store encrypted passwords in your repository.

A checksum is a small-sized datum derived from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage. It is usually applied to an installation file after it is received from the download server. By themselves, checksums are often used to verify data integrity but are not relied upon to verify data authenticity.

Now I am going to show you, How to use MD5 in Java

Java has a class  "MessageDigest" inside java.security package, that contains the implementation of MD5

Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.

We will use this class to get MD5 of a text data

package com.esc.validator;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

    public static void main(String[] args) throws NoSuchAlgorithmException {

        String testString = "I am going to kanpur";
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(testString.getBytes());

        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        System.out.println(sb.toString());

    }

}


Output
 
7341e209948bef702bb6034c5d634098 
 

SHA-1 In Java

Online Demo of MD5 

References 

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

No comments:

Post a Comment