From eb636fce47752ccc8d0da1d5cb2c2478ccd86368 Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Sun, 16 May 2010 14:20:17 +0000 Subject: [PATCH] default message/file compression settings added, also a setting for file decryption to change the default when a file requires it --- res/layout/encrypt.xml | 23 +++++ res/layout/preferences.xml | 52 +++++++++++- res/values/strings.xml | 5 +- src/org/thialfihar/android/apg/Apg.java | 17 ++-- .../thialfihar/android/apg/BaseActivity.java | 23 +++++ src/org/thialfihar/android/apg/Constants.java | 2 + .../android/apg/EncryptActivity.java | 29 ++++++- src/org/thialfihar/android/apg/Id.java | 9 ++ .../android/apg/PreferencesActivity.java | 84 ++++++++++++++++--- 9 files changed, 223 insertions(+), 21 deletions(-) diff --git a/res/layout/encrypt.xml b/res/layout/encrypt.xml index b4b023635..dd51318c2 100644 --- a/res/layout/encrypt.xml +++ b/res/layout/encrypt.xml @@ -108,6 +108,29 @@ + + + + + + + + @@ -123,6 +123,52 @@ + + + + + + + + + + + + + + + + Hash Algorithm Public Key Pass Phrase - Pass Phrase Cache + Pass Phrase Cache + Message Compression + File Compression Select 1 Selected @@ -110,6 +112,7 @@ not valid + None Sign only Encrypt only Sign and Encrypt diff --git a/src/org/thialfihar/android/apg/Apg.java b/src/org/thialfihar/android/apg/Apg.java index 0697618d4..bc21487fa 100644 --- a/src/org/thialfihar/android/apg/Apg.java +++ b/src/org/thialfihar/android/apg/Apg.java @@ -1248,7 +1248,7 @@ public class Apg { long encryptionKeyIds[], long signatureKeyId, String signaturePassPhrase, ProgressDialogUpdater progress, - int symmetricAlgorithm, int hashAlgorithm, + int symmetricAlgorithm, int hashAlgorithm, int compression, String passPhrase) throws IOException, GeneralException, PGPException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException { @@ -1324,9 +1324,14 @@ public class Apg { signatureGenerator.setHashedSubpackets(spGen.generate()); } - PGPCompressedDataGenerator compressGen = - new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZLIB); - BCPGOutputStream bcpgOut = new BCPGOutputStream(compressGen.open(encryptOut)); + PGPCompressedDataGenerator compressGen = null; + BCPGOutputStream bcpgOut = null; + if (compression == Id.choice.compression.none) { + bcpgOut = new BCPGOutputStream(encryptOut); + } else { + compressGen = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB); + bcpgOut = new BCPGOutputStream(compressGen.open(encryptOut)); + } if (signatureKeyId != 0) { signatureGenerator.generateOnePassVersion(false).encode(bcpgOut); } @@ -1357,7 +1362,9 @@ public class Apg { progress.setProgress(R.string.progress_generatingSignature, 95, 100); signatureGenerator.generate().encode(pOut); } - compressGen.close(); + if (compressGen != null) { + compressGen.close(); + } encryptOut.close(); if (armored) { armorOut.close(); diff --git a/src/org/thialfihar/android/apg/BaseActivity.java b/src/org/thialfihar/android/apg/BaseActivity.java index 244c4aee6..64705ba1f 100644 --- a/src/org/thialfihar/android/apg/BaseActivity.java +++ b/src/org/thialfihar/android/apg/BaseActivity.java @@ -20,6 +20,7 @@ import java.io.File; import java.util.Timer; import java.util.TimerTask; +import org.bouncycastle2.bcpg.CompressionAlgorithmTags; import org.bouncycastle2.bcpg.HashAlgorithmTags; import org.bouncycastle2.openpgp.PGPEncryptedData; @@ -414,6 +415,28 @@ public class BaseActivity extends Activity editor.commit(); } + public int getDefaultMessageCompression() { + return mPreferences.getInt(Constants.pref.default_message_compression, + CompressionAlgorithmTags.ZLIB); + } + + public void setDefaultMessageCompression(int value) { + SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(Constants.pref.default_message_compression, value); + editor.commit(); + } + + public int getDefaultFileCompression() { + return mPreferences.getInt(Constants.pref.default_file_compression, + CompressionAlgorithmTags.ZLIB); + } + + public void setDefaultFileCompression(int value) { + SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(Constants.pref.default_file_compression, value); + editor.commit(); + } + public boolean getDefaultAsciiArmour() { return mPreferences.getBoolean(Constants.pref.default_ascii_armour, false); } diff --git a/src/org/thialfihar/android/apg/Constants.java b/src/org/thialfihar/android/apg/Constants.java index a880d3328..f4a6a5066 100644 --- a/src/org/thialfihar/android/apg/Constants.java +++ b/src/org/thialfihar/android/apg/Constants.java @@ -28,6 +28,8 @@ public final class Constants { public static final String default_encryption_algorithm = "defaultEncryptionAlgorithm"; public static final String default_hash_algorithm = "defaultHashAlgorithm"; public static final String default_ascii_armour = "defaultAsciiArmour"; + public static final String default_message_compression = "defaultMessageCompression"; + public static final String default_file_compression = "defaultFileCompression"; public static final String pass_phrase_cache_ttl = "passPhraseCacheTtl"; } } diff --git a/src/org/thialfihar/android/apg/EncryptActivity.java b/src/org/thialfihar/android/apg/EncryptActivity.java index 18273d5d7..073e9dc9c 100644 --- a/src/org/thialfihar/android/apg/EncryptActivity.java +++ b/src/org/thialfihar/android/apg/EncryptActivity.java @@ -37,6 +37,7 @@ import org.bouncycastle2.openpgp.PGPSecretKeyRing; import org.bouncycastle2.util.Strings; import org.openintents.intents.FileManager; import org.thialfihar.android.apg.Apg.GeneralException; +import org.thialfihar.android.apg.utils.Choice; import android.app.Dialog; import android.content.ActivityNotFoundException; @@ -49,11 +50,13 @@ import android.text.ClipboardManager; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AnimationUtils; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; +import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; @@ -87,6 +90,7 @@ public class EncryptActivity extends BaseActivity { private EditText mPassPhrase = null; private EditText mPassPhraseAgain = null; private CheckBox mAsciiArmour = null; + private Spinner mFileCompression = null; private EditText mFilename = null; private CheckBox mDeleteAfter = null; @@ -196,6 +200,26 @@ public class EncryptActivity extends BaseActivity { } }); + mFileCompression = (Spinner) findViewById(R.id.fileCompression); + Choice[] choices = new Choice[] { + new Choice(Id.choice.compression.none, getString(R.string.choice_none)), + new Choice(Id.choice.compression.zip, "ZIP"), + new Choice(Id.choice.compression.bzip2, "BZIP2"), + new Choice(Id.choice.compression.zlib, "ZLIB"), + }; + ArrayAdapter adapter = + new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mFileCompression.setAdapter(adapter); + + int defaultFileCompression = getDefaultFileCompression(); + for (int i = 0; i < choices.length; ++i) { + if (choices[i].getId() == defaultFileCompression) { + mFileCompression.setSelection(i); + break; + } + } + mDeleteAfter = (CheckBox) findViewById(R.id.deleteAfterEncryption); mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour); @@ -492,6 +516,7 @@ public class EncryptActivity extends BaseActivity { long encryptionKeyIds[] = null; long signatureKeyId = 0; boolean signOnly = false; + int compressionId = 0; String passPhrase = null; if (mMode.getCurrentView().getId() == R.id.modeSymmetric) { @@ -519,6 +544,7 @@ public class EncryptActivity extends BaseActivity { File file = new File(mInputFilename); size = file.length(); useAsciiArmour = mAsciiArmour.isChecked(); + compressionId = ((Choice) mFileCompression.getSelectedItem()).getId(); } else { String message = mMessage.getText().toString(); @@ -539,6 +565,7 @@ public class EncryptActivity extends BaseActivity { size = byteData.length; useAsciiArmour = true; + compressionId = getDefaultMessageCompression(); } if (signOnly) { @@ -550,7 +577,7 @@ public class EncryptActivity extends BaseActivity { encryptionKeyIds, signatureKeyId, Apg.getCachedPassPhrase(signatureKeyId), this, getDefaultEncryptionAlgorithm(), getDefaultHashAlgorithm(), - passPhrase); + compressionId, passPhrase); } out.close(); diff --git a/src/org/thialfihar/android/apg/Id.java b/src/org/thialfihar/android/apg/Id.java index d869c810a..47cd0a890 100644 --- a/src/org/thialfihar/android/apg/Id.java +++ b/src/org/thialfihar/android/apg/Id.java @@ -16,6 +16,8 @@ package org.thialfihar.android.apg; +import org.bouncycastle2.bcpg.CompressionAlgorithmTags; + public final class Id { public static final class menu { public static final int export = 0x21070001; @@ -92,6 +94,13 @@ public final class Id { public static final int rsa = 0x21070003; } + public static final class compression { + public static final int none = 0x21070001; + public static final int zlib = CompressionAlgorithmTags.ZLIB; + public static final int bzip2 = CompressionAlgorithmTags.BZIP2; + public static final int zip = CompressionAlgorithmTags.ZIP; + } + public static final class usage { public static final int sign_only = 0x21070001; public static final int encrypt_only = 0x21070002; diff --git a/src/org/thialfihar/android/apg/PreferencesActivity.java b/src/org/thialfihar/android/apg/PreferencesActivity.java index f6bc4c8b4..fae63d63b 100644 --- a/src/org/thialfihar/android/apg/PreferencesActivity.java +++ b/src/org/thialfihar/android/apg/PreferencesActivity.java @@ -30,9 +30,11 @@ import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class PreferencesActivity extends BaseActivity { - private Spinner mPassPhraseCache = null; + private Spinner mPassPhraseCacheTtl = null; private Spinner mEncryptionAlgorithm = null; private Spinner mHashAlgorithm = null; + private Spinner mMessageCompression = null; + private Spinner mFileCompression = null; private CheckBox mAsciiArmour = null; @Override @@ -40,7 +42,7 @@ public class PreferencesActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.preferences); - mPassPhraseCache = (Spinner) findViewById(R.id.passPhraseCache); + mPassPhraseCacheTtl = (Spinner) findViewById(R.id.passPhraseCacheTtl); Choice choices[] = { new Choice(15, getString(R.string.choice_15secs)), @@ -53,20 +55,20 @@ public class PreferencesActivity extends BaseActivity { ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - mPassPhraseCache.setAdapter(adapter); + mPassPhraseCacheTtl.setAdapter(adapter); int passPhraseCache = getPassPhraseCacheTtl(); for (int i = 0; i < choices.length; ++i) { if (choices[i].getId() == passPhraseCache) { - mPassPhraseCache.setSelection(i); + mPassPhraseCacheTtl.setSelection(i); break; } } - mPassPhraseCache.setOnItemSelectedListener(new OnItemSelectedListener() { + mPassPhraseCacheTtl.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView adapter, View view, int index, long id) { - setPassPhraseCacheTtl(((Choice) mPassPhraseCache.getSelectedItem()).getId()); + setPassPhraseCacheTtl(((Choice) mPassPhraseCacheTtl.getSelectedItem()).getId()); } @Override @@ -76,11 +78,6 @@ public class PreferencesActivity extends BaseActivity { }); mEncryptionAlgorithm = (Spinner) findViewById(R.id.encryptionAlgorithm); - mHashAlgorithm = (Spinner) findViewById(R.id.hashAlgorithm); - mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour); - - mAsciiArmour.setChecked(getDefaultAsciiArmour()); - choices = new Choice[] { new Choice(PGPEncryptedData.AES_128, "AES 128"), new Choice(PGPEncryptedData.AES_192, "AES 192"), @@ -116,6 +113,7 @@ public class PreferencesActivity extends BaseActivity { } }); + mHashAlgorithm = (Spinner) findViewById(R.id.hashAlgorithm); choices = new Choice[] { new Choice(HashAlgorithmTags.MD5, "MD5"), new Choice(HashAlgorithmTags.RIPEMD160, "RIPEMD160"), @@ -149,6 +147,70 @@ public class PreferencesActivity extends BaseActivity { } }); + mMessageCompression = (Spinner) findViewById(R.id.messageCompression); + choices = new Choice[] { + new Choice(Id.choice.compression.none, getString(R.string.choice_none)), + new Choice(Id.choice.compression.zip, "ZIP"), + new Choice(Id.choice.compression.bzip2, "BZIP2"), + new Choice(Id.choice.compression.zlib, "ZLIB"), + }; + adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mMessageCompression.setAdapter(adapter); + + int defaultMessageCompression = getDefaultMessageCompression(); + for (int i = 0; i < choices.length; ++i) { + if (choices[i].getId() == defaultMessageCompression) { + mMessageCompression.setSelection(i); + break; + } + } + + mMessageCompression.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapter, View view, int index, long id) { + setDefaultMessageCompression(((Choice) mMessageCompression.getSelectedItem()).getId()); + } + + @Override + public void onNothingSelected(AdapterView adapter) { + // nothing to do + } + }); + + mFileCompression = (Spinner) findViewById(R.id.fileCompression); + choices = new Choice[] { + new Choice(Id.choice.compression.none, getString(R.string.choice_none)), + new Choice(Id.choice.compression.zip, "ZIP"), + new Choice(Id.choice.compression.bzip2, "BZIP2"), + new Choice(Id.choice.compression.zlib, "ZLIB"), + }; + adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mFileCompression.setAdapter(adapter); + + int defaultFileCompression = getDefaultFileCompression(); + for (int i = 0; i < choices.length; ++i) { + if (choices[i].getId() == defaultFileCompression) { + mFileCompression.setSelection(i); + break; + } + } + + mFileCompression.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapter, View view, int index, long id) { + setDefaultFileCompression(((Choice) mFileCompression.getSelectedItem()).getId()); + } + + @Override + public void onNothingSelected(AdapterView adapter) { + // nothing to do + } + }); + + mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour); + mAsciiArmour.setChecked(getDefaultAsciiArmour()); mAsciiArmour.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {