Example usage for java.security DigestOutputStream DigestOutputStream

List of usage examples for java.security DigestOutputStream DigestOutputStream

Introduction

In this page you can find the example usage for java.security DigestOutputStream DigestOutputStream.

Prototype

public DigestOutputStream(OutputStream stream, MessageDigest digest) 

Source Link

Document

Creates a digest output stream, using the specified output stream and message digest.

Usage

From source file:de.blizzy.backup.check.CheckRun.java

private FileCheckResult checkFile(String backupPath, String checksum, long length, Compression compression)
        throws IOException {

    File backupFile = Utils.toBackupFile(backupPath, outputFolder);
    if (backupFile.isFile()) {
        InputStream in = null;/*  ww w .ja  v  a2  s.  c om*/
        OutputStream out = null;
        try {
            InputStream fileIn = new BufferedInputStream(new FileInputStream(backupFile));
            InputStream interceptIn = fileIn;
            for (IStorageInterceptor interceptor : storageInterceptors) {
                interceptIn = interceptor.interceptInputStream(interceptIn, length);
            }
            InputStream compressIn = compression.getInputStream(interceptIn);
            LengthOutputStream lengthOut = new LengthOutputStream(new NullOutputStream());
            MessageDigest digest = MessageDigest.getInstance("SHA-256"); //$NON-NLS-1$
            out = new DigestOutputStream(lengthOut, digest);
            MessageDigest md5Digest = null;
            if (checksum.length() != SHA256_LENGTH) {
                md5Digest = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
                out = new DigestOutputStream(out, md5Digest);
            }
            in = compressIn;
            IOUtils.copy(in, out);
            out.flush();

            String fileChecksum = Hex.encodeHexString(digest.digest());
            String fileChecksumMD5 = (md5Digest != null) ? Hex.encodeHexString(md5Digest.digest()) : null;
            long fileLength = lengthOut.getLength();
            boolean ok = (fileLength == length)
                    && checksum.equals((checksum.length() == SHA256_LENGTH) ? fileChecksum : fileChecksumMD5);
            return new FileCheckResult(ok, fileChecksum);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
    return FileCheckResult.BROKEN;
}

From source file:alluxio.proxy.s3.S3RestServiceHandler.java

/**
 * @summary uploads an object or part of an object in multipart upload
 * @param contentMD5 the optional Base64 encoded 128-bit MD5 digest of the object
 * @param bucket the bucket name//from   w w  w  .ja  va  2 s .c o  m
 * @param object the object name
 * @param partNumber the identification of the part of the object in multipart upload,
 *                   otherwise null
 * @param uploadId the upload ID of the multipart upload, otherwise null
 * @param is the request body
 * @return the response object
 */
@PUT
@Path(OBJECT_PARAM)
@ReturnType("java.lang.Void")
@Consumes(MediaType.WILDCARD)
public Response createObjectOrUploadPart(@HeaderParam("Content-MD5") final String contentMD5,
        @PathParam("bucket") final String bucket, @PathParam("object") final String object,
        @QueryParam("partNumber") final Integer partNumber, @QueryParam("uploadId") final Long uploadId,
        final InputStream is) {
    return S3RestUtils.call(bucket, new S3RestUtils.RestCallable<Response>() {
        @Override
        public Response call() throws S3Exception {
            Preconditions.checkNotNull(bucket, "required 'bucket' parameter is missing");
            Preconditions.checkNotNull(object, "required 'object' parameter is missing");
            Preconditions.checkArgument(
                    (partNumber == null && uploadId == null) || (partNumber != null && uploadId != null),
                    "'partNumber' and 'uploadId' parameter should appear together or be "
                            + "missing together.");

            String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket);
            checkBucketIsAlluxioDirectory(bucketPath);

            String objectPath = bucketPath + AlluxioURI.SEPARATOR + object;
            if (partNumber != null) {
                // This object is part of a multipart upload, should be uploaded into the temporary
                // directory first.
                String tmpDir = S3RestUtils.getMultipartTemporaryDirForObject(bucketPath, object);
                checkUploadId(new AlluxioURI(tmpDir), uploadId);
                objectPath = tmpDir + AlluxioURI.SEPARATOR + Integer.toString(partNumber);
            }
            AlluxioURI objectURI = new AlluxioURI(objectPath);

            try {
                CreateFileOptions options = CreateFileOptions.defaults().setRecursive(true)
                        .setWriteType(getS3WriteType());
                FileOutStream os = mFileSystem.createFile(objectURI, options);
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                DigestOutputStream digestOutputStream = new DigestOutputStream(os, md5);

                try {
                    ByteStreams.copy(is, digestOutputStream);
                } finally {
                    digestOutputStream.close();
                }

                byte[] digest = md5.digest();
                String base64Digest = BaseEncoding.base64().encode(digest);
                if (contentMD5 != null && !contentMD5.equals(base64Digest)) {
                    // The object may be corrupted, delete the written object and return an error.
                    try {
                        mFileSystem.delete(objectURI);
                    } catch (Exception e2) {
                        // intend to continue and return BAD_DIGEST S3Exception.
                    }
                    throw new S3Exception(objectURI.getPath(), S3ErrorCode.BAD_DIGEST);
                }

                String entityTag = Hex.encodeHexString(digest);
                return Response.ok().tag(entityTag).build();
            } catch (Exception e) {
                throw toObjectS3Exception(e, objectPath);
            }
        }
    });
}

From source file:com.oneis.appserver.FileUploads.java

/**
 * Handle the incoming stream, processing files.
 *//*from   w  w  w .ja  v  a 2s .  c o m*/
public void performUploads(HttpServletRequest request) throws IOException, UserReportableFileUploadException {
    instructionsRequired = false;

    // Need a parser for parameters
    ParameterParser paramParser = new ParameterParser();
    paramParser.setLowerCaseNames(true);

    // Thread ID is used for temporary filenames
    long threadId = Thread.currentThread().getId();
    int fileId = 0;

    InputStream requestBody = request.getInputStream();

    MultipartStream multipart = new MultipartStream(requestBody, boundary);
    multipart.setHeaderEncoding("UTF-8");

    boolean nextPart = multipart.skipPreamble();
    while (nextPart) {
        String headerPart = multipart.readHeaders();

        // Parse headers, splitting out the bits we're interested in
        String name = null;
        String filename = null;
        Map<String, String> itemHeaders = HeaderParser.parse(headerPart, true /* keys to lower case */);
        String mimeType = itemHeaders.get("content-type");
        String disposition = itemHeaders.get("content-disposition");
        if (disposition != null) {
            Map disp = paramParser.parse(disposition, PARAMPARSER_SEPERATORS);
            name = (String) disp.get("name");
            filename = (String) disp.get("filename");
        }

        // Set a default MIME type if none is given (Safari may omit it)
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }

        // Remove the path prefix from IE (before the length check)
        if (filename != null) {
            int slash1 = filename.lastIndexOf('/');
            int slash2 = filename.lastIndexOf('\\');
            int slash = (slash1 > slash2) ? slash1 : slash2;
            if (slash != -1) {
                filename = filename.substring(slash + 1);
            }
        }

        boolean isFile = (filename != null && filename.length() > 0);

        if (isFile) {
            // File - did the app server give instructions about it?
            Upload upload = files.get(name);
            if (upload == null) {
                // Looks like a file, but the app server didn't say it should be. Give up.
                throw new UserReportableFileUploadException(
                        "A file was uploaded, but it was not expected by the application. Field name: '" + name
                                + "'");
            }

            String dir = upload.getSaveDirectory();
            if (dir == null) {
                // Ooops.
                throw new IOException("app server didn't specify dir");
            }

            // Generate a temporary filename
            File outFile = null;
            do {
                outFile = new File(String.format("%1$s/u%2$d.%3$d", dir, threadId, fileId++));
            } while (!outFile.createNewFile());

            OutputStream outStream = new FileOutputStream(outFile);

            // Decorate with a digest?
            MessageDigest digest = null;
            if (upload.getDigestName() != null) {
                try {
                    digest = MessageDigest.getInstance(upload.getDigestName());
                } catch (java.security.NoSuchAlgorithmException e) {
                    digest = null;
                }
                if (digest != null) {
                    outStream = new DigestOutputStream(outStream, digest);
                }
            }

            // Decorate with a decompressor?
            String filterName = upload.getFilterName();
            if (filterName != null && filterName.equals("inflate")) {
                outStream = new InflaterOutputStream(outStream);
            }

            multipart.readBodyData(outStream);
            outStream.close();

            String digestAsString = null;
            if (digest != null) {
                String.format("%1$s_digest", name);
                digestAsString = StringUtils.bytesToHex(digest.digest());
            }

            upload.setUploadDetails(outFile.getPath(), digestAsString, mimeType, filename, outFile.length());
        } else {
            // Normal field - just absorb a few k max of it, turn it into a field
            ByteArrayOutputStream value = new ByteArrayOutputStream();
            // TODO: Limit request size as a whole, not on a per-parameter basis.
            multipart.readBodyData(new LimitedFilterOutputStream(value, MAX_TEXT_PARAMETER_LENGTH));
            params.put(name, value.toString("UTF-8"));
        }

        nextPart = multipart.readBoundary();
    }
}

