View Single Post
Old 06-16-2004, 11:17 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
ok, well here is answers i found in another forum regarding making the hash look exactly like php:
Quote:
MD5 is a 128-bit hash.
128 / 8 = 16 bytes
16 bytes, converted to hexadecimal, will give 32 characters.
So convert your MD5 output to hexadecimal
Code:
import java.util.*;
import java.io.*;
import java.security.*;
public class Test  {    
    public static String hex(byte[] array) {        
        StringBuffer sb = new StringBuffer();        
        for (int i = 0; i < array.length; ++i) {            
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1,3));        
        }        
        return sb.toString();    
    }    
    public static String md5 (String message) {         
        try {             
            MessageDigest md = MessageDigest.getInstance("MD5");             
            return hex (md.digest(message.getBytes("CP1252")));         
        } catch (NoSuchAlgorithmException e) {         
        } catch (UnsupportedEncodingException e) {         
        }         
        return null;    
    }    
    public static void main(String[] args) {        
        System.out.println (md5 ("Hello, world!"));    
    }
}
__________________
Mike
sde is offline   Reply With Quote