Java 使用相同算法生成的校验和返回不同的输出

Java 使用相同算法生成的校验和返回不同的输出,java,Java,我有两个程序,使用相同的校验和生成方法(SHA-256)。 区别在于,在第一个程序中,路径是(windowspath/folder/file),在第二个程序中,路径是(./folder/file)。 代码如下: 计划1 String address = fileAddr.getText(); File file = new File(address); try

我有两个程序,使用相同的校验和生成方法(SHA-256)。 区别在于,在第一个程序中,路径是(windowspath/folder/file),在第二个程序中,路径是(./folder/file)。 代码如下:

计划1

String address = fileAddr.getText();
                            File file = new File(address);
                            try
                                {
                                    checksum = getChecksum(file.getAbsolutePath());
                                    data = generateByte(address);

                                }
                            catch (NoSuchAlgorithmException | IOException e1)
                                {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
校验和生成方法

public static String getChecksum(Serializable object) throws IOException, NoSuchAlgorithmException
        {
            ByteArrayOutputStream baos = null;
            ObjectOutputStream oos = null;
            System.out.println(object.toString());
            try
                {
                    baos = new ByteArrayOutputStream();
                    oos = new ObjectOutputStream(baos);
                    oos.writeObject(object);
                    MessageDigest md = MessageDigest.getInstance("SHA-256");
                    byte[] thedigest = md.digest(baos.toByteArray());
                    return DatatypeConverter.printHexBinary(thedigest);
                }
            finally
                {
                    oos.close();
                    baos.close();
                }
        }
计划2

File folder = new File(".\\Plugins");
File[] listOfFiles = folder.listFiles();
listChecksum.add(getChecksum(listOfFiles[i].getPath()));

问题如标题所述,两个校验和不对应。

我认为您不是在读取文件的内容,只是在比较文件名。当然,它们是不同的,所以你会得到不同的校验和值

我建议在生成校验和的函数中读取文件的内容:

public class Main {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        Path x = Paths.get("D:\\work\\some.zip");
        System.out.println(getChecksum(x));
    }

    public static String getChecksum(Path path) throws IOException, NoSuchAlgorithmException {
        byte[] bytes = Files.readAllBytes(path);
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] thedigest = md.digest(bytes);
        return Base64.getEncoder().encodeToString(thedigest);
    }

}

我认为您不是在阅读文件的内容,而是在比较文件名。当然,它们是不同的,所以你会得到不同的校验和值

我建议在生成校验和的函数中读取文件的内容:

public class Main {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        Path x = Paths.get("D:\\work\\some.zip");
        System.out.println(getChecksum(x));
    }

    public static String getChecksum(Path path) throws IOException, NoSuchAlgorithmException {
        byte[] bytes = Files.readAllBytes(path);
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] thedigest = md.digest(bytes);
        return Base64.getEncoder().encodeToString(thedigest);
    }

}

那么我能做些什么来解决这个问题呢?那么我能做些什么来解决这个问题呢?你知道散列生成是如何工作的吗?抱歉,如果我听起来很傲慢,但是散列(也包括用SHA-256生成的散列)在同一个文件中总是一样的。你知道散列生成是如何工作的吗?抱歉,如果我听起来傲慢,但是哈希(也包括用SHA-256生成的哈希)对于同一个文件总是相同的。