Search in sources :

Example 76 with DigestOutputStream

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

the class HashContent method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final String algorithm = context.getProperty(HASH_ALGORITHM).getValue();
    final MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        logger.error("Failed to process {} due to {}; routing to failure", new Object[] { flowFile, e });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final AtomicReference<String> hashValueHolder = new AtomicReference<>(null);
    try {
        session.read(flowFile, new InputStreamCallback() {

            @Override
            public void process(final InputStream in) throws IOException {
                try (final DigestOutputStream digestOut = new DigestOutputStream(new NullOutputStream(), digest)) {
                    StreamUtils.copy(in, digestOut);
                    final byte[] hash = digest.digest();
                    final StringBuilder strb = new StringBuilder(hash.length * 2);
                    for (int i = 0; i < hash.length; i++) {
                        strb.append(Integer.toHexString((hash[i] & 0xFF) | 0x100).substring(1, 3));
                    }
                    hashValueHolder.set(strb.toString());
                }
            }
        });
        final String attributeName = context.getProperty(ATTRIBUTE_NAME).getValue();
        flowFile = session.putAttribute(flowFile, attributeName, hashValueHolder.get());
        logger.info("Successfully added attribute '{}' to {} with a value of {}; routing to success", new Object[] { attributeName, flowFile, hashValueHolder.get() });
        session.getProvenanceReporter().modifyAttributes(flowFile);
        session.transfer(flowFile, REL_SUCCESS);
    } catch (final ProcessException e) {
        logger.error("Failed to process {} due to {}; routing to failure", new Object[] { flowFile, e });
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) DigestOutputStream(java.security.DigestOutputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.nifi.stream.io.NullOutputStream)

Example 77 with DigestOutputStream

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

the class PwManagerOutputTest method testChecksum.

public void testChecksum() throws NoSuchAlgorithmException, IOException, PwDbOutputException {
    // FileOutputStream fos = new FileOutputStream("/dev/null");
    NullOutputStream nos = new NullOutputStream();
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    DigestOutputStream dos = new DigestOutputStream(nos, md);
    PwDbV3Output pos = new PwDbV3OutputDebug(mPM, dos, true);
    pos.outputPlanGroupAndEntries(dos);
    dos.close();
    byte[] digest = md.digest();
    assertTrue("No output", digest.length > 0);
    assertArrayEquals("Hash of groups and entries failed.", mPM.dbHeader.contentsHash, digest);
}
Also used : PwDbV3Output(com.keepassdroid.database.save.PwDbV3Output) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) PwDbV3OutputDebug(com.keepassdroid.database.save.PwDbV3OutputDebug) NullOutputStream(com.keepassdroid.stream.NullOutputStream)

Example 78 with DigestOutputStream

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

the class PwDatabase method makeFinalKey.

public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {
    // Write checksum Checksum
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("SHA-256 not implemented here.");
    }
    NullOutputStream nos = new NullOutputStream();
    DigestOutputStream dos = new DigestOutputStream(nos, md);
    byte[] transformedMasterKey = transformMasterKey(masterSeed2, masterKey, numRounds);
    dos.write(masterSeed);
    dos.write(transformedMasterKey);
    finalKey = md.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 79 with DigestOutputStream

use of java.security.DigestOutputStream in project Conversations by siacs.

the class FileBackend method getStoredPepAvatar.

public Avatar getStoredPepAvatar(String hash) {
    if (hash == null) {
        return null;
    }
    Avatar avatar = new Avatar();
    final File file = getAvatarFile(hash);
    FileInputStream is = null;
    try {
        avatar.size = file.length();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        is = new FileInputStream(file);
        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.flush();
        os.close();
        avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
        avatar.image = new String(mByteArrayOutputStream.toByteArray());
        avatar.height = options.outHeight;
        avatar.width = options.outWidth;
        avatar.type = options.outMimeType;
        return avatar;
    } catch (NoSuchAlgorithmException | IOException e) {
        return null;
    } finally {
        close(is);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) Avatar(eu.siacs.conversations.xmpp.pep.Avatar) FileInputStream(java.io.FileInputStream) Paint(android.graphics.Paint) DigestOutputStream(java.security.DigestOutputStream) BitmapFactory(android.graphics.BitmapFactory) MessageDigest(java.security.MessageDigest) File(java.io.File) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 80 with DigestOutputStream

use of java.security.DigestOutputStream in project atlas by alibaba.

the class LocalSignedJarBuilder method writeSignatureFile.

/**
 * Writes a .SF file with a digest to the manifest.
 */
private void writeSignatureFile(OutputStream out) throws IOException, GeneralSecurityException {
    Manifest sf = new Manifest();
    Attributes main = sf.getMainAttributes();
    main.putValue("Signature-Version", "1.0");
    main.putValue("Created-By", "1.0 (Android)");
    MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, SdkConstants.UTF_8);
    // Digest of the entire manifest
    mManifest.write(print);
    print.flush();
    main.putValue(DIGEST_MANIFEST_ATTR, new String(Base64.encode(md.digest()), "ASCII"));
    Map<String, Attributes> entries = mManifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();
        Attributes sfAttr = new Attributes();
        sfAttr.putValue(DIGEST_ATTR, new String(Base64.encode(md.digest()), "ASCII"));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }
    CountOutputStream cout = new CountOutputStream(out);
    sf.write(cout);
    // As a workaround, add an extra CRLF in this case.
    if ((cout.size() % 1024) == 0) {
        cout.write('\r');
        cout.write('\n');
    }
}
Also used : PrintStream(java.io.PrintStream) Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) Map(java.util.Map)

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