Search in sources :

Example 1 with DigestInputStream

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;
}
Also used : DigestInputStream(java.security.DigestInputStream) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 2 with DigestInputStream

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...");
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) MessageDigest(java.security.MessageDigest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 3 with DigestInputStream

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;
    }
}
Also used : DBCursor(com.mongodb.DBCursor) DigestInputStream(java.security.DigestInputStream) MessageDigest(java.security.MessageDigest) DBObject(com.mongodb.DBObject) File(java.io.File)

Example 4 with DigestInputStream

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());
}
Also used : Path(org.apache.hadoop.fs.Path) DigestInputStream(java.security.DigestInputStream) BufferedInputStream(java.io.BufferedInputStream) DigestOutputStream(java.security.DigestOutputStream) BufferedInputStream(java.io.BufferedInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with DigestInputStream

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());
}
Also used : Path(org.apache.hadoop.fs.Path) DigestInputStream(java.security.DigestInputStream) BufferedInputStream(java.io.BufferedInputStream) DigestOutputStream(java.security.DigestOutputStream) BufferedInputStream(java.io.BufferedInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

DigestInputStream (java.security.DigestInputStream)161 MessageDigest (java.security.MessageDigest)124 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 IOException (java.io.IOException)62 InputStream (java.io.InputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)38 FileInputStream (java.io.FileInputStream)34 File (java.io.File)19 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileOutputStream (java.io.FileOutputStream)8 URL (java.net.URL)7 OutputStream (java.io.OutputStream)6 BigInteger (java.math.BigInteger)5 DigestOutputStream (java.security.DigestOutputStream)5 HashMap (java.util.HashMap)5 FileNotFoundException (java.io.FileNotFoundException)4 Formatter (java.util.Formatter)4 ByteUtil (com.zimbra.common.util.ByteUtil)3 Path (java.nio.file.Path)3