Example usage for java.security DigestOutputStream getMessageDigest

List of usage examples for java.security DigestOutputStream getMessageDigest

Introduction

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

Prototype

public MessageDigest getMessageDigest() 

Source Link

Document

Returns the message digest associated with this stream.

Usage

From source file:org.exist.mongodb.xquery.gridfs.Store.java

void writeCompressed(GridFSInputFile gfsFile, StopWatch stopWatch, Item content, int dataType)
        throws NoSuchAlgorithmException, IOException, XPathException {
    // Store data compressed, add statistics
    try (OutputStream stream = gfsFile.getOutputStream()) {
        MessageDigest md = MessageDigest.getInstance("MD5");
        CountingOutputStream cosGZ = new CountingOutputStream(stream);
        GZIPOutputStream gos = new GZIPOutputStream(cosGZ);
        DigestOutputStream dos = new DigestOutputStream(gos, md);
        CountingOutputStream cosRaw = new CountingOutputStream(dos);

        stopWatch.start();/*from  w ww. ja  v a2  s . c  om*/
        ContentSerializer.serialize(content, context, cosRaw);
        cosRaw.flush();
        cosRaw.close();
        stopWatch.stop();

        long nrBytesRaw = cosRaw.getByteCount();
        long nrBytesGZ = cosGZ.getByteCount();
        String checksum = Hex.encodeHexString(dos.getMessageDigest().digest());

        BasicDBObject info = new BasicDBObject();
        info.put(Constants.EXIST_COMPRESSION, GZIP);
        info.put(Constants.EXIST_ORIGINAL_SIZE, nrBytesRaw);
        info.put(Constants.EXIST_ORIGINAL_MD5, checksum);
        info.put(Constants.EXIST_DATATYPE, dataType);
        info.put(Constants.EXIST_DATATYPE_TEXT, Type.getTypeName(dataType));

        gfsFile.setMetaData(info);

        LOG.info("original_md5:" + checksum);
        LOG.info("compression ratio:" + ((100l * nrBytesGZ) / nrBytesRaw));

    }
}

From source file:org.geogit.storage.AbstractObjectDatabase.java

/**
 * @see org.geogit.storage.ObjectDatabase#put(org.geogit.storage.ObjectWriter)
 */// ww w . j a  v a  2s . c  om
@Override
public final <T> ObjectId put(final ObjectWriter<T> writer) throws Exception {
    MessageDigest sha1;
    sha1 = MessageDigest.getInstance("SHA1");

    ByteArrayOutputStream rawOut = new ByteArrayOutputStream();

    DigestOutputStream keyGenOut = new DigestOutputStream(rawOut, sha1);
    // GZIPOutputStream cOut = new GZIPOutputStream(keyGenOut);
    LZFOutputStream cOut = new LZFOutputStream(keyGenOut);

    try {
        writer.write(cOut);
    } finally {
        // cOut.finish();
        cOut.flush();
        cOut.close();
        keyGenOut.flush();
        keyGenOut.close();
        rawOut.flush();
        rawOut.close();
    }

    final byte[] rawData = rawOut.toByteArray();
    final byte[] rawKey = keyGenOut.getMessageDigest().digest();
    final ObjectId id = new ObjectId(rawKey);
    putInternal(id, rawData, false);
    return id;
}

From source file:org.sourcepit.m2p2.cache.FileCacheTransport.java

private IStatus cache(URI toDownload, OutputStream target, IProgressMonitor monitor,
        final IArtifactDescriptor descriptor, File artifactFile, String md5) {
    log.log(LogService.LOG_INFO, "Downloading " + descriptor.getArtifactKey().toExternalForm());

    final MessageDigest md5Digest = newMd5Digest();

    final File tmpFile;
    try {//w  w w  .  ja v a  2  s.  c  om
        tmpFile = createTempFile(artifactFile);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    final IStatus result;

    DigestOutputStream out = null;
    try {
        out = new DigestOutputStream(
                new CopyOutputStream(target, new BufferedOutputStream(new FileOutputStream(tmpFile))),
                md5Digest);
        result = this.target.download(toDownload, out, monitor);
        out.flush();
    } catch (IOException e) {
        deleteQuietly(tmpFile);
        throw new IllegalStateException(e);
    } finally {
        IOUtils.closeQuietly(out);
    }

    final String actualMd5 = toHexString(out.getMessageDigest().digest());
    if (!md5.equals(actualMd5)) {
        log.log(LogService.LOG_WARNING,
                "Unable to cache artifact " + descriptor.getArtifactKey().toExternalForm()
                        + " due to checksum verification failure. Expected " + md5 + " but was " + actualMd5
                        + ".");
        deleteQuietly(tmpFile);
    } else {
        try {
            deleteFile(artifactFile);
            moveFile(tmpFile, artifactFile);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    return result;
}

From source file:pxb.android.tinysign.TinySign.java

private static void doFile(String name, File f, ZipOutputStream zos, DigestOutputStream dos, Manifest m)
        throws IOException {
    zos.putNextEntry(new ZipEntry(name));
    FileInputStream fis = FileUtils.openInputStream(f);
    IOUtils.copy(fis, dos);// w w w . j  a  v  a 2  s  .  c o m
    IOUtils.closeQuietly(fis);
    byte[] digets = dos.getMessageDigest().digest();
    zos.closeEntry();
    Attributes attr = new Attributes();
    attr.putValue("SHA1-Digest", eBase64(digets));
    m.getEntries().put(name, attr);
}