Example usage for org.apache.commons.codec.digest DigestUtils sha256Hex

List of usage examples for org.apache.commons.codec.digest DigestUtils sha256Hex

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils sha256Hex.

Prototype

public static String sha256Hex(String data) 

Source Link

Usage

From source file:org.cloudfoundry.dependency.resource.InAction.java

private String getSha256(Path artifact) throws IOException {
    try (InputStream in = Files.newInputStream(artifact, StandardOpenOption.READ)) {
        return DigestUtils.sha256Hex(in);
    }//from   w w w.j a  v  a  2s  .  co  m
}

From source file:org.commonjava.aprox.folo.ftest.report.StoreFileAndVerifyInTrackingReportTest.java

@Test
public void storeFileAndVerifyInReport() throws Exception {
    final String trackingId = newName();

    final InputStream stream = new ByteArrayInputStream(("This is a test: " + System.nanoTime()).getBytes());

    final String path = "/path/to/foo.class";
    client.module(AproxFoloContentClientModule.class).store(trackingId, hosted, STORE, path, stream);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(AproxFoloContentClientModule.class).get(trackingId, hosted, STORE,
            path);//w w w. j a  v a 2  s  .c o  m

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    final TrackedContentDTO report = client.module(AproxFoloAdminClientModule.class)
            .getTrackingReport(trackingId, hosted, STORE);
    assertThat(report, notNullValue());

    final Set<TrackedContentEntryDTO> uploads = report.getUploads();

    assertThat(uploads, notNullValue());
    assertThat(uploads.size(), equalTo(1));

    final TrackedContentEntryDTO entry = uploads.iterator().next();

    System.out.println(entry);

    assertThat(entry, notNullValue());
    assertThat(entry.getStoreKey(), equalTo(new StoreKey(hosted, STORE)));
    assertThat(entry.getPath(), equalTo(path));
    assertThat(entry.getLocalUrl(),
            equalTo(UrlUtils.buildUrl(client.getBaseUrl(), hosted.singularEndpointName(), STORE, path)));
    assertThat(entry.getOriginUrl(), nullValue());
    assertThat(entry.getMd5(), equalTo(md5));
    assertThat(entry.getSha256(), equalTo(sha256));
}

From source file:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static String generateErrorId() {
    return DigestUtils.sha256Hex(Thread.currentThread().getName());

    //+ "@" + new SimpleDateFormat( "yyyy-MM-ddThhmmss.nnnZ" ).format( new Date() );
}

From source file:org.commonjava.indy.content.IndyPathGenerator.java

@Override
public String getFilePath(final ConcreteResource resource) {
    final KeyedLocation kl = (KeyedLocation) resource.getLocation();
    final StoreKey key = kl.getKey();

    final String name = key.getPackageType() + "/" + key.getType().name() + "-" + key.getName();

    String path = resource.getPath();
    if (hashed == kl.getAttribute(LocationUtils.PATH_STYLE, PathStyle.class)) {
        File f = new File(path);
        String dir = f.getParent();
        if (dir == null) {
            dir = "";
        }//  w  w  w. jav  a  2  s.com

        if (dir.length() > 1 && dir.startsWith("/")) {
            dir = dir.substring(1, dir.length());
        }

        String digest = DigestUtils.sha256Hex(dir);

        Logger logger = LoggerFactory.getLogger(getClass());
        logger.trace("Using SHA-256 digest: '{}' for dir: '{}' of path: '{}'", digest, dir, path);

        // Format examples:
        // - aa/bb/aabbccddeeff001122/simple-1.0.pom
        // - aa/bb/aabbccddeeff001122/gulp-size
        // - 00/11/001122334455667788/gulp-size-1.3.0.tgz
        path = String.format("%s/%s/%s/%s", digest.substring(0, 2), digest.substring(2, 4), digest,
                f.getName());
    }
    // else it's plain and we leave it alone.

    return PathUtils.join(name, path);
}

