Search in sources :

Example 41 with DigestOutputStream

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

the class FileDataStore method addRecord.

/**
 * Creates a new data record.
 * The stream is first consumed and the contents are saved in a temporary file
 * and the {@link #DIGEST} message digest of the stream is calculated. If a
 * record with the same {@link #DIGEST} digest (and length) is found then it is
 * returned. Otherwise the temporary file is moved in place to become
 * the new data record that gets returned.
 *
 * @param input binary stream
 * @return data record that contains the given stream
 * @throws DataStoreException if the record could not be created
 */
public DataRecord addRecord(InputStream input) throws DataStoreException {
    File temporary = null;
    try {
        temporary = newTemporaryFile();
        DataIdentifier tempId = new DataIdentifier(temporary.getName());
        usesIdentifier(tempId);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        long length = 0;
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest);
        try {
            length = IOUtils.copyLarge(input, output);
        } finally {
            output.close();
        }
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        File file;
        synchronized (this) {
            // Check if the same record already exists, or
            // move the temporary file in place if needed
            usesIdentifier(identifier);
            file = getFile(identifier);
            if (!file.exists()) {
                File parent = file.getParentFile();
                parent.mkdirs();
                if (temporary.renameTo(file)) {
                    // no longer need to delete the temporary file
                    temporary = null;
                } else {
                    throw new IOException("Can not rename " + temporary.getAbsolutePath() + " to " + file.getAbsolutePath() + " (media read only?)");
                }
            } else {
                long now = System.currentTimeMillis();
                if (getLastModified(file) < now + ACCESS_TIME_RESOLUTION) {
                    setLastModified(file, now + ACCESS_TIME_RESOLUTION);
                }
            }
            if (file.length() != length) {
                // but better safe than sorry...
                if (!file.isFile()) {
                    throw new IOException("Not a file: " + file);
                }
                throw new IOException(DIGEST + " collision: " + file);
            }
        }
        // this will also make sure that
        // tempId is not garbage collected until here
        inUse.remove(tempId);
        return new FileDataRecord(this, identifier, file);
    } catch (NoSuchAlgorithmException e) {
        throw new DataStoreException(DIGEST + " not available", e);
    } catch (IOException e) {
        throw new DataStoreException("Could not add record", e);
    } finally {
        if (temporary != null) {
            temporary.delete();
        }
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 42 with DigestOutputStream

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

the class CachingDataStore method addRecord.

/**
 * Creates a new data record in {@link Backend}. The stream is first
 * consumed and the contents are saved in a temporary file and the {@link #DIGEST}
 * message digest of the stream is calculated. If a record with the same
 * {@link #DIGEST} digest (and length) is found then it is returned. Otherwise new
 * record is created in {@link Backend} and the temporary file is moved in
 * place to {@link LocalCache}.
 *
 * @param input
 *            binary stream
 * @return {@link CachingDataRecord}
 * @throws DataStoreException
 *             if the record could not be created.
 */
@Override
public DataRecord addRecord(InputStream input) throws DataStoreException {
    File temporary = null;
    long startTime = System.currentTimeMillis();
    long length = 0;
    try {
        temporary = newTemporaryFile();
        DataIdentifier tempId = new DataIdentifier(temporary.getName());
        usesIdentifier(tempId);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest);
        try {
            length = IOUtils.copyLarge(input, output);
        } finally {
            output.close();
        }
        long currTime = System.currentTimeMillis();
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        LOG.debug("Digest of [{}], length =[{}] took [{}]ms ", new Object[] { identifier, length, (currTime - startTime) });
        String fileName = getFileName(identifier);
        AsyncUploadCacheResult result = null;
        synchronized (this) {
            usesIdentifier(identifier);
            // check if async upload is already in progress
            if (!asyncWriteCache.hasEntry(fileName, true)) {
                result = cache.store(fileName, temporary, true);
            }
        }
        LOG.debug("storing  [{}] in localCache took [{}] ms", identifier, (System.currentTimeMillis() - currTime));
        if (result != null) {
            if (result.canAsyncUpload()) {
                backend.writeAsync(identifier, result.getFile(), this);
            } else {
                backend.write(identifier, result.getFile());
            }
        }
        // this will also make sure that
        // tempId is not garbage collected until here
        inUse.remove(tempId);
        LOG.debug("addRecord [{}] of length [{}] took [{}]ms.", new Object[] { identifier, length, (System.currentTimeMillis() - startTime) });
        return new CachingDataRecord(this, identifier);
    } catch (NoSuchAlgorithmException e) {
        throw new DataStoreException(DIGEST + " not available", e);
    } catch (IOException e) {
        throw new DataStoreException("Could not add record", e);
    } finally {
        if (temporary != null) {
            // try to delete - but it's not a big deal if we can't
            temporary.delete();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 43 with DigestOutputStream

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

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 (OutOfMemoryError e) {
        Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
        return null;
    } 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(eu.siacs.conversations.xmpp.pep.Avatar) FileWriterException(eu.siacs.conversations.utils.FileWriterException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 44 with DigestOutputStream

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

the class FileBackend method save.

public boolean save(final Avatar avatar) {
    File file;
    if (isAvatarCached(avatar)) {
        file = getAvatarFile(avatar.getFilename());
        avatar.size = file.length();
    } else {
        file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
        if (file.getParentFile().mkdirs()) {
            Log.d(Config.LOGTAG, "created cache directory");
        }
        OutputStream os = null;
        try {
            if (!file.createNewFile()) {
                Log.d(Config.LOGTAG, "unable to create temporary file " + file.getAbsolutePath());
            }
            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)) {
                final File outputFile = getAvatarFile(avatar.getFilename());
                if (outputFile.getParentFile().mkdirs()) {
                    Log.d(Config.LOGTAG, "created avatar directory");
                }
                final File avatarFile = getAvatarFile(avatar.getFilename());
                if (!file.renameTo(avatarFile)) {
                    Log.d(Config.LOGTAG, "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
                    return false;
                }
            } else {
                Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
                if (!file.delete()) {
                    Log.d(Config.LOGTAG, "unable to delete temporary file");
                }
                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) File(java.io.File) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 45 with DigestOutputStream

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

the class DigestOutputStreamTest method testWriteint05.

/**
 * Test #5 for <code>write(int)</code> method<br>
 * Test #2 for <code>on(boolean)</code> method<br>
 *
 * Assertion: broken <code>DigestOutputStream</code>instance:
 * associated <code>MessageDigest</code> not set.
 * <code>write(int)</code> must work when digest
 * functionality is off
 */
public final void testWriteint05() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
    DigestOutputStream dos = new DigestOutputStream(bos, null);
    // set digest functionality to off
    dos.on(false);
    // the following must pass without any exception
    for (int i = 0; i < MY_MESSAGE_LEN; i++) {
        dos.write(myMessage[i]);
    }
    // check that bytes have been written correctly
    assertTrue(Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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