use of java.security.DigestInputStream in project elasticsearch by elastic.
the class MockAmazonS3 method putObject.
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
String blobName = putObjectRequest.getKey();
DigestInputStream stream = (DigestInputStream) putObjectRequest.getInputStream();
if (blobs.containsKey(blobName)) {
throw new AmazonS3Exception("[" + blobName + "] already exists.");
}
blobs.put(blobName, stream);
// input and output md5 hashes need to match to avoid an exception
String md5 = Base64.encodeAsString(stream.getMessageDigest().digest());
PutObjectResult result = new PutObjectResult();
result.setContentMd5(md5);
return result;
}
use of java.security.DigestInputStream in project elasticsearch by elastic.
the class DefaultS3OutputStream method doUpload.
protected void doUpload(S3BlobStore blobStore, String bucketName, String blobName, InputStream is, int length, boolean serverSideEncryption) throws AmazonS3Exception {
ObjectMetadata md = new ObjectMetadata();
if (serverSideEncryption) {
md.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
md.setContentLength(length);
InputStream inputStream = is;
// We try to compute a MD5 while reading it
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("MD5");
inputStream = new DigestInputStream(is, messageDigest);
} catch (NoSuchAlgorithmException impossible) {
// Every implementation of the Java platform is required to support MD5 (see MessageDigest)
throw new RuntimeException(impossible);
}
PutObjectRequest putRequest = new PutObjectRequest(bucketName, blobName, inputStream, md).withStorageClass(blobStore.getStorageClass()).withCannedAcl(blobStore.getCannedACL());
PutObjectResult putObjectResult = blobStore.client().putObject(putRequest);
String localMd5 = Base64.encodeAsString(messageDigest.digest());
String remoteMd5 = putObjectResult.getContentMd5();
if (!localMd5.equals(remoteMd5)) {
logger.debug("MD5 local [{}], remote [{}] are not equal...", localMd5, remoteMd5);
throw new AmazonS3Exception("MD5 local [" + localMd5 + "], remote [" + remoteMd5 + "] are not equal...");
}
}
use of java.security.DigestInputStream in project mongo-java-driver by mongodb.
the class CLI method main.
// CHECKSTYLE:OFF
public static void main(final String[] args) throws Exception {
if (args.length < 1) {
printUsage();
return;
}
for (int i = 0; i < args.length; i++) {
String s = args[i];
if (s.equals("--db")) {
db = args[i + 1];
i++;
continue;
}
if (s.equals("--host")) {
host = args[i + 1];
i++;
continue;
}
if (s.equals("help")) {
printUsage();
return;
}
if (s.equals("list")) {
GridFS fs = getGridFS();
System.out.printf("%-60s %-10s%n", "Filename", "Length");
DBCursor fileListCursor = fs.getFileList();
try {
while (fileListCursor.hasNext()) {
DBObject o = fileListCursor.next();
System.out.printf("%-60s %-10d%n", o.get("filename"), ((Number) o.get("length")).longValue());
}
} finally {
fileListCursor.close();
}
return;
}
if (s.equals("get")) {
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSDBFile f = fs.findOne(fn);
if (f == null) {
System.err.println("can't find file: " + fn);
return;
}
f.writeTo(f.getFilename());
return;
}
if (s.equals("put")) {
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSInputFile f = fs.createFile(new File(fn));
f.save();
f.validate();
return;
}
if (s.equals("md5")) {
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSDBFile f = fs.findOne(fn);
if (f == null) {
System.err.println("can't find file: " + fn);
return;
}
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
int read = 0;
DigestInputStream is = new DigestInputStream(f.getInputStream(), md5);
try {
while (is.read() >= 0) {
read++;
int r = is.read(new byte[17]);
if (r < 0) {
break;
}
read += r;
}
} finally {
is.close();
}
byte[] digest = md5.digest();
System.out.println("length: " + read + " md5: " + Util.toHex(digest));
return;
}
System.err.println("unknown option: " + s);
return;
}
}
use of java.security.DigestInputStream in project hadoop by apache.
the class TestAliyunOSSFileSystemStore method writeRenameReadCompare.
protected void writeRenameReadCompare(Path path, long len) throws IOException, NoSuchAlgorithmException {
// If len > fs.oss.multipart.upload.threshold,
// we'll use a multipart upload copy
MessageDigest digest = MessageDigest.getInstance("MD5");
OutputStream out = new BufferedOutputStream(new DigestOutputStream(fs.create(path, false), digest));
for (long i = 0; i < len; i++) {
out.write('Q');
}
out.flush();
out.close();
assertTrue("Exists", fs.exists(path));
Path copyPath = path.suffix(".copy");
fs.rename(path, copyPath);
assertTrue("Copy exists", fs.exists(copyPath));
// Download file from Aliyun OSS and compare the digest against the original
MessageDigest digest2 = MessageDigest.getInstance("MD5");
InputStream in = new BufferedInputStream(new DigestInputStream(fs.open(copyPath), digest2));
long copyLen = 0;
while (in.read() != -1) {
copyLen++;
}
in.close();
assertEquals("Copy length matches original", len, copyLen);
assertArrayEquals("Digests match", digest.digest(), digest2.digest());
}
use of java.security.DigestInputStream in project hadoop by apache.
the class ITestJets3tNativeFileSystemStore method writeRenameReadCompare.
protected void writeRenameReadCompare(Path path, long len) throws IOException, NoSuchAlgorithmException {
// If len > fs.s3n.multipart.uploads.block.size,
// we'll use a multipart upload copy
MessageDigest digest = MessageDigest.getInstance("MD5");
OutputStream out = new BufferedOutputStream(new DigestOutputStream(fs.create(path, false), digest));
for (long i = 0; i < len; i++) {
out.write('Q');
}
out.flush();
out.close();
assertTrue("Exists", fs.exists(path));
// Depending on if this file is over 5 GB or not,
// rename will cause a multipart upload copy
Path copyPath = path.suffix(".copy");
fs.rename(path, copyPath);
assertTrue("Copy exists", fs.exists(copyPath));
// Download file from S3 and compare the digest against the original
MessageDigest digest2 = MessageDigest.getInstance("MD5");
InputStream in = new BufferedInputStream(new DigestInputStream(fs.open(copyPath), digest2));
long copyLen = 0;
while (in.read() != -1) {
copyLen++;
}
in.close();
assertEquals("Copy length matches original", len, copyLen);
assertArrayEquals("Digests match", digest.digest(), digest2.digest());
}
Aggregations