From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java

/**
 * Verifies the signature in the update center data file.
 *///from  ww  w  .  j  a  va  2  s. c o  m
private FormValidation verifySignature(final JSONObject o) throws IOException {
    try {
        FormValidation warning = null;

        final JSONObject signature = o.getJSONObject("signature");
        if (signature.isNullObject()) {
            return FormValidation.error("No signature block found in update center '" + getId() + "'");
        }
        o.remove("signature");

        final List<X509Certificate> certs = new ArrayList<X509Certificate>();
        {// load and verify certificates
            final CertificateFactory cf = CertificateFactory.getInstance("X509");
            for (final Object cert : signature.getJSONArray("certificates")) {
                final X509Certificate c = (X509Certificate) cf.generateCertificate(
                        new ByteArrayInputStream(Base64.decode(cert.toString().toCharArray())));
                try {
                    c.checkValidity();
                } catch (final CertificateExpiredException e) { // even if the certificate isn't valid yet,
                    // we'll proceed it anyway
                    warning = FormValidation.warning(e, String.format(
                            "Certificate %s has expired in update center '%s'", cert.toString(), getId()));
                } catch (final CertificateNotYetValidException e) {
                    warning = FormValidation.warning(e, String.format(
                            "Certificate %s is not yet valid in update center '%s'", cert.toString(), getId()));
                }
                certs.add(c);
            }

            // all default root CAs in JVM are trusted, plus certs bundled in Jenkins
            final Set<TrustAnchor> anchors = new HashSet<TrustAnchor>(); // CertificateUtil.getDefaultRootCAs();
            final ServletContext context = Jenkins.getInstance().servletContext;
            anchors.add(new TrustAnchor(loadLicenseCaCertificate(), null));
            for (final String cert : (Set<String>) context.getResourcePaths("/WEB-INF/update-center-rootCAs")) {
                if (cert.endsWith(".txt")) {
                    continue; // skip text files that are meant to be documentation
                }
                final InputStream stream = context.getResourceAsStream(cert);
                if (stream != null) {
                    try {
                        anchors.add(new TrustAnchor((X509Certificate) cf.generateCertificate(stream), null));
                    } finally {
                        IOUtils.closeQuietly(stream);
                    }
                }
            }
            CertificateUtil.validatePath(certs, anchors);
        }

        // this is for computing a digest to check sanity
        final MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        final DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), sha1);

        // this is for computing a signature
        final Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initVerify(certs.get(0));
        final SignatureOutputStream sos = new SignatureOutputStream(sig);

        // until JENKINS-11110 fix, UC used to serve invalid digest (and therefore unverifiable signature)
        // that only covers the earlier portion of the file. This was caused by the lack of close() call
        // in the canonical writing, which apparently leave some bytes somewhere that's not flushed to
        // the digest output stream. This affects Jenkins [1.424,1,431].
        // Jenkins 1.432 shipped with the "fix" (1eb0c64abb3794edce29cbb1de50c93fa03a8229) that made it
        // compute the correct digest, but it breaks all the existing UC json metadata out there. We then
        // quickly discovered ourselves in the catch-22 situation. If we generate UC with the correct signature,
        // it'll cut off [1.424,1.431] from the UC. But if we don't, we'll cut off [1.432,*).
        //
        // In 1.433, we revisited 1eb0c64abb3794edce29cbb1de50c93fa03a8229 so that the original "digest"/"signature"
        // pair continues to be generated in a buggy form, while "correct_digest"/"correct_signature" are generated
        // correctly.
        //
        // Jenkins should ignore "digest"/"signature" pair. Accepting it creates a vulnerability that allows
        // the attacker to inject a fragment at the end of the json.
        o.writeCanonical(new OutputStreamWriter(new TeeOutputStream(dos, sos), "UTF-8")).close();

        // did the digest match? this is not a part of the signature validation, but if we have a bug in the c14n
        // (which is more likely than someone tampering with update center), we can tell
        final String computedDigest = new String(Base64.encode(sha1.digest()));
        final String providedDigest = signature.optString("correct_digest");
        if (providedDigest == null) {
            return FormValidation.error("No correct_digest parameter in update center '" + getId()
                    + "'. This metadata appears to be old.");
        }
        if (!computedDigest.equalsIgnoreCase(providedDigest)) {
            return FormValidation.error("Digest mismatch: " + computedDigest + " vs " + providedDigest
                    + " in update center '" + getId() + "'");
        }

        final String providedSignature = signature.getString("correct_signature");
        if (!sig.verify(Base64.decode(providedSignature.toCharArray()))) {
            return FormValidation.error(
                    "Signature in the update center doesn't match with the certificate in update center '"
                            + getId() + "'");
        }

        if (warning != null) {
            return warning;
        }
        return FormValidation.ok();
    } catch (final GeneralSecurityException e) {
        return FormValidation.error(e, "Signature verification failed in the update center '" + getId() + "'");
    }
}