From source file:org.commonjava.indy.folo.dto.TrackedContentEntryDTOTest.java

@Test
public void jsonRoundTrip_Checksums() throws IOException {
    TrackedContentEntryDTO in = new TrackedContentEntryDTO(new StoreKey(StoreType.remote, "foo"),
            AccessChannel.MAVEN_REPO, "/path/to/my.pom");

    String content = "This is a test string";
    in.setMd5(DigestUtils.md5Hex(content));
    in.setSha256(DigestUtils.sha256Hex(content));
    in.setSha1(DigestUtils.shaHex(content));

    assertRoundTrip(in, (out) -> {/*from  ww w.  j a  va 2s. c  om*/
        assertThat(out.getMd5(), equalTo(in.getMd5()));
        assertThat(out.getSha256(), equalTo(in.getSha256()));
        assertThat(out.getSha1(), equalTo(in.getSha1()));
    });
}

From source file:org.commonjava.indy.folo.ftest.report.RecalculateMissingChecksumForTrackingRecordTest.java

@BMRules(rules = {
        @BMRule(name = "setup_metadata_retrieval_countdown", targetClass = "^org.commonjava.indy.content.ContentDigester", isInterface = true, targetMethod = "<init>", targetLocation = "ENTRY", action = "System.out.println(\"SETUP COUNTDOWN\"); createCountDown(\"COUNTDOWN\", 1);"),
        @BMRule(name = "prevent_first_metadata_retrieval", targetClass = "^org.commonjava.indy.content.ContentDigester", isInterface = true, targetMethod = "getContentMetadata", targetLocation = "ENTRY", binding = "path:String = $1.getPath();", condition = "path.endsWith(\"path/to/foo.class\") && !countDown(\"COUNTDOWN\")", action = "System.out.println(\"RETURN NULL\"); return null;") })
@Test/* w w w  .  j  ava2  s.  c o m*/
public void run() throws Exception {
    final String trackingId = newName();
    final String repoId = "repo";
    final String path = "/path/to/foo.class";

    final InputStream stream = new ByteArrayInputStream(
            "This is a test with the same content each time.".getBytes());

    server.expect(server.formatUrl(repoId, path), 200, stream);

    RemoteRepository rr = new RemoteRepository(repoId, server.formatUrl(repoId));
    rr = client.stores().create(rr, "adding test remote", RemoteRepository.class);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(IndyFoloContentClientModule.class).get(trackingId, remote, repoId,
            path);

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    waitForEventPropagation();

    assertThat(client.module(IndyFoloAdminClientModule.class).sealTrackingRecord(trackingId), equalTo(true));

    final TrackedContentDTO report = client.module(IndyFoloAdminClientModule.class)
            .getTrackingReport(trackingId);
    assertThat(report, notNullValue());

    final Set<TrackedContentEntryDTO> downloads = report.getDownloads();

    assertThat(downloads, notNullValue());
    assertThat(downloads.size(), equalTo(1));

    final TrackedContentEntryDTO entry = downloads.iterator().next();

    System.out.println(entry);

    assertThat(entry, notNullValue());
    assertThat(entry.getStoreKey(), equalTo(new StoreKey(remote, repoId)));
    assertThat(entry.getPath(), equalTo(path));
    assertThat(entry.getLocalUrl(), equalTo(client.content().contentUrl(remote, repoId, path)));
    assertThat(entry.getOriginUrl(), equalTo(server.formatUrl(repoId, path)));
    assertThat(entry.getMd5(), equalTo(md5));
    assertThat(entry.getSha256(), equalTo(sha256));
}

From source file:org.commonjava.indy.folo.ftest.report.RetrieveFileAndVerifyInTrackingReportTest.java

