Search in sources :

Example 91 with DigestOutputStream

use of java.security.DigestOutputStream in project j2objc by google.

the class DigestOutputStreamTest method test_getMessageDigest.

/**
 * java.security.DigestOutputStream#getMessageDigest()
 */
public void test_getMessageDigest() {
    MessageDigest digest = new MyMessageDigest1();
    OutputStream out = new MyOutputStream();
    // non-null parameter
    DigestOutputStream dos = new DigestOutputStream(out, digest);
    assertSame(digest, dos.getMessageDigest());
    // null parameter
    dos = new DigestOutputStream(out, null);
    assertNull("getMessageDigest should have returned null", dos.getMessageDigest());
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Support_OutputStream(tests.support.Support_OutputStream) MyMessageDigest1(org.apache.harmony.security.tests.support.MyMessageDigest1) MessageDigest(java.security.MessageDigest)

Example 92 with DigestOutputStream

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

the class RangerKeyStore method engineStore.

@Override
public void engineStore(OutputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
    if (logger.isDebugEnabled()) {
        logger.debug("==> RangerKeyStore.engineStore()");
    }
    synchronized (deltaEntries) {
        if (keyVaultEnabled) {
            for (Entry<String, Object> entry : deltaEntries.entrySet()) {
                Long creationDate = ((SecretKeyByteEntry) entry.getValue()).date.getTime();
                SecretKeyByteEntry secretSecureKey = (SecretKeyByteEntry) entry.getValue();
                XXRangerKeyStore xxRangerKeyStore = mapObjectToEntity(entry.getKey(), creationDate, secretSecureKey.key, secretSecureKey.cipher_field, secretSecureKey.bit_length, secretSecureKey.description, secretSecureKey.version, secretSecureKey.attributes);
                dbOperationStore(xxRangerKeyStore);
            }
        } else {
            // password is mandatory when storing
            if (password == null) {
                throw new IllegalArgumentException("Ranger Master Key can't be null");
            }
            MessageDigest md = getKeyedMessageDigest(password);
            byte[] digest = md.digest();
            for (Entry<String, Object> entry : deltaEntries.entrySet()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(new DigestOutputStream(baos, md));
                ObjectOutputStream oos = null;
                try {
                    oos = new ObjectOutputStream(dos);
                    oos.writeObject(((SecretKeyEntry) entry.getValue()).sealedKey);
                    dos.write(digest);
                    dos.flush();
                    Long creationDate = ((SecretKeyEntry) entry.getValue()).date.getTime();
                    SecretKeyEntry secretKey = (SecretKeyEntry) entry.getValue();
                    XXRangerKeyStore xxRangerKeyStore = mapObjectToEntity(entry.getKey(), creationDate, baos.toByteArray(), secretKey.cipher_field, secretKey.bit_length, secretKey.description, secretKey.version, secretKey.attributes);
                    dbOperationStore(xxRangerKeyStore);
                } finally {
                    if (oos != null) {
                        oos.close();
                    } else {
                        dos.close();
                    }
                }
            }
        }
        clearDeltaEntires();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) XXRangerKeyStore(org.apache.ranger.entity.XXRangerKeyStore) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) DigestOutputStream(java.security.DigestOutputStream) SealedObject(javax.crypto.SealedObject) MessageDigest(java.security.MessageDigest)

Example 93 with DigestOutputStream

use of java.security.DigestOutputStream in project rskj by rsksmart.

the class MapSnapshotTest method composeByteArray.

private static byte[] composeByteArray(OutputStreamConsumer consumer) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        DigestOutputStream digestOutput = new DigestOutputStream(outputStream, HashUtil.makeMessageDigest());
        DataOutputStream dataOutput = new DataOutputStream(digestOutput);
        consumer.accept(dataOutput);
        byte[] digest = digestOutput.getMessageDigest().digest();
        dataOutput.writeInt(digest.length);
        dataOutput.write(digest);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}
Also used : DigestOutputStream(java.security.DigestOutputStream)

Example 94 with DigestOutputStream

use of java.security.DigestOutputStream in project rskj by rsksmart.

the class MapSnapshotTest method writeSnapshot_WhenWrite_ShouldPopulateOutputStream.

@Test
public void writeSnapshot_WhenWrite_ShouldPopulateOutputStream() throws IOException {
    byte[] key = new byte[] { 0, 1, 2, 3, 4 };
    byte[] value = new byte[] { 5, 6, 7, 8, 9 };
    Map<ByteArrayWrapper, byte[]> outMap = new HashMap<>();
    outMap.put(ByteUtil.wrap(key), value);
    ByteArrayOutputStream expectedOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutput = new DigestOutputStream(expectedOutputStream, HashUtil.makeMessageDigest());
    DataOutputStream dataOutputStream = new DataOutputStream(digestOutput);
    dataOutputStream.writeInt(1);
    dataOutputStream.writeInt(key.length);
    dataOutputStream.write(key);
    dataOutputStream.writeInt(value.length);
    dataOutputStream.write(value);
    byte[] digest = digestOutput.getMessageDigest().digest();
    dataOutputStream.writeInt(digest.length);
    dataOutputStream.write(digest);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    MapSnapshot.Out outSnapshot = new MapSnapshot.Out(outputStream);
    outSnapshot.write(outMap);
    assertArrayEquals(expectedOutputStream.toByteArray(), outputStream.toByteArray());
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper) HashMap(java.util.HashMap) DigestOutputStream(java.security.DigestOutputStream) Test(org.junit.Test)

Example 95 with DigestOutputStream

use of java.security.DigestOutputStream in project gocd by gocd.

the class PluginsZip method create.

public void create() {
    checkFilesAccessibility(bundledPlugins, externalPlugins);
    reset();
    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    try (ZipOutputStream zos = new ZipOutputStream(new DigestOutputStream(new BufferedOutputStream(new FileOutputStream(destZipFile)), md5Digest))) {
        for (GoPluginBundleDescriptor agentPlugins : agentPlugins()) {
            String zipEntryPrefix = "external/";
            if (agentPlugins.isBundledPlugin()) {
                zipEntryPrefix = "bundled/";
            }
            zos.putNextEntry(new ZipEntry(zipEntryPrefix + new File(agentPlugins.bundleJARFileLocation()).getName()));
            Files.copy(new File(agentPlugins.bundleJARFileLocation()).toPath(), zos);
            zos.closeEntry();
        }
    } catch (Exception e) {
        LOG.error("Could not create zip of plugins for agent to download.", e);
    }
    md5DigestOfPlugins = Hex.encodeHexString(md5Digest.digest());
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) DigestOutputStream(java.security.DigestOutputStream) GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) MessageDigest(java.security.MessageDigest) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)107 MessageDigest (java.security.MessageDigest)87 ByteArrayOutputStream (java.io.ByteArrayOutputStream)54 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 Map (java.util.Map)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NullOutputStream (org.apache.commons.io.output.NullOutputStream)7 Base64OutputStream (android.util.Base64OutputStream)6 DigestInputStream (java.security.DigestInputStream)6 Attributes (java.util.jar.Attributes)6 DataOutputStream (java.io.DataOutputStream)5 FileInputStream (java.io.FileInputStream)5