From source file:com.cloudbees.jenkins.plugins.enterpriseplugins.CloudBeesUpdateSite.java

/**
 * Verifies the signature in the update center data file.
 *///from www. j a  va 2 s.  co m
private FormValidation verifySignature(JSONObject o) throws IOException {
    try {
        FormValidation warning = null;

        JSONObject signature = o.getJSONObject("signature");
        if (signature.isNullObject()) {
            return FormValidation.error("No signature block found in update center '" + getId() + "'");
        }
        o.remove("signature");

        List<X509Certificate> certs = new ArrayList<X509Certificate>();
        {// load and verify certificates
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            for (Object cert : signature.getJSONArray("certificates")) {
                X509Certificate c = (X509Certificate) cf.generateCertificate(
                        new ByteArrayInputStream(Base64.decode(cert.toString().toCharArray())));
                try {
                    c.checkValidity();
                } catch (CertificateExpiredException e) { // even if the certificate isn't valid yet,
                    // we'll proceed it anyway
                    warning = FormValidation.warning(e, String.format(
                            "Certificate %s has expired in update center '%s'", cert.toString(), getId()));
                } catch (CertificateNotYetValidException e) {
                    warning = FormValidation.warning(e, String.format(
                            "Certificate %s is not yet valid in update center '%s'", cert.toString(), getId()));
                }
                certs.add(c);
            }

            // all default root CAs in JVM are trusted, plus certs bundled in Jenkins
            Set<TrustAnchor> anchors = new HashSet<TrustAnchor>(); // CertificateUtil.getDefaultRootCAs();
            ServletContext context = Hudson.getInstance().servletContext;
            anchors.add(new TrustAnchor(loadLicenseCaCertificate(), null));
            for (String cert : (Set<String>) context.getResourcePaths("/WEB-INF/update-center-rootCAs")) {
                if (cert.endsWith(".txt")) {
                    continue; // skip text files that are meant to be documentation
                }
                InputStream stream = context.getResourceAsStream(cert);
                if (stream != null) {
                    try {
                        anchors.add(new TrustAnchor((X509Certificate) cf.generateCertificate(stream), null));
                    } finally {
                        IOUtils.closeQuietly(stream);
                    }
                }
            }
            CertificateUtil.validatePath(certs, anchors);
        }

        // this is for computing a digest to check sanity
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), sha1);

        // this is for computing a signature
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initVerify(certs.get(0));
        SignatureOutputStream sos = new SignatureOutputStream(sig);

        // until JENKINS-11110 fix, UC used to serve invalid digest (and therefore unverifiable signature)
        // that only covers the earlier portion of the file. This was caused by the lack of close() call
        // in the canonical writing, which apparently leave some bytes somewhere that's not flushed to
        // the digest output stream. This affects Jenkins [1.424,1,431].
        // Jenkins 1.432 shipped with the "fix" (1eb0c64abb3794edce29cbb1de50c93fa03a8229) that made it
        // compute the correct digest, but it breaks all the existing UC json metadata out there. We then
        // quickly discovered ourselves in the catch-22 situation. If we generate UC with the correct signature,
        // it'll cut off [1.424,1.431] from the UC. But if we don't, we'll cut off [1.432,*).
        //
        // In 1.433, we revisited 1eb0c64abb3794edce29cbb1de50c93fa03a8229 so that the original "digest"/"signature"
        // pair continues to be generated in a buggy form, while "correct_digest"/"correct_signature" are generated
        // correctly.
        //
        // Jenkins should ignore "digest"/"signature" pair. Accepting it creates a vulnerability that allows
        // the attacker to inject a fragment at the end of the json.
        o.writeCanonical(new OutputStreamWriter(new TeeOutputStream(dos, sos), "UTF-8")).close();

        // did the digest match? this is not a part of the signature validation, but if we have a bug in the c14n
        // (which is more likely than someone tampering with update center), we can tell
        String computedDigest = new String(Base64.encode(sha1.digest()));
        String providedDigest = signature.optString("correct_digest");
        if (providedDigest == null) {
            return FormValidation.error("No correct_digest parameter in update center '" + getId()
                    + "'. This metadata appears to be old.");
        }
        if (!computedDigest.equalsIgnoreCase(providedDigest)) {
            return FormValidation.error("Digest mismatch: " + computedDigest + " vs " + providedDigest
                    + " in update center '" + getId() + "'");
        }

        String providedSignature = signature.getString("correct_signature");
        if (!sig.verify(Base64.decode(providedSignature.toCharArray()))) {
            return FormValidation.error(
                    "Signature in the update center doesn't match with the certificate in update center '"
                            + getId() + "'");
        }

        if (warning != null) {
            return warning;
        }
        return FormValidation.ok();
    } catch (GeneralSecurityException e) {
        return FormValidation.error(e, "Signature verification failed in the update center '" + getId() + "'");
    }
}

