Search in sources :

Example 1 with DigestOutputStream

use of java.security.DigestOutputStream 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 2 with DigestOutputStream

use of java.security.DigestOutputStream 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)

Example 3 with DigestOutputStream

use of java.security.DigestOutputStream in project hadoop by apache.

the class OfflineImageReconstructor method run.

/**
   * Run the OfflineImageReconstructor.
   *
   * @param inputPath         The input path to use.
   * @param outputPath        The output path to use.
   *
   * @throws Exception        On error.
   */
public static void run(String inputPath, String outputPath) throws Exception {
    MessageDigest digester = MD5Hash.getDigester();
    FileOutputStream fout = null;
    File foutHash = new File(outputPath + ".md5");
    // delete any .md5 file that exists
    Files.deleteIfExists(foutHash.toPath());
    CountingOutputStream out = null;
    FileInputStream fis = null;
    InputStreamReader reader = null;
    try {
        Files.deleteIfExists(Paths.get(outputPath));
        fout = new FileOutputStream(outputPath);
        fis = new FileInputStream(inputPath);
        reader = new InputStreamReader(fis, Charset.forName("UTF-8"));
        out = new CountingOutputStream(new DigestOutputStream(new BufferedOutputStream(fout), digester));
        OfflineImageReconstructor oir = new OfflineImageReconstructor(out, reader);
        oir.processXml();
    } finally {
        IOUtils.cleanup(LOG, reader, fis, out, fout);
    }
    // Write the md5 file
    MD5FileUtils.saveMD5File(new File(outputPath), new MD5Hash(digester.digest()));
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) InputStreamReader(java.io.InputStreamReader) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) MD5Hash(org.apache.hadoop.io.MD5Hash) MessageDigest(java.security.MessageDigest) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 4 with DigestOutputStream

use of java.security.DigestOutputStream in project jdk8u_jdk by JetBrains.

the class TestDigestIOStream method testMDChange.

/**
     * Test DigestInputStream and DigestOutputStream digest function when Swap
     * the message digest engines between DigestIn/OutputStream
     *
     * @param algo
     *            Message Digest algorithm
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testMDChange(String algo, int dataLength) throws Exception {
    // Generate the DigestInputStream/DigestOutputStream object
    MessageDigest mdIn = MessageDigest.getInstance(algo);
    MessageDigest mdOut = MessageDigest.getInstance(algo);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, mdIn);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, mdOut)) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[10];
        // use both read() and read(byte[], int, int)
        while ((k = dis.read()) != -1) {
            dos.write(k);
            if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                dos.write(buffer, 0, k);
            }
            // Swap the message digest engines between
            // DigestIn/OutputStream objects
            dis.setMessageDigest(mdOut);
            dos.setMessageDigest(mdIn);
            mdIn = dis.getMessageDigest();
            mdOut = dos.getMessageDigest();
        }
        // Get the output and the "correct" digest values
        byte[] output1 = mdIn.digest();
        byte[] output2 = mdOut.digest();
        byte[] standard = md.digest(data);
        // Compare generated digest values
        return MessageDigest.isEqual(output1, standard) && MessageDigest.isEqual(output2, standard);
    } catch (Exception ex) {
        out.println("testMDChange failed at:" + algo + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest)

Example 5 with DigestOutputStream

use of java.security.DigestOutputStream in project jdk8u_jdk by JetBrains.

the class TestDigestIOStream method testDigestOnOff.

/**
     * Test DigestInputStream and DigestOutputStream digest function when digest
     * set on and off
     *
     * @param algo
     *            Message Digest algorithm
     * @param readModel
     *            which read method used(READ, BUFFER_READ, MIX_READ)
     * @param on
     *            digest switch(on and off)
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on, int dataLength) throws Exception {
    // Generate the DigestInputStream/DigestOutputStream object
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, MessageDigest.getInstance(algo));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, MessageDigest.getInstance(algo));
        ByteArrayOutputStream baOut = new ByteArrayOutputStream()) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[5];
        boolean enDigest = true;
        // Make sure the digest function is on (default)
        dis.on(enDigest);
        dos.on(enDigest);
        switch(readModel) {
            case // use only read()
            READ:
                while ((k = dis.read()) != -1) {
                    if (on) {
                        dos.write(k);
                    } else {
                        dos.write(k);
                        if (enDigest) {
                            baOut.write(k);
                        }
                        enDigest = !enDigest;
                        dos.on(enDigest);
                        dis.on(enDigest);
                    }
                }
                break;
            case // use only read(byte[], int, int)
            BUFFER_READ:
                while ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                    if (on) {
                        dos.write(buffer, 0, k);
                    } else {
                        dos.write(buffer, 0, k);
                        if (enDigest) {
                            baOut.write(buffer, 0, k);
                        }
                        enDigest = !enDigest;
                        dis.on(enDigest);
                        dos.on(enDigest);
                    }
                }
                break;
            case // use both read() and read(byte[], int, int)
            MIX_READ:
                while ((k = dis.read()) != -1) {
                    if (on) {
                        dos.write(k);
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                            dos.write(buffer, 0, k);
                        }
                    } else {
                        dos.write(k);
                        if (enDigest) {
                            baOut.write(k);
                        }
                        enDigest = !enDigest;
                        dis.on(enDigest);
                        dos.on(enDigest);
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                            dos.write(buffer, 0, k);
                            if (enDigest) {
                                baOut.write(buffer, 0, k);
                            }
                            enDigest = !enDigest;
                            dis.on(enDigest);
                            dos.on(enDigest);
                        }
                    }
                }
                break;
            default:
                out.println("ERROR: Invalid read/write combination choice!");
                return false;
        }
        // Get the output and the "correct" digest values
        byte[] output1 = dis.getMessageDigest().digest();
        byte[] output2 = dos.getMessageDigest().digest();
        byte[] standard;
        if (on) {
            standard = md.digest(data);
        } else {
            byte[] dataDigested = baOut.toByteArray();
            standard = md.digest(dataDigested);
        }
        // Compare the output byte array value to the input data
        if (!MessageDigest.isEqual(data, baos.toByteArray())) {
            out.println("ERROR of " + readModel + ": output and input data unexpectedly changed");
            return false;
        }
        // Compare generated digest values
        if (!MessageDigest.isEqual(output1, standard) || !MessageDigest.isEqual(output2, standard)) {
            out.println("ERROR" + readModel + ": generated digest data unexpectedly changed");
            return false;
        }
        return true;
    } catch (Exception ex) {
        out.println("testDigestOnOff failed at:" + algo + "/" + readModel + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)106 MessageDigest (java.security.MessageDigest)86 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)44 IOException (java.io.IOException)41 OutputStream (java.io.OutputStream)26 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)13 Support_OutputStream (tests.support.Support_OutputStream)9 NullOutputStream (com.keepassdroid.stream.NullOutputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 NullOutputStream (org.apache.commons.io.output.NullOutputStream)7 Base64OutputStream (android.util.Base64OutputStream)6 DigestInputStream (java.security.DigestInputStream)6 DataOutputStream (java.io.DataOutputStream)5 FileInputStream (java.io.FileInputStream)5 Attributes (java.util.jar.Attributes)5