Search in sources :

Example 1 with Md5Generator

use of org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory.Md5Generator in project galley by Commonjava.

the class Md5GeneratorFactoryTest method verifyWithMessageDigest.

@Test
public void verifyWithMessageDigest() throws Exception {
    final byte[] data = "this is a test".getBytes();
    final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), "my-path.txt"));
    final Md5GeneratorFactory factory = new Md5GeneratorFactory();
    final Md5Generator generator = factory.newGenerator(txfr, true);
    generator.update(data);
    generator.write();
    final MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(data);
    final byte[] digest = md.digest();
    final String digestHex = Hex.encodeHexString(digest);
    final Transfer md5Txfr = txfr.getSiblingMeta(".md5");
    InputStream in = null;
    String resultHex = null;
    try {
        in = md5Txfr.openInputStream();
        resultHex = IOUtils.toString(in);
    } finally {
        IOUtils.closeQuietly(in);
    }
    assertThat(resultHex, equalTo(digestHex));
}
Also used : Md5Generator(org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory.Md5Generator) InputStream(java.io.InputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)1 MessageDigest (java.security.MessageDigest)1 Md5Generator (org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory.Md5Generator)1 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)1 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)1 Transfer (org.commonjava.maven.galley.model.Transfer)1 Test (org.junit.Test)1