From source file:de.elomagic.carafile.client.CaraFileClient.java

/**
 * Downloads a file into a {@link OutputStream}.
 *
 * @param md {@link MetaData} of the file.
 * @param out The output stream. It's not recommended to use a buffered stream.
 * @throws IOException Thrown when unable to write file into the output stream or the SHA-1 validation failed.
 *//*w ww .  j av a 2  s  . c  o  m*/
public void downloadFile(final MetaData md, final OutputStream out) throws IOException {
    if (md == null) {
        throw new IllegalArgumentException("Parameter 'md' must not be null!");
    }

    if (out == null) {
        throw new IllegalArgumentException("Parameter 'out' must not be null!");
    }

    Map<String, Path> downloadedChunks = new HashMap<>();
    Set<String> chunksToDownload = new HashSet<>();
    for (ChunkData chunkData : md.getChunks()) {
        chunksToDownload.add(chunkData.getId());
    }

    try {
        while (!chunksToDownload.isEmpty()) {
            PeerChunk pc = peerChunkSelector.getNext(md, chunksToDownload);
            if (pc == null || pc.getPeerURI() == null) {
                throw new IOException("No peer found or selected for download");
            }

            Path chunkFile = Files.createTempFile("fs_", ".tmp");
            try (OutputStream chunkOut = Files.newOutputStream(chunkFile, StandardOpenOption.APPEND)) {
                downloadShunk(pc, md, chunkOut);

                downloadedChunks.put(pc.getChunkId(), chunkFile);
                chunksToDownload.remove(pc.getChunkId());

                chunkOut.flush();
            } catch (Exception ex) {
                Files.deleteIfExists(chunkFile);
                throw ex;
            }
        }

        MessageDigest messageDigest = DigestUtils.getSha1Digest();

        // Write chunk on correct order to file.
        try (DigestOutputStream dos = new DigestOutputStream(out, messageDigest);
                BufferedOutputStream bos = new BufferedOutputStream(dos, md.getChunkSize())) {
            for (ChunkData chunk : md.getChunks()) {
                Path chunkPath = downloadedChunks.get(chunk.getId());
                Files.copy(chunkPath, bos);
            }
        }

        String sha1 = Hex.encodeHexString(messageDigest.digest());
        if (!sha1.equalsIgnoreCase(md.getId())) {
            throw new IOException(
                    "SHA1 validation of file failed. Expected " + md.getId() + " but was " + sha1);
        }
    } finally {
        for (Path path : downloadedChunks.values()) {
            try {
                Files.deleteIfExists(path);
            } catch (IOException ex) {
                LOG.error("Unable to delete chunk " + path.toString() + "; " + ex.getMessage(), ex);
            }
        }
    }
}

