Java比较来自另一个生成器的MD5与另一个MD5:值不相同。

huangapple go评论84阅读模式
英文:

Java compare md5 with another md5 from another generator : not same value

问题

这是您要求的代码部分的翻译:

我可以使用以下函数生成MD5哈希值

private void generateMd5() throws NoSuchAlgorithmException, IOException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(pathFile.getBytes());
        nomGestionnaire.setText(String.valueOf(String.format("%032X", new BigInteger(1, hash))));
}
我的问题是当我将生成的MD5哈希值与另一个MD5生成器进行比较时我得到的值不相同
这正常吗好像我的生成器没有生成**真正的MD5**

使用此文件进行测试aaa.txt内容aaa

我的生成器:`A4FA953DB4BC7772E5AF67BD706B9110`

其他生成器:`47bce5c74f589f4867dbd57e9ca9f808`

编辑

```java
FileChooser fileChooser = new FileChooser();
File selectedFile = new File(String.valueOf(fileChooser.showOpenDialog(primaryStage)));
nameFile = selectedFile.getName();
pathFile = selectedFile.getPath();

请注意,MD5哈希值的不同可能是由于文本编码或其他因素引起的。

英文:

I can generate md5 with this function :

private void generateMd5() throws NoSuchAlgorithmException, IOException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(pathFile.getBytes());
        nomGestionnaire.setText(String.valueOf(String.format("%032X", new BigInteger(1, hash))));

    }

My problem is when I compare my md5 generate with another md5 generator I don't have the same value.
Is it normal ? It's like my generator doesn't generate a real md5 ?

Test with this file : aaa.txt (content : aaa)

My generator : A4FA953DB4BC7772E5AF67BD706B9110

other generator : 47bce5c74f589f4867dbd57e9ca9f808

EDIT :

FileChooser fileChooser = new FileChooser();
File selectedFile = new 
File(String.valueOf(fileChooser.showOpenDialog(primaryStage)));
nameFile = selectedFile.getName();
pathFile = selectedFile.getPath();

答案1

得分: 2

以下是您要翻译的内容:

我猜输入中有一些错误。不幸的是,您提供的文件不完整。因此,我首先编写了一个执行基本MD5的Java方法。然后,我进行了一些取证工作,猜测并修复了代码。两者都生成了正确的MD5值:47BCE5C74F589F4867DBD57E9CA9F808

public static String getMD5(String filename)
    throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(Files.readAllBytes(Paths.get(filename)));
    byte[] digest = md.digest();
    String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
    return myChecksum;
}

public static String generateMd5(String pathToFile) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hash = md.digest(Files.readAllBytes(Paths.get(pathToFile)));
    return String.valueOf(String.format("%032X", new BigInteger(1, hash)));
}
英文:

I guess there's some error on the input. Unfortunately the and file you provided is not complete. So I did I first wrote a Java method that does a basic md5. Then I did some forensics to guess and fix the code. Both deliver correct MD5: 47BCE5C74F589F4867DBD57E9CA9F808

    public static String getMD5(String filename)
        throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(Files.readAllBytes(Paths.get(filename)));
    byte[] digest = md.digest();
    String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
    return myChecksum;
}



public static String generateMd5(String pathToFile) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hash = md.digest(Files.readAllBytes(Paths.get(pathToFile)));
    return String.valueOf(String.format("%032X", new BigInteger(1, hash)));
}

答案2

得分: 0

我以前遇到过这个问题,这可能是一个编码问题(来自文件路径)。
我在我的项目中这样放置它,从那以后它就一直工作得很好。
我会为您提供一个示例,所以您可能会从中得到一个思路:

public String hash(String stringToHash){
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    // 在这里,您使用所需的算法初始化了消息摘要单例
    messageDigest.update(stringToHash.getBytes());
    // 现在,您使用字符串的字节更新了它
    byte[] digest = messageDigest.digest();
    String result = DatatypeConverter.printHexBinary(digest);
    // 最后,您将结果(十六进制)转换为字符串(您的散列字符串)
    // 您可能希望使用toLowerCase()以使其不区分大小写
    return result;
}

注意:您可以使用多种方法来使用MD5进行哈希,我更喜欢使用Apache Commons作为一种简单的方法,您可能想要查看这个 使用Apache Commons进行MD5哈希
您还可以使用Google Guava来做到这一点:使用Google Guava进行MD5哈希

英文:

I faced this problem before, it is probably an encoding problem (from that file path).
I put it like this in my project, and it's working just fine since then.
I'll provide you with a sample, so probably you get an idea from this:

public String hash(String stringToHash){
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    // here you init the message digit singleton with the wanted algorithm
    messageDigest.update(stringToHash.getBytes());
    //now you updated it, with the bytes of your string
    byte[] digest = messageDigest.digest();
    String result = DatatypeConverter.printHexBinary(digest);
    // finally you converted the result (hex) to String (you hashed string)
    // you may want to use toLowerCase() so you make it case insensitive
    return result;
}

NOTE: you have many way to hash using md5, I really prefer using the Apache Commons as an easy way, you may want to check this out MD5 Hashing using Apache Commons.
You also can do that using Google Guava : MD5 Hashing using Google Guava.

huangapple
  • 本文由 发表于 2020年7月28日 22:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63136803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定