JoshuaJeong / nhin-d

Automatically exported from code.google.com/p/nhin-d
0 stars 0 forks source link

1.1-SNAPSHOT version of xd-common generated incorrect hash value #180

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
What is the expected output? What do you see instead?

The following test case can see the hash value difference.
import java.io.File;

import junit.framework.TestCase;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;

public class HashTest extends TestCase {

    public void testHashShortText () throws Exception {
        System.out.println("Short Text.");
        String data = "Test";
        String expect = DigestUtils.shaHex(data);
        String actual = DirectDocument2.getSha1Hash(data);

        System.out.println("Expect: " + expect);
        System.out.println("Actual: " + actual);
        assertEquals(expect, actual);
    }

    public void testHashLargeText () throws Exception {
        System.out.println("Large file.");
        byte[] byteArray = FileUtils.readFileToByteArray(new File("src/test/resources/samplexdm.zip"));
        String expect = DigestUtils.shaHex(byteArray);
        String actual = DirectDocument2.getSha1Hash(byteArray);

        System.out.println("Expect: " + expect);
        System.out.println("Actual: " + actual);
        assertEquals(expect, actual);
    }

}

What version of the product are you using? On what operating system?
1.1-SNAPSHOT version of xd-common.

Please provide any additional information below.
Use org.apache.commons.codec.digest.DigestUtils instead.

Original issue reported on code.google.com by teru.mor...@gmail.com on 1 Mar 2012 at 9:57

GoogleCodeExporter commented 9 years ago
Also it's better to use InputStream to generate hash value.

this.hash = getSha1Hash(FileUtils.readFileToString(file));

-->

InputStream is = null;
try {
    is = new FileInputStream(file);
    this.hash = DigestUtils.shaHex(is);
} catch (IOException e) {
    throw e;
} finally {
    IOUtils.closeQuietly(is);
}

Original comment by teru.mor...@gmail.com on 1 Mar 2012 at 10:00

GoogleCodeExporter commented 9 years ago
xd-common-1.0.1 also has the same issue.

Original comment by teru.mor...@gmail.com on 1 Mar 2012 at 10:17

GoogleCodeExporter commented 9 years ago
Adding a patch, see xd-common-patch-180.txt

Summary of changes:

- xd-common/pom.xml
-- Upgraded commons-codec to 1.7 (consider 1.9, but I know 1.7 works)

- xd-common/src/main/java/org/nhindirect/xd/common/DirectDocument2.java
-- Both getSha1Hash methods now just call commons codec's DigestUtils

- xd-common/src/main/java/org/nhindirect/xd/common/XdmPackage.java
-- Don't convert to a String here, just use the bytes

Original comment by marc...@mirthcorp.com on 6 Oct 2014 at 6:26

Attachments: