Search in sources :

Example 96 with DigestOutputStream

use of java.security.DigestOutputStream in project Pix-Art-Messenger by kriztan.

the class FileBackend method save.

public boolean save(Avatar avatar) {
    File file;
    if (isAvatarCached(avatar)) {
        file = new File(getAvatarPath(avatar.getFilename()));
        avatar.size = file.length();
    } else {
        String filename = getAvatarPath(avatar.getFilename());
        file = new File(filename + ".tmp");
        file.getParentFile().mkdirs();
        OutputStream os = null;
        try {
            file.createNewFile();
            os = new FileOutputStream(file);
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
            final byte[] bytes = avatar.getImageAsBytes();
            mDigestOutputStream.write(bytes);
            mDigestOutputStream.flush();
            mDigestOutputStream.close();
            String sha1sum = CryptoHelper.bytesToHex(digest.digest());
            if (sha1sum.equals(avatar.sha1sum)) {
                file.renameTo(new File(filename));
            } else {
                Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
                file.delete();
                return false;
            }
            avatar.size = bytes.length;
        } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
            return false;
        } finally {
            close(os);
        }
    }
    return true;
}
Also used : DigestOutputStream(java.security.DigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(android.util.Base64OutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) File(java.io.File)

Example 97 with DigestOutputStream

use of java.security.DigestOutputStream in project Pix-Art-Messenger by kriztan.

the class FileBackend method getPepAvatar.

private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
    try {
        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
        if (!bitmap.compress(format, quality, mDigestOutputStream)) {
            return null;
        }
        mDigestOutputStream.flush();
        mDigestOutputStream.close();
        long chars = mByteArrayOutputStream.size();
        if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
            int q = quality - 2;
            Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
            return getPepAvatar(bitmap, format, q);
        }
        Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
        final Avatar avatar = new Avatar();
        avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
        avatar.image = new String(mByteArrayOutputStream.toByteArray());
        if (format.equals(Bitmap.CompressFormat.WEBP)) {
            avatar.type = "image/webp";
        } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
            avatar.type = "image/jpeg";
        } else if (format.equals(Bitmap.CompressFormat.PNG)) {
            avatar.type = "image/png";
        }
        avatar.width = bitmap.getWidth();
        avatar.height = bitmap.getHeight();
        return avatar;
    } catch (Exception e) {
        return null;
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(android.util.Base64OutputStream) MessageDigest(java.security.MessageDigest) Paint(android.graphics.Paint) Avatar(de.pixart.messenger.xmpp.pep.Avatar) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileWriterException(de.pixart.messenger.utils.FileWriterException) IOException(java.io.IOException)

Example 98 with DigestOutputStream

use of java.security.DigestOutputStream in project keepass2android by PhilippC.

the class PwDatabaseV3 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 99 with DigestOutputStream

use of java.security.DigestOutputStream in project keepass2android by PhilippC.

the class PwDbV3Output method outputHeader.

public PwDbHeader 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);
    // Write checksum Checksum
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    NullOutputStream nos;
    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
    PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, os);
    try {
        pho.output();
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output the header.");
    }
    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)

Example 100 with DigestOutputStream

use of java.security.DigestOutputStream in project ma-core-public by infiniteautomation.

the class JsonSerializableUtility method digestsDiffer.

private boolean digestsDiffer(Object fromValue, Object toValue) throws IOException, JsonException {
    try (DigestOutputStream dos = new DigestOutputStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // no-op, just digesting
        }
    }, MessageDigest.getInstance("MD5"));
        OutputStreamWriter toStreamWriter = new OutputStreamWriter(dos);
        OutputStreamWriter fromStreamWriter = new OutputStreamWriter(dos)) {
        JsonWriter fromWriter = new JsonWriter(Common.JSON_CONTEXT, fromStreamWriter);
        // We need fresh writers to avoid miscellaneous commas or whatnot
        JsonWriter toWriter = new JsonWriter(Common.JSON_CONTEXT, toStreamWriter);
        fromWriter.writeObject(fromValue);
        fromWriter.flush();
        byte[] fromDigest = dos.getMessageDigest().digest();
        fromDigest = Arrays.copyOf(fromDigest, fromDigest.length);
        toWriter.writeObject(toValue);
        toWriter.flush();
        byte[] toDigest = dos.getMessageDigest().digest();
        return !Arrays.equals(fromDigest, toDigest);
    } catch (NoSuchAlgorithmException e) {
        // Required to implement MD5, really shouldn't happen
        throw new ShouldNeverHappenException(e);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonWriter(com.serotonin.json.JsonWriter)

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