Search in sources :

Example 6 with MasterCipher

use of org.thoughtcrime.securesms.crypto.MasterCipher in project Signal-Android by WhisperSystems.

the class ConversationActivity method saveDraft.

protected ListenableFuture<Long> saveDraft() {
    final SettableFuture<Long> future = new SettableFuture<>();
    if (this.recipients == null || this.recipients.isEmpty()) {
        future.set(threadId);
        return future;
    }
    final Drafts drafts = getDraftsForCurrentState();
    final long thisThreadId = this.threadId;
    final MasterSecret thisMasterSecret = this.masterSecret.parcelClone();
    final int thisDistributionType = this.distributionType;
    new AsyncTask<Long, Void, Long>() {

        @Override
        protected Long doInBackground(Long... params) {
            ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(ConversationActivity.this);
            DraftDatabase draftDatabase = DatabaseFactory.getDraftDatabase(ConversationActivity.this);
            long threadId = params[0];
            if (drafts.size() > 0) {
                if (threadId == -1)
                    threadId = threadDatabase.getThreadIdFor(getRecipients(), thisDistributionType);
                draftDatabase.insertDrafts(new MasterCipher(thisMasterSecret), threadId, drafts);
                threadDatabase.updateSnippet(threadId, drafts.getSnippet(ConversationActivity.this), drafts.getUriSnippet(ConversationActivity.this), System.currentTimeMillis(), Types.BASE_DRAFT_TYPE, true);
            } else if (threadId > 0) {
                threadDatabase.update(threadId, false);
            }
            return threadId;
        }

        @Override
        protected void onPostExecute(Long result) {
            future.set(result);
        }
    }.execute(thisThreadId);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts)

Example 7 with MasterCipher

use of org.thoughtcrime.securesms.crypto.MasterCipher in project Signal-Android by WhisperSystems.

the class EncryptingJobSerializer method serialize.

@Override
public String serialize(Job job) throws IOException {
    String plaintext = delegate.serialize(job);
    if (job.getEncryptionKeys() != null) {
        MasterSecret masterSecret = ParcelUtil.deserialize(job.getEncryptionKeys().getEncoded(), MasterSecret.CREATOR);
        MasterCipher masterCipher = new MasterCipher(masterSecret);
        return masterCipher.encryptBody(plaintext);
    } else {
        return plaintext;
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher)

Example 8 with MasterCipher

use of org.thoughtcrime.securesms.crypto.MasterCipher in project Signal-Android by WhisperSystems.

the class EncryptingJobSerializer method deserialize.

@Override
public Job deserialize(EncryptionKeys keys, boolean encrypted, String serialized) throws IOException {
    try {
        String plaintext;
        if (encrypted) {
            MasterSecret masterSecret = ParcelUtil.deserialize(keys.getEncoded(), MasterSecret.CREATOR);
            MasterCipher masterCipher = new MasterCipher(masterSecret);
            plaintext = masterCipher.decryptBody(serialized);
        } else {
            plaintext = serialized;
        }
        return delegate.deserialize(keys, encrypted, plaintext);
    } catch (InvalidMessageException e) {
        throw new IOException(e);
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) IOException(java.io.IOException)

Example 9 with MasterCipher

use of org.thoughtcrime.securesms.crypto.MasterCipher in project Signal-Android by WhisperSystems.

the class DirectShareService method onGetChooserTargets.

@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
    List<ChooserTarget> results = new LinkedList<>();
    MasterSecret masterSecret = KeyCachingService.getMasterSecret(this);
    if (masterSecret == null) {
        return results;
    }
    ComponentName componentName = new ComponentName(this, ShareActivity.class);
    ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
    Cursor cursor = threadDatabase.getDirectShareList();
    try {
        ThreadDatabase.Reader reader = threadDatabase.readerFor(cursor, new MasterCipher(masterSecret));
        ThreadRecord record;
        while ((record = reader.getNext()) != null && results.size() < 10) {
            Recipients recipients = RecipientFactory.getRecipientsForIds(this, record.getRecipients().getIds(), false);
            String name = recipients.toShortString();
            Drawable drawable = recipients.getContactPhoto().asDrawable(this, recipients.getColor().toConversationColor(this));
            Bitmap avatar = BitmapUtil.createFromDrawable(drawable, 500, 500);
            Bundle bundle = new Bundle();
            bundle.putLong(ShareActivity.EXTRA_THREAD_ID, record.getThreadId());
            bundle.putLongArray(ShareActivity.EXTRA_RECIPIENT_IDS, recipients.getIds());
            bundle.putInt(ShareActivity.EXTRA_DISTRIBUTION_TYPE, record.getDistributionType());
            results.add(new ChooserTarget(name, Icon.createWithBitmap(avatar), 1.0f, componentName, bundle));
        }
        return results;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) Recipients(org.thoughtcrime.securesms.recipients.Recipients) Bundle(android.os.Bundle) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) Drawable(android.graphics.drawable.Drawable) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) Bitmap(android.graphics.Bitmap) ChooserTarget(android.service.chooser.ChooserTarget) ComponentName(android.content.ComponentName)

Aggregations

MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)9 IOException (java.io.IOException)4 MasterSecret (org.thoughtcrime.securesms.crypto.MasterSecret)4 FileInputStream (java.io.FileInputStream)3 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)3 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 ThreadDatabase (org.thoughtcrime.securesms.database.ThreadDatabase)2 Recipients (org.thoughtcrime.securesms.recipients.Recipients)2 ComponentName (android.content.ComponentName)1 ContentValues (android.content.ContentValues)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 Bitmap (android.graphics.Bitmap)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 ChooserTarget (android.service.chooser.ChooserTarget)1 File (java.io.File)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1