在Java中生成相同的MD5哈希

在Java中生成相同的MD5哈希,java,md5,Java,Md5,为了创建一个安全的登录名,我决定使用下面的代码创建一个MD5散列,这是从 但是,在创建用户时,这不会生成与在登录页面上创建的相同的散列。为什么会这样,因为我认为哈希对每个字符串都是唯一的 MessageDigest messageDigest = null; try{ messageDigest = MessageDigest.getInstance("MD5"); }catch(NoSuchAlgorithmException e){

为了创建一个安全的登录名,我决定使用下面的代码创建一个MD5散列,这是从

但是,在创建用户时,这不会生成与在登录页面上创建的相同的散列。为什么会这样,因为我认为哈希对每个字符串都是唯一的

    MessageDigest messageDigest = null;

    try{
        messageDigest = MessageDigest.getInstance("MD5");
    }catch(NoSuchAlgorithmException e){
        System.out.println("Error: " + e);
    }

    messageDigest.reset();
    messageDigest.update(inPassword.getBytes());
    byte[] digest = messageDigest.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String encodedPass = bigInt.toString(16);

    while (encodedPass.length() < 32) {
        encodedPass = "0" + encodedPass;
    }


    inPassword = encodedPass;
MessageDigest=null;
试一试{
messageDigest=messageDigest.getInstance(“MD5”);
}捕获(无算法异常){
System.out.println(“错误:+e”);
}
messageDigest.reset();
update(inPassword.getBytes());
字节[]摘要=messageDigest.digest();
biginger bigInt=新的biginger(1,摘要);
字符串encodedPass=bigInt.toString(16);
while(encodedPass.length()<32){
encodedPass=“0”+encodedPass;
}
inPassword=编码过程;

以下是您可能需要的完整代码

import java.io.FileInputStream;
import java.security.MessageDigest;

public class MD5CheckSumExample 
{
    public static void main(String[] args)throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream("c:\\loging.log");

        byte[] dataBytes = new byte[1024];

        int nread = 0; 
        while ((nread = fis.read(dataBytes)) != -1) {
          md.update(dataBytes, 0, nread);
        };
        byte[] mdbytes = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
          sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }

        System.out.println("Digest(in hex format):: " + sb.toString());

        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<mdbytes.length;i++) {
            String hex=Integer.toHexString(0xff & mdbytes[i]);
            if(hex.length()==1) hexString.append('0');
            hexString.append(hex);
        }
        System.out.println("Digest(in hex format):: " + hexString.toString());
    }
}
import java.io.FileInputStream;
导入java.security.MessageDigest;
公共类MD5checksume示例
{
公共静态void main(字符串[]args)引发异常
{
MessageDigest md=MessageDigest.getInstance(“MD5”);
FileInputStream fis=新的FileInputStream(“c:\\loging.log”);
字节[]数据字节=新字节[1024];
int nread=0;
而((nread=fis.read(数据字节))!=-1){
md.update(数据字节,0,nread);
};
byte[]mdbytes=md.digest();
//将字节转换为十六进制格式方法1
StringBuffer sb=新的StringBuffer();
对于(int i=0;i
messageDigest.update(inPassword.getBytes());
这是使用平台默认编码将密码转换为字节。这在运行密码的每个系统上都可能有所不同。我强烈建议您指定一种编码-理想情况下,该编码将处理所有Unicode字符(例如UTF-8)


您可能还想考虑盐渍,使用比MD5更好的东西,我不确定您是否可以将
byte[]
转换为hex-这可能还可以,但我会找到一个库,可以在不使用
biginger
的情况下完成整个过程。试试这个,它适合我:

messageDigest.update(myString.getBytes(), 0, myString.length());
代码的其余部分似乎正确。希望有帮助!:)