Search in sources :

Example 1 with StreamableDigest

use of org.exist.util.crypto.digest.StreamableDigest in project exist by eXist-db.

the class BlobStoreImpl method getDigest.

@Override
@Nullable
public MessageDigest getDigest(final Txn transaction, final BlobId blobId, final DigestType digestType) throws IOException {
    if (this.digestType.equals(digestType)) {
        // optimisation, we can just return the BlobId as that is the digest for this digest type!
        return new MessageDigest(digestType, blobId.getId());
    } else {
        // calculate the digest
        final StreamableDigest streamableDigest = digestType.newStreamableDigest();
        final Try<MessageDigest, IOException> result = with(transaction, blobId, maybeBlobFile -> maybeBlobFile == null ? null : TaggedTryUnchecked(IOException.class, () -> {
            FileUtils.digest(maybeBlobFile, streamableDigest);
            return new MessageDigest(streamableDigest.getDigestType(), streamableDigest.getMessageDigest());
        }));
        return result.get();
    }
}
Also used : StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) IOException(java.io.IOException) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Nullable(javax.annotation.Nullable)

Example 2 with StreamableDigest

use of org.exist.util.crypto.digest.StreamableDigest in project exist by eXist-db.

the class BlobStoreImpl method stage.

/**
 * Stages a BLOB file.
 *
 * Writes a BLOB to a file in the Blob Store staging area.
 *
 * @param is data stream for the BLOB.
 * @return The file path, length and checksum of the staged BLOB
 * @throws IOException if an error occurs whilst staging the BLOB.
 */
private Tuple3<Path, Long, MessageDigest> stage(final InputStream is) throws IOException {
    final Path stageFile = stagingDir.resolve(UUIDGenerator.getUUIDversion4());
    final CountingInputStream cis = new CountingInputStream(is);
    final StreamableDigest streamableDigest = digestType.newStreamableDigest();
    final DigestInputStream dis = new DigestInputStream(cis, streamableDigest);
    Files.copy(dis, stageFile);
    return Tuple(stageFile, cis.getByteCount(), streamableDigest.copyMessageDigest());
}
Also used : Path(java.nio.file.Path) DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) CountingInputStream(org.apache.commons.io.input.CountingInputStream)

Example 3 with StreamableDigest

use of org.exist.util.crypto.digest.StreamableDigest in project exist by eXist-db.

the class BlobStoreImplTest method generateTestFile.

private Tuple2<byte[], MessageDigest> generateTestFile() {
    // generate random data
    // 1MB
    final byte[] data = new byte[1024 * 1024];
    random.nextBytes(data);
    // get the checksum of the random data
    final StreamableDigest streamableDigest = DIGEST_TYPE.newStreamableDigest();
    streamableDigest.update(data);
    final MessageDigest expectedDataDigest = streamableDigest.copyMessageDigest();
    return Tuple(data, expectedDataDigest);
}
Also used : StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 4 with StreamableDigest

use of org.exist.util.crypto.digest.StreamableDigest in project exist by eXist-db.

the class BlobStoreImplTest method readAll.

private Tuple2<byte[], MessageDigest> readAll(InputStream is) throws IOException {
    final StreamableDigest streamableDigest = DIGEST_TYPE.newStreamableDigest();
    is = new DigestInputStream(is, streamableDigest);
    try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        os.write(is);
        return Tuple(os.toByteArray(), streamableDigest.copyMessageDigest());
    }
}
Also used : DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 5 with StreamableDigest

use of org.exist.util.crypto.digest.StreamableDigest in project exist by eXist-db.

the class BlobStoreRecoveryTest method removeNoCommit.

private void removeNoCommit(final TransactionManager transactionManager, final BlobStore blobStore, final byte[] blob) throws IOException, TransactionException {
    final StreamableDigest streamableDigest = DIGEST_TYPE.newStreamableDigest();
    streamableDigest.update(blob);
    final BlobId blobId = new BlobId(streamableDigest.getMessageDigest());
    final Txn transaction = transactionManager.beginTransaction();
    blobStore.remove(transaction, blobId);
// NOTE must not use ARM to close the transaction, otherwise it will auto-abort!
}
Also used : StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) Txn(org.exist.storage.txn.Txn)

Aggregations

StreamableDigest (org.exist.util.crypto.digest.StreamableDigest)10 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 MessageDigest (org.exist.util.crypto.digest.MessageDigest)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)2 Txn (org.exist.storage.txn.Txn)2 Nullable (javax.annotation.Nullable)1 CountingInputStream (org.apache.commons.io.input.CountingInputStream)1 BlobId (org.exist.storage.blob.BlobId)1