From source file:dk.netarkivet.common.distribute.FTPRemoteFile.java

/**
 * Write the contents of this ftp remote file to an output stream. Notice that while the checksum of the transferred
 * data is checked, no retries are performed, and in case of failure, there is no guarantee that any data have been
 * transferred./*  w ww.j  av  a  2  s .c  o  m*/
 *
 * @param out OutputStream that the data will be written to. This stream will not be closed by this operation.
 * @throws IOFailure If append operation fails
 */
@Override
public void appendTo(OutputStream out) {
    ArgumentNotValid.checkNotNull(out, "OutputStream out");

    if (filesize == 0) {
        return;
    }

    try {
        cm.logOn();

        if (useChecksums) {
            out = new DigestOutputStream(out, ChecksumCalculator.getMessageDigest(ChecksumCalculator.MD5));
        }
        if (!cm.getFTPClient().retrieveFile(ftpFileName, out)) {
            final String msg = "Append operation from '" + ftpFileName + "' failed: " + cm.getFtpErrorMessage();
            log.warn(msg);
            throw new IOFailure(msg);
        }
        out.flush();
        if (useChecksums) {
            String newChecksum = ChecksumCalculator
                    .toHex(((DigestOutputStream) out).getMessageDigest().digest());
            if (checksum != null && !checksum.equals(newChecksum)) {
                final String msg = "Checksums of '" + ftpFileName + "' do not match! Should be " + checksum
                        + " but was " + newChecksum;
                log.warn(msg);
                throw new IOFailure(msg);
            }
        }
    } catch (IOException e) {
        String msg = "Append operation from '" + ftpFileName + "' failed ";
        if (e instanceof CopyStreamException) {
            CopyStreamException realException = (CopyStreamException) e;
            msg += "(real cause = " + realException.getIOException() + ")";
        }
        log.warn(msg, e);
        throw new IOFailure(msg, e);
    } finally {
        cm.logOut();
        if (!multipleDownloads) {
            cleanup();
        }
    }
}

