Search in sources :

Example 71 with DigestOutputStream

use of java.security.DigestOutputStream in project jackrabbit-oak by apache.

the class S3DataStoreStatsTest method getIdForInputStream.

private String getIdForInputStream(final InputStream in) throws Exception {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    OutputStream output = new DigestOutputStream(new NullOutputStream(), digest);
    try {
        IOUtils.copyLarge(in, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
    }
    return encodeHexString(digest.digest());
}
Also used : DigestOutputStream(java.security.DigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 72 with DigestOutputStream

use of java.security.DigestOutputStream in project jackrabbit-oak by apache.

the class AzureDataStoreTest method getIdForInputStream.

private static String getIdForInputStream(final InputStream in) throws NoSuchAlgorithmException, IOException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    OutputStream output = new DigestOutputStream(new NullOutputStream(), digest);
    try {
        IOUtils.copyLarge(in, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
    }
    return encodeHexString(digest.digest());
}
Also used : DigestOutputStream(java.security.DigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 73 with DigestOutputStream

use of java.security.DigestOutputStream in project bnd by bndtools.

the class CachingUriResourceHandle method copyWithSHA.

private String copyWithSHA(InputStream input, OutputStream output) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance(SHA_256);
        DigestOutputStream digestOutput = new DigestOutputStream(output, digest);
        IO.copy(input, digestOutput);
        return Hex.toHexString(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        // Can't happen... hopefully...
        throw new IOException(e.getMessage(), e);
    } finally {
        IO.close(input);
        IO.close(output);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 74 with DigestOutputStream

use of java.security.DigestOutputStream in project sling by apache.

the class FileDistributionPackageBuilder method createPackageForAdd.

@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull final DistributionRequest request) throws DistributionException {
    DistributionPackage distributionPackage;
    OutputStream outputStream = null;
    String digestMessage = null;
    final File file;
    try {
        file = File.createTempFile("distrpck-create-" + randomUUID(), "." + getType(), tempDirectory);
        if (digestAlgorithm != null) {
            outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
        } else {
            outputStream = new FileOutputStream(file);
        }
        final DistributionExportFilter filter = distributionContentSerializer.isRequestFiltering() ? null : DistributionExportFilter.createFilter(request, nodeFilters, propertyFilters);
        DistributionExportOptions distributionExportOptions = new DistributionExportOptions(request, filter);
        distributionContentSerializer.exportToStream(resourceResolver, distributionExportOptions, outputStream);
        outputStream.flush();
        if (digestAlgorithm != null) {
            digestMessage = readDigestMessage((DigestOutputStream) outputStream);
        }
        distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
    } catch (IOException e) {
        throw new DistributionException(e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
    return distributionPackage;
}
Also used : DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException) File(java.io.File)

Example 75 with DigestOutputStream

use of java.security.DigestOutputStream in project Essentials by drtshock.

the class ManagedFile method copyResourceAscii.

public static void copyResourceAscii(final String resourceName, final File file) throws IOException {
    final InputStreamReader reader = new InputStreamReader(ManagedFile.class.getResourceAsStream(resourceName));
    try {
        final MessageDigest digest = getDigest();
        final DigestOutputStream digestStream = new DigestOutputStream(new FileOutputStream(file), digest);
        try {
            final OutputStreamWriter writer = new OutputStreamWriter(digestStream);
            try {
                final char[] buffer = new char[BUFFERSIZE];
                do {
                    final int length = reader.read(buffer);
                    if (length >= 0) {
                        writer.write(buffer, 0, length);
                    } else {
                        break;
                    }
                } while (true);
                writer.write("\n");
                writer.flush();
                final BigInteger hashInt = new BigInteger(1, digest.digest());
                digestStream.on(false);
                digestStream.write('#');
                digestStream.write(hashInt.toString(16).getBytes());
            } finally {
                writer.close();
            }
        } finally {
            digestStream.close();
        }
    } finally {
        reader.close();
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)107 MessageDigest (java.security.MessageDigest)87 ByteArrayOutputStream (java.io.ByteArrayOutputStream)54 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)44 IOException (java.io.IOException)41 OutputStream (java.io.OutputStream)26 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)13 Support_OutputStream (tests.support.Support_OutputStream)9 NullOutputStream (com.keepassdroid.stream.NullOutputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)8 InputStream (java.io.InputStream)8 Map (java.util.Map)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NullOutputStream (org.apache.commons.io.output.NullOutputStream)7 Base64OutputStream (android.util.Base64OutputStream)6 DigestInputStream (java.security.DigestInputStream)6 Attributes (java.util.jar.Attributes)6 DataOutputStream (java.io.DataOutputStream)5 FileInputStream (java.io.FileInputStream)5