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

huangapple 未分类评论57阅读模式
英文:

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

问题

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

  1. 我可以使用以下函数生成MD5哈希值
  2. private void generateMd5() throws NoSuchAlgorithmException, IOException {
  3. MessageDigest md = MessageDigest.getInstance("MD5");
  4. byte[] hash = md.digest(pathFile.getBytes());
  5. nomGestionnaire.setText(String.valueOf(String.format("%032X", new BigInteger(1, hash))));
  6. }
  1. 我的问题是当我将生成的MD5哈希值与另一个MD5生成器进行比较时我得到的值不相同
  2. 这正常吗好像我的生成器没有生成**真正的MD5**
  3. 使用此文件进行测试aaa.txt内容aaa
  4. 我的生成器`A4FA953DB4BC7772E5AF67BD706B9110`
  5. 其他生成器`47bce5c74f589f4867dbd57e9ca9f808`
  6. 编辑
  7. ```java
  8. FileChooser fileChooser = new FileChooser();
  9. File selectedFile = new File(String.valueOf(fileChooser.showOpenDialog(primaryStage)));
  10. nameFile = selectedFile.getName();
  11. pathFile = selectedFile.getPath();

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

英文:

I can generate md5 with this function :

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

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 :

  1. FileChooser fileChooser = new FileChooser();
  2. File selectedFile = new
  3. File(String.valueOf(fileChooser.showOpenDialog(primaryStage)));
  4. nameFile = selectedFile.getName();
  5. pathFile = selectedFile.getPath();

答案1

得分: 2

以下是您要翻译的内容:

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

  1. public static String getMD5(String filename)
  2. throws NoSuchAlgorithmException, IOException {
  3. MessageDigest md = MessageDigest.getInstance("MD5");
  4. md.update(Files.readAllBytes(Paths.get(filename)));
  5. byte[] digest = md.digest();
  6. String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
  7. return myChecksum;
  8. }
  9. public static String generateMd5(String pathToFile) throws NoSuchAlgorithmException, IOException {
  10. MessageDigest md = MessageDigest.getInstance("MD5");
  11. byte[] hash = md.digest(Files.readAllBytes(Paths.get(pathToFile)));
  12. return String.valueOf(String.format("%032X", new BigInteger(1, hash)));
  13. }
英文:

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

  1. public static String getMD5(String filename)
  2. throws NoSuchAlgorithmException, IOException {
  3. MessageDigest md = MessageDigest.getInstance("MD5");
  4. md.update(Files.readAllBytes(Paths.get(filename)));
  5. byte[] digest = md.digest();
  6. String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
  7. return myChecksum;
  8. }
  9. public static String generateMd5(String pathToFile) throws NoSuchAlgorithmException, IOException {
  10. MessageDigest md = MessageDigest.getInstance("MD5");
  11. byte[] hash = md.digest(Files.readAllBytes(Paths.get(pathToFile)));
  12. return String.valueOf(String.format("%032X", new BigInteger(1, hash)));
  13. }

答案2

得分: 0

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

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

注意:您可以使用多种方法来使用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:

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

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://java.coder-hub.com/63136803.html
匿名

发表评论

匿名网友

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

确定