default message/file compression settings added, also a setting for file decryption to change the default when a file requires it

This commit is contained in:
Thialfihar 2010-05-16 14:20:17 +00:00
parent c7f0041751
commit eb636fce47
9 changed files with 223 additions and 21 deletions

View file

@ -108,6 +108,29 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_fileCompression"
android:text="@string/label_fileCompression"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<Spinner
android:id="@+id/fileCompression"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -46,8 +46,8 @@
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/label_passPhraseCache" android:id="@+id/label_passPhraseCacheTtl"
android:text="@string/label_passPhraseCache" android:text="@string/label_passPhraseCacheTtl"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_width="0dip"
@ -56,7 +56,7 @@
android:paddingRight="10dip"/> android:paddingRight="10dip"/>
<Spinner <Spinner
android:id="@+id/passPhraseCache" android:id="@+id/passPhraseCacheTtl"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/> android:layout_gravity="center_vertical"/>
@ -123,6 +123,52 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_messageCompression"
android:text="@string/label_messageCompression"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<Spinner
android:id="@+id/messageCompression"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_fileCompression"
android:text="@string/label_fileCompression"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"/>
<Spinner
android:id="@+id/fileCompression"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -93,7 +93,9 @@
<string name="label_hashAlgorithm">Hash Algorithm</string> <string name="label_hashAlgorithm">Hash Algorithm</string>
<string name="label_asymmetric">Public Key</string> <string name="label_asymmetric">Public Key</string>
<string name="label_symmetric">Pass Phrase</string> <string name="label_symmetric">Pass Phrase</string>
<string name="label_passPhraseCache">Pass Phrase Cache</string> <string name="label_passPhraseCacheTtl">Pass Phrase Cache</string>
<string name="label_messageCompression">Message Compression</string>
<string name="label_fileCompression">File Compression</string>
<string name="noKeysSelected">Select</string> <string name="noKeysSelected">Select</string>
<string name="oneKeySelected">1 Selected</string> <string name="oneKeySelected">1 Selected</string>
@ -110,6 +112,7 @@
<string name="notValid">not valid</string> <string name="notValid">not valid</string>
<!-- choice_lowerCase: capitalized firwst word, no punctuation --> <!-- choice_lowerCase: capitalized firwst word, no punctuation -->
<string name="choice_none">None</string>
<string name="choice_signOnly">Sign only</string> <string name="choice_signOnly">Sign only</string>
<string name="choice_encryptOnly">Encrypt only</string> <string name="choice_encryptOnly">Encrypt only</string>
<string name="choice_signAndEncrypt">Sign and Encrypt</string> <string name="choice_signAndEncrypt">Sign and Encrypt</string>

View file

@ -1248,7 +1248,7 @@ public class Apg {
long encryptionKeyIds[], long signatureKeyId, long encryptionKeyIds[], long signatureKeyId,
String signaturePassPhrase, String signaturePassPhrase,
ProgressDialogUpdater progress, ProgressDialogUpdater progress,
int symmetricAlgorithm, int hashAlgorithm, int symmetricAlgorithm, int hashAlgorithm, int compression,
String passPhrase) String passPhrase)
throws IOException, GeneralException, PGPException, NoSuchProviderException, throws IOException, GeneralException, PGPException, NoSuchProviderException,
NoSuchAlgorithmException, SignatureException { NoSuchAlgorithmException, SignatureException {
@ -1324,9 +1324,14 @@ public class Apg {
signatureGenerator.setHashedSubpackets(spGen.generate()); signatureGenerator.setHashedSubpackets(spGen.generate());
} }
PGPCompressedDataGenerator compressGen = PGPCompressedDataGenerator compressGen = null;
new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZLIB); BCPGOutputStream bcpgOut = null;
BCPGOutputStream bcpgOut = new BCPGOutputStream(compressGen.open(encryptOut)); 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) { if (signatureKeyId != 0) {
signatureGenerator.generateOnePassVersion(false).encode(bcpgOut); signatureGenerator.generateOnePassVersion(false).encode(bcpgOut);
} }
@ -1357,7 +1362,9 @@ public class Apg {
progress.setProgress(R.string.progress_generatingSignature, 95, 100); progress.setProgress(R.string.progress_generatingSignature, 95, 100);
signatureGenerator.generate().encode(pOut); signatureGenerator.generate().encode(pOut);
} }
compressGen.close(); if (compressGen != null) {
compressGen.close();
}
encryptOut.close(); encryptOut.close();
if (armored) { if (armored) {
armorOut.close(); armorOut.close();

View file

@ -20,6 +20,7 @@ import java.io.File;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.bouncycastle2.bcpg.CompressionAlgorithmTags;
import org.bouncycastle2.bcpg.HashAlgorithmTags; import org.bouncycastle2.bcpg.HashAlgorithmTags;
import org.bouncycastle2.openpgp.PGPEncryptedData; import org.bouncycastle2.openpgp.PGPEncryptedData;
@ -414,6 +415,28 @@ public class BaseActivity extends Activity
editor.commit(); 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() { public boolean getDefaultAsciiArmour() {
return mPreferences.getBoolean(Constants.pref.default_ascii_armour, false); return mPreferences.getBoolean(Constants.pref.default_ascii_armour, false);
} }

View file

@ -28,6 +28,8 @@ public final class Constants {
public static final String default_encryption_algorithm = "defaultEncryptionAlgorithm"; public static final String default_encryption_algorithm = "defaultEncryptionAlgorithm";
public static final String default_hash_algorithm = "defaultHashAlgorithm"; public static final String default_hash_algorithm = "defaultHashAlgorithm";
public static final String default_ascii_armour = "defaultAsciiArmour"; 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"; public static final String pass_phrase_cache_ttl = "passPhraseCacheTtl";
} }
} }