From source file:com.jrummyapps.busybox.signing.ZipSigner.java

/**
 * Write a .SF file with a digest the specified manifest.
 *//*from  w w w.  ja v a  2 s.co m*/
private static byte[] writeSignatureFile(Manifest manifest, OutputStream out)
        throws IOException, GeneralSecurityException {
    final Manifest sf = new Manifest();
    final Attributes main = sf.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true,
            "UTF-8");

    // Digest of the entire manifest
    manifest.write(print);
    print.flush();
    main.putValue("SHA1-Digest-Manifest", base64encode(md.digest()));

    final Map<String, Attributes> entries = manifest.getEntries();
    for (final Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (final Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();

        final Attributes sfAttr = new Attributes();
        sfAttr.putValue("SHA1-Digest", base64encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }

    final ByteArrayOutputStream sos = new ByteArrayOutputStream();
    sf.write(sos);

    String value = sos.toString();
    String done = value.replace("Manifest-Version", "Signature-Version");

    out.write(done.getBytes());

    print.close();
    sos.close();

    return done.getBytes();
}

From source file:alluxio.proxy.s3.S3RestServiceHandler.java

private Response completeMultipartUpload(final String bucket, final String object, final long uploadId) {
    return S3RestUtils.call(bucket, new S3RestUtils.RestCallable<CompleteMultipartUploadResult>() {
        @Override//from w  w  w  . j a  v  a2 s.c o m
        public CompleteMultipartUploadResult call() throws S3Exception {
            String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket);
            checkBucketIsAlluxioDirectory(bucketPath);
            String objectPath = bucketPath + AlluxioURI.SEPARATOR + object;
            AlluxioURI multipartTemporaryDir = new AlluxioURI(
                    S3RestUtils.getMultipartTemporaryDirForObject(bucketPath, object));
            checkUploadId(multipartTemporaryDir, uploadId);

            try {
                List<URIStatus> parts = mFileSystem.listStatus(multipartTemporaryDir);
                Collections.sort(parts, new URIStatusNameComparator());

                CreateFileOptions options = CreateFileOptions.defaults().setRecursive(true)
                        .setWriteType(getS3WriteType());
                FileOutStream os = mFileSystem.createFile(new AlluxioURI(objectPath), options);
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                DigestOutputStream digestOutputStream = new DigestOutputStream(os, md5);

                try {
                    for (URIStatus part : parts) {
                        try (FileInStream is = mFileSystem.openFile(new AlluxioURI(part.getPath()))) {
                            ByteStreams.copy(is, digestOutputStream);
                        }
                    }
                } finally {
                    digestOutputStream.close();
                }

                mFileSystem.delete(multipartTemporaryDir, DeleteOptions.defaults().setRecursive(true));

                String entityTag = Hex.encodeHexString(md5.digest());
                return new CompleteMultipartUploadResult(objectPath, bucket, object, entityTag);
            } catch (Exception e) {
                throw toObjectS3Exception(e, objectPath);
            }
        }
    });
}

