Java MD5

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.security.MessageDigest;
    import java.math.BigInteger;
    
    public class Solution {
    
        public static void main(String[] args) throws Exception {
            Scanner scan = new Scanner(System.in);
            
            String str = scan.nextLine();
            
            scan.close();
            
            try {
     
                // Static getInstance method is called with hashing MD5
                MessageDigest md = MessageDigest.getInstance("MD5");
     
                // digest() method is called to calculate message digest
                // of an input digest() return array of byte
                byte[] messageDigest = md.digest(str.getBytes());
     
                // Convert byte array into signum representation
                BigInteger no = new BigInteger(1, messageDigest);
     
                // Convert message digest into hex value
                String hashtext = no.toString(16);
                while (hashtext.length() < 32) {
                    hashtext = "0" + hashtext;
                }
                
                System.out.println(hashtext);
            }
     
            // For specifying wrong message digest algorithms
            catch (Exception e) {
                throw new Exception(e);
            }
            
        }
    }