protected String[] retrieveAndSeal(String repoId, String path, String content) throws Exception {
    String trackingId = newName();

    final InputStream stream = new ByteArrayInputStream(content.getBytes());

    server.expect(server.formatUrl(repoId, path), 200, stream);

    if (!client.stores().exists(new StoreKey(MAVEN_PKG_KEY, remote, repoId))) {
        RemoteRepository rr = new RemoteRepository(MAVEN_PKG_KEY, repoId, server.formatUrl(repoId));
        rr = client.stores().create(rr, "adding test remote", RemoteRepository.class);
    }//  w ww.j  av  a2  s  .c o m

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(IndyFoloContentClientModule.class).get(trackingId, remote, repoId,
            path);

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    waitForEventPropagation();

    assertThat(client.module(IndyFoloAdminClientModule.class).sealTrackingRecord(trackingId), equalTo(true));

    return new String[] { trackingId, md5, sha256 };
}

From source file:org.commonjava.indy.folo.ftest.report.RetrieveFileFromGroupAndVerifyInTrackingReportTest.java

@Test
public void run() throws Exception {
    final String trackingId = newName();
    final String repoId = "repo";
    final String groupId = "group";
    final String path = "/path/to/foo.class";

    final InputStream stream = new ByteArrayInputStream(("This is a test: " + System.nanoTime()).getBytes());

    server.expect(server.formatUrl(repoId, path), 200, stream);

    RemoteRepository rr = new RemoteRepository(repoId, server.formatUrl(repoId));
    rr = client.stores().create(rr, "adding test remote", RemoteRepository.class);

    Group g = new Group(groupId, rr.getKey());
    g = client.stores().create(g, "Adding test group", Group.class);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(IndyFoloContentClientModule.class).get(trackingId, group, groupId,
            path);/*ww w  .ja va 2 s.c om*/

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    assertThat(client.module(IndyFoloAdminClientModule.class).sealTrackingRecord(trackingId), equalTo(true));

    final TrackedContentDTO report = client.module(IndyFoloAdminClientModule.class)
            .getTrackingReport(trackingId);
    assertThat(report, notNullValue());

    final Set<TrackedContentEntryDTO> downloads = report.getDownloads();

    assertThat(downloads, notNullValue());
    assertThat(downloads.size(), equalTo(1));

    final TrackedContentEntryDTO entry = downloads.iterator().next();

    System.out.println(entry);

    assertThat(entry, notNullValue());
    assertThat(entry.getStoreKey(), equalTo(new StoreKey(remote, repoId)));
    assertThat(entry.getPath(), equalTo(path));
    assertThat(entry.getLocalUrl(), equalTo(client.content().contentUrl(remote, repoId, path)));
    assertThat(entry.getOriginUrl(), equalTo(server.formatUrl(repoId, path)));
    assertThat(entry.getMd5(), equalTo(md5));
    assertThat(entry.getSha256(), equalTo(sha256));
}

From source file:org.commonjava.indy.folo.ftest.report.RetrieveTrackingReportAfterCacheExpirationTest.java

@Test
public void run() throws Exception {
    final String trackingId = newName();
    final String repoId = "repo";
    final String path = "/path/to/foo.class";

    final InputStream stream = new ByteArrayInputStream(("This is a test: " + System.nanoTime()).getBytes());

    server.expect(server.formatUrl(repoId, path), 200, stream);

    RemoteRepository rr = new RemoteRepository(repoId, server.formatUrl(repoId));
    rr = client.stores().create(rr, "adding test remote", RemoteRepository.class);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(IndyFoloContentClientModule.class).get(trackingId, remote, repoId,
            path);//from www . jav a2  s.  co  m

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    // sleep so the cache evicts the record
    Thread.sleep(2000);

    assertThat(client.module(IndyFoloAdminClientModule.class).sealTrackingRecord(trackingId), equalTo(true));

    final TrackedContentDTO report = client.module(IndyFoloAdminClientModule.class)
            .getTrackingReport(trackingId);
    assertThat(report, notNullValue());

    final Set<TrackedContentEntryDTO> downloads = report.getDownloads();

    assertThat(downloads, notNullValue());
    assertThat(downloads.size(), equalTo(1));

    final TrackedContentEntryDTO entry = downloads.iterator().next();

    System.out.println(entry);

    assertThat(entry, notNullValue());
    assertThat(entry.getStoreKey(), equalTo(new StoreKey(remote, repoId)));
    assertThat(entry.getPath(), equalTo(path));
    assertThat(entry.getLocalUrl(), equalTo(client.content().contentUrl(remote, repoId, path)));
    assertThat(entry.getOriginUrl(), equalTo(server.formatUrl(repoId, path)));
    assertThat(entry.getMd5(), equalTo(md5));
    assertThat(entry.getSha256(), equalTo(sha256));
}

