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

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

Introduction

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

Prototype

public static MessageDigest getMd5Digest() 

Source Link

Usage

From source file:com.joyent.manta.client.MantaObjectOutputStreamIT.java

public void canUploadMuchLargerFile() throws IOException {
    String path = testPathPrefix + "uploaded-" + UUID.randomUUID() + ".txt";
    MantaObjectOutputStream out = mantaClient.putAsOutputStream(path);
    ByteArrayOutputStream bout = new ByteArrayOutputStream(6553600);

    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    long totalBytes = 0;

    try {//from   ww  w .  j  a v a  2s.c  o m
        for (int i = 0; i < 100; i++) {
            int chunkSize = RandomUtils.nextInt(1, 131072);
            byte[] randomBytes = RandomUtils.nextBytes(chunkSize);
            md5Digest.update(randomBytes);
            totalBytes += randomBytes.length;
            out.write(randomBytes);
            bout.write(randomBytes);

            // periodically flush
            if (i % 25 == 0) {
                out.flush();
            }
        }
    } finally {
        out.close();
        bout.close();
    }

    try (InputStream in = mantaClient.getAsInputStream(path)) {
        byte[] expected = bout.toByteArray();
        byte[] actual = IOUtils.readFully(in, (int) totalBytes);

        AssertJUnit.assertArrayEquals("Bytes written via OutputStream don't match read bytes", expected,
                actual);
    }
}

From source file:com.thoughtworks.go.plugin.infra.commons.PluginsZip.java

public void create() {
    checkFilesAccessibility(bundledPlugins, externalPlugins);
    reset();/*from   w w  w .j a v a 2s  .  co  m*/

    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    try (ZipOutputStream zos = new ZipOutputStream(
            new DigestOutputStream(new BufferedOutputStream(new FileOutputStream(destZipFile)), md5Digest))) {
        for (GoPluginDescriptor agentPlugins : agentPlugins()) {
            String zipEntryPrefix = "external/";

            if (agentPlugins.isBundledPlugin()) {
                zipEntryPrefix = "bundled/";
            }

            zos.putNextEntry(
                    new ZipEntry(zipEntryPrefix + new File(agentPlugins.pluginFileLocation()).getName()));
            Files.copy(new File(agentPlugins.pluginFileLocation()).toPath(), zos);
            zos.closeEntry();
        }
    } catch (Exception e) {
        LOG.error("Could not create zip of plugins for agent to download.", e);
    }

    md5DigestOfPlugins = Hex.encodeHexString(md5Digest.digest());
}

From source file:com.joyent.manta.client.MantaObjectOutputStreamIT.java

public void canUploadMuchLargerFileWithPeriodicWaits() throws IOException, InterruptedException {
    String path = testPathPrefix + "uploaded-" + UUID.randomUUID() + ".txt";
    MantaObjectOutputStream out = mantaClient.putAsOutputStream(path);
    ByteArrayOutputStream bout = new ByteArrayOutputStream(6553600);

    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    long totalBytes = 0;

    try {/*from  w  w w.j a  v a2s. c om*/
        for (int i = 0; i < 100; i++) {
            int chunkSize = RandomUtils.nextInt(1, 131072);
            byte[] randomBytes = RandomUtils.nextBytes(chunkSize);
            md5Digest.update(randomBytes);
            totalBytes += randomBytes.length;
            out.write(randomBytes);
            bout.write(randomBytes);

            // periodically wait
            if (i % 3 == 0) {
                Thread.sleep(RandomUtils.nextLong(1L, 1000L));
            }
        }
    } finally {
        out.close();
        bout.close();
    }

    try (InputStream in = mantaClient.getAsInputStream(path)) {
        byte[] expected = bout.toByteArray();
        byte[] actual = IOUtils.readFully(in, (int) totalBytes);

        AssertJUnit.assertArrayEquals("Bytes written via OutputStream don't match read bytes", expected,
                actual);
    }
}

From source file:com.microsoftopentechnologies.windowsazurestorage.service.UploadToFileService.java

private String uploadCloudFile(final CloudFile cloudFile, final FilePath localPath) throws WAStorageException {
    try {//ww  w. j  a v a2 s . co  m
        ensureDirExist(cloudFile.getParent());
        cloudFile.setMetadata(updateMetadata(cloudFile.getMetadata()));

        final MessageDigest md = DigestUtils.getMd5Digest();
        long startTime = System.currentTimeMillis();
        try (InputStream inputStream = localPath.read();
                DigestInputStream digestInputStream = new DigestInputStream(inputStream, md)) {
            cloudFile.upload(digestInputStream, localPath.length(), null, new FileRequestOptions(),
                    Utils.updateUserAgent());
        }
        long endTime = System.currentTimeMillis();

        println("Uploaded blob with uri " + cloudFile.getUri() + " in " + getTime(endTime - startTime));
        return DatatypeConverter.printHexBinary(md.digest());
    } catch (IOException | InterruptedException | URISyntaxException | StorageException e) {
        throw new WAStorageException("fail to upload file to azure file storage", e);
    }

}

From source file:com.temenos.interaction.loader.classloader.CachingParentLastURLClassloaderFactory.java

protected Object calculateCurrentState(FileEvent<File> param) {
    Collection<File> files = FileUtils.listFiles(param.getResource(), new String[] { "jar" }, true);

    MessageDigest md = DigestUtils.getMd5Digest();
    for (File f : files) {
        md.update(Longs.toByteArray(f.lastModified()));
    }// www  .j  a  v a2s . co m

    Object state = Hex.encodeHexString(md.digest());
    LOGGER.trace(
            "Calculated representation /hash/ of state of collection of URLs for classloader creation to: {}",
            state);
    return state;
}

