Search in sources :

Example 26 with DigestOutputStream

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

the class ResourceDistributionPackageBuilder method createPackageForAdd.

@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
    DistributionPackage distributionPackage;
    try {
        FileBackedMemoryOutputStream outputStream = null;
        DigestOutputStream digestStream = null;
        String digestMessage = null;
        try {
            outputStream = new FileBackedMemoryOutputStream(fileThreshold, memoryUnit, useOffHeapMemory, tempDirectory, "distrpck-create-", "." + getType());
            if (digestAlgorithm != null) {
                digestStream = openDigestOutputStream(outputStream, digestAlgorithm);
                export(resourceResolver, request, digestStream);
            } else {
                export(resourceResolver, request, outputStream);
            }
            outputStream.flush();
            if (digestAlgorithm != null) {
                digestMessage = readDigestMessage(digestStream);
            }
        } finally {
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(digestStream);
        }
        Resource packagesRoot = DistributionPackageUtils.getPackagesRoot(resourceResolver, packagesPath);
        InputStream inputStream = null;
        Resource packageResource = null;
        try {
            inputStream = outputStream.openWrittenDataInputStream();
            packageResource = uploadStream(resourceResolver, packagesRoot, inputStream, outputStream.size());
        } finally {
            IOUtils.closeQuietly(inputStream);
            outputStream.clean();
        }
        distributionPackage = new ResourceDistributionPackage(packageResource, getType(), resourceResolver, digestAlgorithm, digestMessage);
    } catch (IOException e) {
        throw new DistributionException(e);
    }
    return distributionPackage;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) FileBackedMemoryOutputStream(org.apache.sling.distribution.util.impl.FileBackedMemoryOutputStream) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) InputStream(java.io.InputStream) Resource(org.apache.sling.api.resource.Resource) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException)

Example 27 with DigestOutputStream

use of java.security.DigestOutputStream in project bnd by bndtools.

the class Jar method getTimelessDigest.

public byte[] getTimelessDigest() throws Exception {
    check();
    MessageDigest md = MessageDigest.getInstance("SHA1");
    OutputStream dout = new DigestOutputStream(IO.nullStream, md);
    // dout = System.out;
    Manifest m = getManifest();
    if (m != null) {
        Manifest m2 = new Manifest(m);
        Attributes main = m2.getMainAttributes();
        String lastmodified = (String) main.remove(new Attributes.Name(Constants.BND_LASTMODIFIED));
        String version = main.getValue(new Attributes.Name(Constants.BUNDLE_VERSION));
        if (version != null && Verifier.isVersion(version)) {
            Version v = new Version(version);
            main.putValue(Constants.BUNDLE_VERSION, v.getWithoutQualifier().toString());
        }
        writeManifest(m2, dout);
        for (Map.Entry<String, Resource> entry : getResources().entrySet()) {
            String path = entry.getKey();
            if (path.equals(manifestName))
                continue;
            Resource resource = entry.getValue();
            dout.write(path.getBytes(UTF_8));
            resource.write(dout);
        }
    }
    return md.digest();
}
Also used : Version(aQute.bnd.version.Version) DigestOutputStream(java.security.DigestOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Attributes(java.util.jar.Attributes) MessageDigest(java.security.MessageDigest) Manifest(java.util.jar.Manifest) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 28 with DigestOutputStream

use of java.security.DigestOutputStream in project fdroidclient by f-droid.

the class JKS method engineStore.

public void engineStore(OutputStream out, char[] passwd) throws IOException, NoSuchAlgorithmException, CertificateException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(charsToBytes(passwd));
    md.update("Mighty Aphrodite".getBytes("UTF-8"));
    DataOutputStream dout = new DataOutputStream(new DigestOutputStream(out, md));
    dout.writeInt(MAGIC);
    dout.writeInt(2);
    dout.writeInt(aliases.size());
    for (Enumeration e = aliases.elements(); e.hasMoreElements(); ) {
        String alias = (String) e.nextElement();
        if (trustedCerts.containsKey(alias)) {
            dout.writeInt(TRUSTED_CERT);
            dout.writeUTF(alias);
            dout.writeLong(((Date) dates.get(alias)).getTime());
            writeCert(dout, (Certificate) trustedCerts.get(alias));
        } else {
            dout.writeInt(PRIVATE_KEY);
            dout.writeUTF(alias);
            dout.writeLong(((Date) dates.get(alias)).getTime());
            byte[] key = (byte[]) privateKeys.get(alias);
            dout.writeInt(key.length);
            dout.write(key);
            Certificate[] chain = (Certificate[]) certChains.get(alias);
            dout.writeInt(chain.length);
            for (int i = 0; i < chain.length; i++) writeCert(dout, chain[i]);
        }
    }
    byte[] digest = md.digest();
    dout.write(digest);
}
Also used : Enumeration(java.util.Enumeration) DataOutputStream(java.io.DataOutputStream) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) Certificate(java.security.cert.Certificate)

