Java & php: Java analog of php md5 function

Trabla: Java & php: Java analog of php md5 function




Java & php: Java analog of php md5 function



Solving:

1. PHP code:

<?php

$password = "trololo";

echo md5($password);

?>

Result:  37f62f1363b04df4370753037853fe88

Java & php: Java analog of php md5 function - screenshot 1



2. Java code:


import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class Main {
    
  // This implementationfound on stackoverflow
  // url: http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash
  // RESULT SAME AS PHP md5 !!!

  public static String MD5(String md5) {
   try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
       }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}     
    
    
  public static void main(String[] args) {
      
      String password = new String("trololo");
      
      System.out.println( MD5(password) );
      
    
  }
}

Result: 37f62f1363b04df4370753037853fe88

Java & php: Java analog of php md5 function - screenshot 2

No comments:

Post a Comment