From source file:com.microsoftopentechnologies.windowsazurestorage.service.UploadToBlobService.java

/**
 * @param blob// w  w w . j a v a2  s.  co  m
 * @param src
 * @throws StorageException
 * @throws IOException
 * @throws InterruptedException
 * @returns Md5 hash of the uploaded file in hexadecimal encoding
 */
private String uploadBlob(CloudBlockBlob blob, FilePath src)
        throws StorageException, IOException, InterruptedException {
    final MessageDigest md = DigestUtils.getMd5Digest();
    long startTime = System.currentTimeMillis();
    try (InputStream inputStream = src.read();
            DigestInputStream digestInputStream = new DigestInputStream(inputStream, md)) {
        blob.upload(digestInputStream, src.length(), null, getBlobRequestOptions(), Utils.updateUserAgent());
    }
    long endTime = System.currentTimeMillis();

    println("Uploaded to file storage with uri " + blob.getUri() + " in " + getTime(endTime - startTime));
    return DatatypeConverter.printHexBinary(md.digest());
}

From source file:com.lightszentip.module.security.password.PasswordModuleImpl.java

private byte[] hashPwd(String password, AlgorithmType algorithm)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = null;//from  w ww.java 2 s .  c o m
    if (AlgorithmType.MD5.equals(algorithm)) {
        md = DigestUtils.getMd5Digest();
    } else if (AlgorithmType.SHA_512.equals(algorithm)) {
        md = DigestUtils.getSha512Digest();
    } else if (AlgorithmType.SHA_384.equals(algorithm)) {
        md = DigestUtils.getSha384Digest();
    } else if (AlgorithmType.SHA_256.equals(algorithm)) {
        md = DigestUtils.getSha256Digest();
    } else if (AlgorithmType.SHA_1.equals(algorithm)) {
        md = DigestUtils.getSha1Digest();
    }
    md.update(password.getBytes());
    return md.digest();
}

From source file:eu.cassandra.sim.utilities.Utils.java

public static boolean authenticate(String headerMessage, DB db) {
    String username = extractUsername(headerMessage);
    String password = extractPassword(headerMessage);
    if (username == null || password == null)
        return false;
    DBObject user = getUser(username, db);
    if (user == null)
        return false;
    String user_id = user.get("_id").toString();
    String passwordHash = user.get("password").toString();
    MessageDigest m = DigestUtils.getMd5Digest();
    m.update((password + user_id).getBytes(), 0, (password + user_id).length());
    String output = new BigInteger(1, m.digest()).toString(16);
    return passwordHash.equals(output);
}

From source file:com.inventage.nexusaptplugin.DebianIndexCreator.java

@Override
public void populateArtifactInfo(ArtifactContext ac) throws IOException {
    if (ac.getArtifact() != null && "deb".equals(ac.getArtifactInfo().packaging)) {
        List<String> control = GetControl.doGet(ac.getArtifact());
        ac.getArtifactInfo().getAttributes().putAll(DebControlParser.parse(control));
        ac.getArtifactInfo().getAttributes().put("Filename", getRelativeFileNameOfArtifact(ac));

        FileInputStream is = null;
        try {//  w w  w  .jav  a2s.com
            is = new FileInputStream(ac.getArtifact());
            MessageDigest md5d = DigestUtils.getMd5Digest();
            MessageDigest sha256 = DigestUtils.getSha256Digest();
            MessageDigest sha512 = DigestUtils.getSha512Digest();

            int count;
            byte[] b = new byte[512];
            while ((count = is.read(b)) >= 0) {
                md5d.update(b, 0, count);
                sha256.update(b, 0, count);
                sha512.update(b, 0, count);
            }

            ac.getArtifactInfo().md5 = Hex.encodeHexString(md5d.digest());
            ac.getArtifactInfo().getAttributes().put(DEBIAN.SHA256.getFieldName(),
                    Hex.encodeHexString(sha256.digest()));
            ac.getArtifactInfo().getAttributes().put(DEBIAN.SHA512.getFieldName(),
                    Hex.encodeHexString(sha512.digest()));
        } finally {
            is.close();
        }
    }
}

From source file:com.microsoftopentechnologies.windowsazurestorage.WAStorageClient.java

/**
 *
 * @param listener//w ww.  j a  va2  s .c o  m
 * @param blob
 * @param src
 * @throws StorageException
 * @throws IOException
 * @throws InterruptedException
* @returns Md5 hash of the uploaded file in hexadecimal encoding
 */
protected static String upload(TaskListener listener, CloudBlockBlob blob, FilePath src)
        throws StorageException, IOException, InterruptedException {
    MessageDigest md = DigestUtils.getMd5Digest();
    long startTime = System.currentTimeMillis();
    try (InputStream inputStream = src.read();
            DigestInputStream digestInputStream = new DigestInputStream(inputStream, md)) {
        blob.upload(digestInputStream, src.length(), null, getBlobRequestOptions(), Utils.updateUserAgent());
    }
    long endTime = System.currentTimeMillis();
    listener.getLogger()
            .println("Uploaded blob with uri " + blob.getUri() + " in " + getTime(endTime - startTime));
    return DatatypeConverter.printHexBinary(md.digest());
}