Search in sources :

Example 21 with DigestOutputStream

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

the class GoDashboardPipelineGroup method etag.

public String etag() {
    try {
        MessageDigest digest = DigestUtils.getSha256Digest();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new DigestOutputStream(new NullOutputStream(), digest));
        outputStreamWriter.write(name);
        outputStreamWriter.write("/");
        outputStreamWriter.write(Integer.toString(permissions.hashCode()));
        outputStreamWriter.write("[");
        for (Map.Entry<String, GoDashboardPipeline> entry : pipelines.entrySet()) {
            long lastUpdatedTimeStamp = entry.getValue().getLastUpdatedTimeStamp();
            outputStreamWriter.write(entry.getKey());
            outputStreamWriter.write(":");
            outputStreamWriter.write(Long.toString(lastUpdatedTimeStamp));
        }
        outputStreamWriter.write("]");
        outputStreamWriter.flush();
        return Hex.encodeHexString(digest.digest());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MessageDigest(java.security.MessageDigest) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 22 with DigestOutputStream

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

the class BridgeSupportTest method getCheckpoints.

private InputStream getCheckpoints(NetworkParameters _networkParameters, List<BtcBlock> checkpoints) {
    try {
        ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
        MessageDigest digest = Sha256Hash.newDigest();
        final DigestOutputStream digestOutputStream = new DigestOutputStream(baOutputStream, digest);
        digestOutputStream.on(false);
        final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
        StoredBlock storedBlock = new StoredBlock(_networkParameters.getGenesisBlock(), _networkParameters.getGenesisBlock().getWork(), 0);
        try {
            dataOutputStream.writeBytes("CHECKPOINTS 1");
            // Number of signatures to read. Do this later.
            dataOutputStream.writeInt(0);
            digestOutputStream.on(true);
            dataOutputStream.writeInt(checkpoints.size());
            ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
            for (BtcBlock block : checkpoints) {
                storedBlock = storedBlock.build(block);
                storedBlock.serializeCompact(buffer);
                dataOutputStream.write(buffer.array());
                buffer.position(0);
            }
        } finally {
            dataOutputStream.close();
            digestOutputStream.close();
            baOutputStream.close();
        }
        return new ByteArrayInputStream(baOutputStream.toByteArray());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 23 with DigestOutputStream

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

the class Util method computeMethodHash.

/**
     * Compute the "method hash" of a remote method.  The method hash
     * is a long containing the first 64 bits of the SHA digest from
     * the UTF encoded string of the method name and descriptor.
     */
public static long computeMethodHash(Method m) {
    long hash = 0;
    ByteArrayOutputStream sink = new ByteArrayOutputStream(127);
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        DataOutputStream out = new DataOutputStream(new DigestOutputStream(sink, md));
        String s = getMethodNameAndDescriptor(m);
        if (serverRefLog.isLoggable(Log.VERBOSE)) {
            serverRefLog.log(Log.VERBOSE, "string used for method hash: \"" + s + "\"");
        }
        out.writeUTF(s);
        // use only the first 64 bits of the digest for the hash
        out.flush();
        byte[] hasharray = md.digest();
        for (int i = 0; i < Math.min(8, hasharray.length); i++) {
            hash += ((long) (hasharray[i] & 0xFF)) << (i * 8);
        }
    } catch (IOException ignore) {
        /* can't happen, but be deterministic anyway. */
        hash = -1;
    } catch (NoSuchAlgorithmException complain) {
        throw new SecurityException(complain.getMessage());
    }
    return hash;
}
Also used : DataOutputStream(java.io.DataOutputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) TCPEndpoint(sun.rmi.transport.tcp.TCPEndpoint)

Example 24 with DigestOutputStream

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

the class JceKeyStore method engineStore.

/**
     * Stores this keystore to the given output stream, and protects its
     * integrity with the given password.
     *
     * @param stream the output stream to which this keystore is written.
     * @param password the password to generate the keystore integrity check
     *
     * @exception IOException if there was an I/O problem with data
     * @exception NoSuchAlgorithmException if the appropriate data integrity
     * algorithm could not be found
     * @exception CertificateException if any of the certificates included in
     * the keystore data could not be stored
     */
public void engineStore(OutputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
    synchronized (entries) {
        // password is mandatory when storing
        if (password == null) {
            throw new IllegalArgumentException("password can't be null");
        }
        // the certificate encoding
        byte[] encoded;
        MessageDigest md = getPreKeyedHash(password);
        DataOutputStream dos = new DataOutputStream(new DigestOutputStream(stream, md));
        // NOTE: don't pass dos to oos at this point or it'll corrupt
        // the keystore!!!
        ObjectOutputStream oos = null;
        try {
            dos.writeInt(JCEKS_MAGIC);
            // always write the latest version
            dos.writeInt(VERSION_2);
            dos.writeInt(entries.size());
            Enumeration<String> e = entries.keys();
            while (e.hasMoreElements()) {
                String alias = e.nextElement();
                Object entry = entries.get(alias);
                if (entry instanceof PrivateKeyEntry) {
                    PrivateKeyEntry pentry = (PrivateKeyEntry) entry;
                    // write PrivateKeyEntry tag
                    dos.writeInt(1);
                    // write the alias
                    dos.writeUTF(alias);
                    // write the (entry creation) date
                    dos.writeLong(pentry.date.getTime());
                    // write the protected private key
                    dos.writeInt(pentry.protectedKey.length);
                    dos.write(pentry.protectedKey);
                    // write the certificate chain
                    int chainLen;
                    if (pentry.chain == null) {
                        chainLen = 0;
                    } else {
                        chainLen = pentry.chain.length;
                    }
                    dos.writeInt(chainLen);
                    for (int i = 0; i < chainLen; i++) {
                        encoded = pentry.chain[i].getEncoded();
                        dos.writeUTF(pentry.chain[i].getType());
                        dos.writeInt(encoded.length);
                        dos.write(encoded);
                    }
                } else if (entry instanceof TrustedCertEntry) {
                    // write TrustedCertEntry tag
                    dos.writeInt(2);
                    // write the alias
                    dos.writeUTF(alias);
                    // write the (entry creation) date
                    dos.writeLong(((TrustedCertEntry) entry).date.getTime());
                    // write the trusted certificate
                    encoded = ((TrustedCertEntry) entry).cert.getEncoded();
                    dos.writeUTF(((TrustedCertEntry) entry).cert.getType());
                    dos.writeInt(encoded.length);
                    dos.write(encoded);
                } else {
                    // write SecretKeyEntry tag
                    dos.writeInt(3);
                    // write the alias
                    dos.writeUTF(alias);
                    // write the (entry creation) date
                    dos.writeLong(((SecretKeyEntry) entry).date.getTime());
                    // write the sealed key
                    oos = new ObjectOutputStream(dos);
                    oos.writeObject(((SecretKeyEntry) entry).sealedKey);
                // NOTE: don't close oos here since we are still
                // using dos!!!
                }
            }
            /*
                 * Write the keyed hash which is used to detect tampering with
                 * the keystore (such as deleting or modifying key or
                 * certificate entries).
                 */
            byte[] digest = md.digest();
            dos.write(digest);
            dos.flush();
        } finally {
            if (oos != null) {
                oos.close();
            } else {
                dos.close();
            }
        }
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) SealedObject(javax.crypto.SealedObject) MessageDigest(java.security.MessageDigest)

Example 25 with DigestOutputStream

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

the class FileDistributionPackageBuilder method readPackageInternal.

@Override
protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
    DistributionPackage distributionPackage;
    final File file;
    DigestOutputStream outputStream = null;
    try {
        String name;
        // stable id
        Map<String, Object> info = new HashMap<String, Object>();
        DistributionPackageUtils.readInfo(stream, info);
        Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
        if (remoteId != null) {
            name = remoteId.toString();
            log.debug("preserving remote id {}", name);
        } else {
            name = "distrpck-read-" + System.nanoTime();
            log.debug("generating a new id {}", name);
        }
        file = File.createTempFile(name, "." + getType(), tempDirectory);
        outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
        IOUtils.copy(stream, outputStream);
        outputStream.flush();
        String digestMessage = readDigestMessage(outputStream);
        distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
    } catch (Exception e) {
        throw new DistributionException(e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
    return distributionPackage;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) HashMap(java.util.HashMap) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) DistributionException(org.apache.sling.distribution.common.DistributionException) File(java.io.File) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException)

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