View file

@ -37,6 +37,7 @@ import org.bouncycastle2.openpgp.PGPSecretKeyRing;
import org.bouncycastle2.util.Strings; import org.bouncycastle2.util.Strings;
import org.openintents.intents.FileManager; import org.openintents.intents.FileManager;
import org.thialfihar.android.apg.Apg.GeneralException; import org.thialfihar.android.apg.Apg.GeneralException;
import org.thialfihar.android.apg.utils.Choice;
import android.app.Dialog; import android.app.Dialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
@ -49,11 +50,13 @@ import android.text.ClipboardManager;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ViewFlipper; import android.widget.ViewFlipper;
@ -87,6 +90,7 @@ public class EncryptActivity extends BaseActivity {
private EditText mPassPhrase = null; private EditText mPassPhrase = null;
private EditText mPassPhraseAgain = null; private EditText mPassPhraseAgain = null;
private CheckBox mAsciiArmour = null; private CheckBox mAsciiArmour = null;
private Spinner mFileCompression = null;
private EditText mFilename = null; private EditText mFilename = null;
private CheckBox mDeleteAfter = 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<Choice> adapter =
new ArrayAdapter<Choice>(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); mDeleteAfter = (CheckBox) findViewById(R.id.deleteAfterEncryption);
mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour); mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour);
@ -492,6 +516,7 @@ public class EncryptActivity extends BaseActivity {
long encryptionKeyIds[] = null; long encryptionKeyIds[] = null;
long signatureKeyId = 0; long signatureKeyId = 0;
boolean signOnly = false; boolean signOnly = false;
int compressionId = 0;
String passPhrase = null; String passPhrase = null;
if (mMode.getCurrentView().getId() == R.id.modeSymmetric) { if (mMode.getCurrentView().getId() == R.id.modeSymmetric) {
@ -519,6 +544,7 @@ public class EncryptActivity extends BaseActivity {
File file = new File(mInputFilename); File file = new File(mInputFilename);
size = file.length(); size = file.length();
useAsciiArmour = mAsciiArmour.isChecked(); useAsciiArmour = mAsciiArmour.isChecked();
compressionId = ((Choice) mFileCompression.getSelectedItem()).getId();
} else { } else {
String message = mMessage.getText().toString(); String message = mMessage.getText().toString();
@ -539,6 +565,7 @@ public class EncryptActivity extends BaseActivity {
size = byteData.length; size = byteData.length;
useAsciiArmour = true; useAsciiArmour = true;
compressionId = getDefaultMessageCompression();
} }
if (signOnly) { if (signOnly) {
@ -550,7 +577,7 @@ public class EncryptActivity extends BaseActivity {
encryptionKeyIds, signatureKeyId, encryptionKeyIds, signatureKeyId,
Apg.getCachedPassPhrase(signatureKeyId), this, Apg.getCachedPassPhrase(signatureKeyId), this,
getDefaultEncryptionAlgorithm(), getDefaultHashAlgorithm(), getDefaultEncryptionAlgorithm(), getDefaultHashAlgorithm(),
passPhrase); compressionId, passPhrase);
} }
out.close(); out.close();

View file

@ -16,6 +16,8 @@
package org.thialfihar.android.apg; package org.thialfihar.android.apg;
import org.bouncycastle2.bcpg.CompressionAlgorithmTags;
public final class Id { public final class Id {
public static final class menu { public static final class menu {
public static final int export = 0x21070001; public static final int export = 0x21070001;
@ -92,6 +94,13 @@ public final class Id {
public static final int rsa = 0x21070003; 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 class usage {
public static final int sign_only = 0x21070001; public static final int sign_only = 0x21070001;
public static final int encrypt_only = 0x21070002; public static final int encrypt_only = 0x21070002;

View file

@ -30,9 +30,11 @@ import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView.OnItemSelectedListener;
public class PreferencesActivity extends BaseActivity { public class PreferencesActivity extends BaseActivity {
private Spinner mPassPhraseCache = null; private Spinner mPassPhraseCacheTtl = null;
private Spinner mEncryptionAlgorithm = null; private Spinner mEncryptionAlgorithm = null;
private Spinner mHashAlgorithm = null; private Spinner mHashAlgorithm = null;
private Spinner mMessageCompression = null;
private Spinner mFileCompression = null;
private CheckBox mAsciiArmour = null; private CheckBox mAsciiArmour = null;
@Override @Override
@ -40,7 +42,7 @@ public class PreferencesActivity extends BaseActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.preferences); setContentView(R.layout.preferences);
mPassPhraseCache = (Spinner) findViewById(R.id.passPhraseCache); mPassPhraseCacheTtl = (Spinner) findViewById(R.id.passPhraseCacheTtl);
Choice choices[] = { Choice choices[] = {
new Choice(15, getString(R.string.choice_15secs)), new Choice(15, getString(R.string.choice_15secs)),
@ -53,20 +55,20 @@ public class PreferencesActivity extends BaseActivity {
ArrayAdapter<Choice> adapter = ArrayAdapter<Choice> adapter =
new ArrayAdapter<Choice>(this, android.R.layout.simple_spinner_item, choices); new ArrayAdapter<Choice>(this, android.R.layout.simple_spinner_item, choices);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mPassPhraseCache.setAdapter(adapter); mPassPhraseCacheTtl.setAdapter(adapter);
int passPhraseCache = getPassPhraseCacheTtl(); int passPhraseCache = getPassPhraseCacheTtl();
for (int i = 0; i < choices.length; ++i) { for (int i = 0; i < choices.length; ++i) {
if (choices[i].getId() == passPhraseCache) { if (choices[i].getId() == passPhraseCache) {
mPassPhraseCache.setSelection(i); mPassPhraseCacheTtl.setSelection(i);
break; break;
} }
} }
mPassPhraseCache.setOnItemSelectedListener(new OnItemSelectedListener() { mPassPhraseCacheTtl.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> adapter, View view, int index, long id) { public void onItemSelected(AdapterView<?> adapter, View view, int index, long id) {
setPassPhraseCacheTtl(((Choice) mPassPhraseCache.getSelectedItem()).getId()); setPassPhraseCacheTtl(((Choice) mPassPhraseCacheTtl.getSelectedItem()).getId());
} }
@Override @Override
@ -76,11 +78,6 @@ public class PreferencesActivity extends BaseActivity {
}); });
mEncryptionAlgorithm = (Spinner) findViewById(R.id.encryptionAlgorithm); mEncryptionAlgorithm = (Spinner) findViewById(R.id.encryptionAlgorithm);
mHashAlgorithm = (Spinner) findViewById(R.id.hashAlgorithm);
mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour);
mAsciiArmour.setChecked(getDefaultAsciiArmour());
choices = new Choice[] { choices = new Choice[] {
new Choice(PGPEncryptedData.AES_128, "AES 128"), new Choice(PGPEncryptedData.AES_128, "AES 128"),
new Choice(PGPEncryptedData.AES_192, "AES 192"), 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[] { choices = new Choice[] {
new Choice(HashAlgorithmTags.MD5, "MD5"), new Choice(HashAlgorithmTags.MD5, "MD5"),
new Choice(HashAlgorithmTags.RIPEMD160, "RIPEMD160"), 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<Choice>(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<Choice>(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() { mAsciiArmour.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {