如何检查 Java 游戏中资源的 MD5 哈希值是否正确?

How do I check if MD5 hashes of resources are correct in Java game?

我正在开发一个Java游戏,我想验证资源是否与我包含的资源不同。我可以通过检查 MD5 哈希来做到这一点吗?如果可以的话,我该怎么做? 感谢任何帮助,谢谢。

找到获取 MD5 的代码here:.我只是修改为接受资源路径并将该资源的字节读入字节数组,然后将其传递给 MD5 算法

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

    public static String getMD5(String pathToResource) {
            Path path = Paths.get(pathToResource);
            byte[] data = Files.readAllBytes(path);
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] messageDigest = md.digest(data);
                BigInteger number = new BigInteger(1, messageDigest);
                String hashtext = number.toString(16);
                // Now we need to zero pad it if you actually want the full 32 chars.
                while (hashtext.length() < 32) {
                    hashtext = "0" + hashtext;
                }
                return hashtext;
            }
            catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }