Java MD5

Sort by

recency

|

109 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.; import java.security.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        String str;
        boolean result = false;
    
        while(s.hasNext()){
            str = s.nextLine().trim();
            result = Pattern.matches("^[a-zA-Z0-9]+$",str);
            if(result){
                try{
                    MessageDigest md = MessageDigest.getInstance("MD5");
    
                    md.update(str.getBytes());
    
                    byte[] digest = md.digest();
    
                    for(byte b : digest){
                        System.out.printf("%02x",b);
                    }
                    System.out.println();
    
                } catch (Exception e){
    
                }                
            } else {
                System.out.println("Invalid Input!");
            }
        }
        s.close();
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    import java.security.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner s = new Scanner(System.in);
            String str;
            boolean result = false;
            
            while(s.hasNext()){
                str = s.nextLine().trim();
                result = Pattern.matches("^[a-zA-Z0-9]+$",str);
                if(result){
                    try{
                        MessageDigest md = MessageDigest.getInstance("MD5");
                        
                        md.update(str.getBytes());
                        
                        byte[] digest = md.digest();
                        
                        for(byte b : digest){
                            System.out.printf("%02x",b);
                        }
                        System.out.println();
                        
                    } catch (Exception e){
                        
                    }                
                } else {
                    System.out.println("Invalid Input!");
                }
            }
            s.close();
        }
    }
    
  • + 0 comments

    works well for java7, java8, java15 import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Scanner;

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); sc.close();

        try {
            // Create MD5 MessageDigest instance
            MessageDigest md = MessageDigest.getInstance("MD5");
    
            // Calculate MD5 hash in bytes
            byte[] messageDigest = md.digest(s.getBytes());
    
            // Convert byte array into signum representation
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                // Convert each byte to hexadecimal
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
    
            // Print the result
            System.out.println(hexString.toString());
        } catch (NoSuchAlgorithmException e) {
            // Handle exception if MD5 algorithm is not available
            throw new RuntimeException(e);
        }
    }
    

    }

  • + 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);
        }
    
    }
    

    }

  • + 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);
            }
            
        }
    }