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();
}
}
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());
}
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);
}
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());
}
}
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!
}
Aggregations