Example 29 with DigestOutputStream

use of java.security.DigestOutputStream in project KeePassDX by Kunzisoft.

the class CryptoUtil method hashGen.

public static byte[] hashGen(String transform, byte[] data, int offset, int count) {
    MessageDigest hash;
    try {
        hash = MessageDigest.getInstance(transform);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    NullOutputStream nos = new NullOutputStream();
    DigestOutputStream dos = new DigestOutputStream(nos, hash);
    try {
        dos.write(data, offset, count);
        dos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return hash.digest();
}
Also used : DigestOutputStream(java.security.DigestOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) NullOutputStream(com.keepassdroid.stream.NullOutputStream)

Example 30 with DigestOutputStream

use of java.security.DigestOutputStream in project KeePassDX by Kunzisoft.

the class PwDbV3Output method outputHeader.

public PwDbHeaderV3 outputHeader(OutputStream os) throws PwDbOutputException {
    // Build header
    PwDbHeaderV3 header = new PwDbHeaderV3();
    header.signature1 = PwDbHeader.PWM_DBSIG_1;
    header.signature2 = PwDbHeaderV3.DBSIG_2;
    header.flags = PwDbHeaderV3.FLAG_SHA2;
    if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal) {
        header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL;
    } else if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish) {
        header.flags |= PwDbHeaderV3.FLAG_TWOFISH;
    } else {
        throw new PwDbOutputException("Unsupported algorithm.");
    }
    header.version = PwDbHeaderV3.DBVER_DW;
    header.numGroups = mPM.getGroups().size();
    header.numEntries = mPM.entries.size();
    header.numKeyEncRounds = mPM.getNumKeyEncRecords();
    setIVs(header);
    // Content checksum
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    // Header checksum
    MessageDigest headerDigest;
    try {
        headerDigest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    NullOutputStream nos;
    nos = new NullOutputStream();
    DigestOutputStream headerDos = new DigestOutputStream(nos, headerDigest);
    // Output header for the purpose of calculating the header checksum
    PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, headerDos);
    try {
        pho.outputStart();
        pho.outputEnd();
        headerDos.flush();
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
    byte[] headerHash = headerDigest.digest();
    headerHashBlock = getHeaderHashBuffer(headerHash);
    // Output database for the purpose of calculating the content checksum
    nos = new NullOutputStream();
    DigestOutputStream dos = new DigestOutputStream(nos, md);
    BufferedOutputStream bos = new BufferedOutputStream(dos);
    try {
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to generate checksum.");
    }
    header.contentsHash = md.digest();
    // Output header for real output, containing content hash
    pho = new PwDbHeaderOutputV3(header, os);
    try {
        pho.outputStart();
        dos.on(false);
        pho.outputContentHash();
        dos.on(true);
        pho.outputEnd();
        dos.flush();
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
    return header;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) DigestOutputStream(java.security.DigestOutputStream) PwDbHeaderV3(com.keepassdroid.database.PwDbHeaderV3) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) BufferedOutputStream(java.io.BufferedOutputStream) NullOutputStream(com.keepassdroid.stream.NullOutputStream)

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