From source file:org.commonjava.indy.folo.ftest.report.UseChecksumFromTransferDecoratorForTrackingRecordTest.java

@BMRules(rules = {
        @BMRule(name = "setup_metadata_countdown", targetClass = "^org.commonjava.indy.content.ContentDigester", isInterface = true, targetMethod = "<init>", targetLocation = "ENTRY", action = "System.out.println(\"SETUP COUNTDOWN\"); createCountDown(\"COUNTDOWN\", 1);"),
        @BMRule(name = "prevent_successive_metadata_additions", targetClass = "^org.commonjava.indy.content.ContentDigester", isInterface = true, targetMethod = "addMetadata", targetLocation = "ENTRY", binding = "path:String = $1.getPath();", condition = "path.endsWith(\"path/to/foo.class\") && countDown(\"COUNTDOWN\")", action = "System.out.println(\"RETURN NULL\"); return null;") })
@Test//from   w ww . j  a v  a  2  s  .c  o  m
public void run() throws Exception {
    final String trackingId = newName();
    final String repoId = "repo";
    final String path = "/path/to/foo.class";

    final InputStream stream = new ByteArrayInputStream(
            "This is a test with the same content each time.".getBytes());

    server.expect(server.formatUrl(repoId, path), 200, stream);

    RemoteRepository rr = new RemoteRepository(repoId, server.formatUrl(repoId));
    rr = client.stores().create(rr, "adding test remote", RemoteRepository.class);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = client.module(IndyFoloContentClientModule.class).get(trackingId, remote, repoId,
            path);

    IOUtils.copy(in, baos);
    in.close();

    final byte[] bytes = baos.toByteArray();

    final String md5 = md5Hex(bytes);
    final String sha256 = sha256Hex(bytes);

    assertThat(md5, equalTo(DigestUtils.md5Hex(bytes)));
    assertThat(sha256, equalTo(DigestUtils.sha256Hex(bytes)));

    waitForEventPropagation();

    assertThat(client.module(IndyFoloAdminClientModule.class).sealTrackingRecord(trackingId), equalTo(true));

    final TrackedContentDTO report = client.module(IndyFoloAdminClientModule.class)
            .getTrackingReport(trackingId);
    assertThat(report, notNullValue());

    final Set<TrackedContentEntryDTO> downloads = report.getDownloads();

    assertThat(downloads, notNullValue());
    assertThat(downloads.size(), equalTo(1));

    final TrackedContentEntryDTO entry = downloads.iterator().next();

    System.out.println(entry);

    assertThat(entry, notNullValue());
    assertThat(entry.getStoreKey(), equalTo(new StoreKey(remote, repoId)));
    assertThat(entry.getPath(), equalTo(path));
    assertThat(entry.getLocalUrl(), equalTo(client.content().contentUrl(remote, repoId, path)));
    assertThat(entry.getOriginUrl(), equalTo(server.formatUrl(repoId, path)));
    assertThat(entry.getMd5(), equalTo(md5));
    assertThat(entry.getSha256(), equalTo(sha256));
}