https://everleaf.blogspot.com/search/label/PHP

10/24/2023

Java MD5 Hash encoding

Dibawah ini adalah fungsi statis untuk memanggil md5 hash dengan fungsi core java MessageDigest(), data type tersebut menghasilkan return md5 hash dengan data type String plaintext
	
    private static String md5hash(String e) {
        String result = "null";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hash = md.digest(e.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder(2 * hash.length);
            for (byte b : hash) {
                sb.append(String.format("%02x", b & 0xff));
            }
            result = sb.toString();
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
Simpan script java dibawah ini dengan nama HelloWorld.java
    
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

class HelloWorld {
    
    private static String md5hash(String e) {
        String result = "null";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hash = md.digest(e.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder(2 * hash.length);
            for (byte b : hash) {
                sb.append(String.format("%02x", b & 0xff));
            }
            result = sb.toString();
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }


    public static void main(String[] args) {
        String text = "myplaintext";
        
        String md5_hash = md5hash(text);
        System.out.println("md5 hash result : "+md5_hash+"\n");
    }
}
Maka dari diatas akan menghasilkan md5 hash "3c5d21f4241582748caf9d6f6507167e" dari input string "myplaintext". Jalankan script java : java HelloWorld.java, hasilnya:
    
md5 hash result : 3c5d21f4241582748caf9d6f6507167e

Java base64 text encoding

Dibawah ini adalah fungsi statis untuk memanggil base64 encode dengan data type return String plaintext
	
    private static String encodeData(String e) {
        try {
            return new String(Base64.getEncoder().encode(e.getBytes()));
        } catch (Exception ex) {
            return e;
        }
    }
Dibawah ini adalah fungsi statis untuk memanggil base64 decode dengan data type return String plaintext
   
    private static String decodeData(String e) {
        try {
            return new String(Base64.getDecoder().decode(e.getBytes()));
        } catch (Exception ex) {
            return e;
        }
    }}
    }
Simpan script java dibawah ini dengan nama HelloWorld.java
    
import java.util.Base64;

class HelloWorld {
    
    private static String encodeData(String e) {
        try {
            return new String(Base64.getEncoder().encode(e.getBytes()));
        } catch (Exception ex) {
            return e;
        }
    }
    
    private static String decodeData(String e) {
        try {
            return new String(Base64.getDecoder().decode(e.getBytes()));
        } catch (Exception ex) {
            return e;
        }
    }


    public static void main(String[] args) {
        String mytext = "hello world java";
        
        String encoded_string = encodeData(mytext);
        System.out.println("base64 encoded text : "+encoded_string+"\n");
        
        String decoded_string = decodeData(encoded_string);
        System.out.println("base64 decoded text : "+decoded_string+"\n");
    }
}
Jalankan script java : java HelloWorld.java, hasilnya:
    
base64 encoded text : aGVsbG8gd29ybGQgamF2YQ==

base64 decoded text : hello world java

Java switch select statement

Simpan script java dengan nama HelloWorld.java
	
class HelloWorld {
    public static void main(String[] args) {
        String animal = "dog";
        switch(animal){
            case "dog":
                System.out.println("found animal dog");
                break;
            case "cat":
                System.out.println("found animal cat");
                break;
            case "bird":
                System.out.println("found animal bird");
                break;
            default:
                break;
        }
    }
}
Jalankan script java : java HelloWorld.java, hasilnya:
    
found animal dog