From source file:de.blizzy.backup.backup.BackupRun.java

private int backupFileContents(final IFile file, final File backupFile, String backupFilePath)
        throws IOException {

    FileUtils.forceMkdir(backupFile.getParentFile());

    final MessageDigest[] digest = new MessageDigest[1];
    IOutputStreamProvider outputStreamProvider = new IOutputStreamProvider() {
        @Override/*from   w  ww.j av  a 2  s  . c  o m*/
        public OutputStream getOutputStream() throws IOException {
            try {
                digest[0] = MessageDigest.getInstance("SHA-256"); //$NON-NLS-1$
                OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(backupFile));
                OutputStream interceptOut = fileOut;
                for (IStorageInterceptor interceptor : storageInterceptors) {
                    interceptOut = interceptor.interceptOutputStream(interceptOut, file.getLength());
                }
                OutputStream compressOut = Compression.BZIP2.getOutputStream(interceptOut);
                OutputStream digestOut = new DigestOutputStream(compressOut, digest[0]);
                return digestOut;
            } catch (GeneralSecurityException e) {
                throw new RuntimeException(e);
            }
        }
    };
    boolean fileCopied = false;
    try {
        file.copy(outputStreamProvider);
        fileCopied = true;
    } finally {
        if (!fileCopied) {
            try {
                Files.delete(backupFile.toPath());
            } catch (IOException e) {
                BackupPlugin.getDefault().logError("error while deleting file: " + //$NON-NLS-1$
                        backupFile.getAbsolutePath(), e);
                fireBackupErrorOccurred(e, BackupErrorEvent.Severity.WARNING);
            }
            removeFoldersIfEmpty(backupFile.getParentFile());
        }
    }

    String checksum = toHexString(digest[0]);

    database.factory().insertInto(Tables.FILES).set(Tables.FILES.BACKUP_PATH, backupFilePath)
            .set(Tables.FILES.CHECKSUM, checksum).set(Tables.FILES.LENGTH, Long.valueOf(file.getLength()))
            .set(Tables.FILES.COMPRESSION, Byte.valueOf((byte) Compression.BZIP2.getValue())).execute();
    return database.factory().lastID().intValue();
}