progress for signing binary

This commit is contained in:
Dominik Schürmann 2014-08-11 09:55:24 +02:00
parent 867b89be0a
commit 94b7b1b5d8

View File

@ -37,6 +37,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ProgressScaler;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -131,7 +132,7 @@ public class PgpSignEncrypt {
this.mOutStream = outStream; this.mOutStream = outStream;
} }
public Builder setProgressable(Progressable progressable) { public Builder setProgressable(Progressable progressable) {
mProgressable = progressable; mProgressable = progressable;
return this; return this;
} }
@ -277,7 +278,7 @@ public class PgpSignEncrypt {
} }
try { try {
signingKey = signingKeyRing.getSigningSubKey(); signingKey = signingKeyRing.getSigningSubKey();
} catch(PgpGeneralException e) { } catch (PgpGeneralException e) {
throw new NoSigningKeyException(); throw new NoSigningKeyException();
} }
@ -293,7 +294,7 @@ public class PgpSignEncrypt {
throw new KeyExtractionException(); throw new KeyExtractionException();
} }
} }
updateProgress(R.string.progress_preparing_streams, 5, 100); updateProgress(R.string.progress_preparing_streams, 2, 100);
/* Initialize PGPEncryptedDataGenerator for later usage */ /* Initialize PGPEncryptedDataGenerator for later usage */
PGPEncryptedDataGenerator cPk = null; PGPEncryptedDataGenerator cPk = null;
@ -334,13 +335,13 @@ public class PgpSignEncrypt {
PGPSignatureGenerator signatureGenerator = null; PGPSignatureGenerator signatureGenerator = null;
PGPV3SignatureGenerator signatureV3Generator = null; PGPV3SignatureGenerator signatureV3Generator = null;
if (enableSignature) { if (enableSignature) {
updateProgress(R.string.progress_preparing_signature, 10, 100); updateProgress(R.string.progress_preparing_signature, 4, 100);
try { try {
boolean cleartext = mCleartextInput && mEnableAsciiArmorOutput && !enableEncryption; boolean cleartext = mCleartextInput && mEnableAsciiArmorOutput && !enableEncryption;
if (mSignatureForceV3) { if (mSignatureForceV3) {
signatureV3Generator = signingKey.getV3SignatureGenerator( signatureV3Generator = signingKey.getV3SignatureGenerator(
mSignatureHashAlgorithm,cleartext); mSignatureHashAlgorithm, cleartext);
} else { } else {
signatureGenerator = signingKey.getSignatureGenerator( signatureGenerator = signingKey.getSignatureGenerator(
mSignatureHashAlgorithm, cleartext); mSignatureHashAlgorithm, cleartext);
@ -351,13 +352,15 @@ public class PgpSignEncrypt {
} }
} }
ProgressScaler progressScaler =
new ProgressScaler(mProgressable, 8, 95, 100);
PGPCompressedDataGenerator compressGen = null; PGPCompressedDataGenerator compressGen = null;
OutputStream pOut; OutputStream pOut;
OutputStream encryptionOut = null; OutputStream encryptionOut = null;
BCPGOutputStream bcpgOut; BCPGOutputStream bcpgOut;
if (enableEncryption) { if (enableEncryption) {
/* actual encryption */ /* actual encryption */
updateProgress(R.string.progress_encrypting, 20, 100); updateProgress(R.string.progress_encrypting, 8, 100);
encryptionOut = cPk.open(out, new byte[1 << 16]); encryptionOut = cPk.open(out, new byte[1 << 16]);
@ -398,8 +401,9 @@ public class PgpSignEncrypt {
} }
alreadyWritten += length; alreadyWritten += length;
if (mData.getSize() != 0) { if (mData.getSize() > 0) {
updateProgress((int) (20 + (95 - 20) * alreadyWritten / mData.getSize()), 100); long progress = 100 * alreadyWritten / mData.getSize();
progressScaler.setProgress((int) progress, 100);
} }
} }
@ -407,7 +411,7 @@ public class PgpSignEncrypt {
} else if (enableSignature && mCleartextInput && mEnableAsciiArmorOutput) { } else if (enableSignature && mCleartextInput && mEnableAsciiArmorOutput) {
/* cleartext signature: sign-only of ascii text */ /* cleartext signature: sign-only of ascii text */
updateProgress(R.string.progress_signing, 40, 100); updateProgress(R.string.progress_signing, 8, 100);
// write -----BEGIN PGP SIGNED MESSAGE----- // write -----BEGIN PGP SIGNED MESSAGE-----
armorOut.beginClearText(mSignatureHashAlgorithm); armorOut.beginClearText(mSignatureHashAlgorithm);
@ -422,6 +426,7 @@ public class PgpSignEncrypt {
processLine(reader.readLine(), armorOut, signatureGenerator); processLine(reader.readLine(), armorOut, signatureGenerator);
} }
// TODO: progress: fake annealing?
while (true) { while (true) {
String line = reader.readLine(); String line = reader.readLine();
@ -449,7 +454,7 @@ public class PgpSignEncrypt {
} else if (enableSignature && !mCleartextInput) { } else if (enableSignature && !mCleartextInput) {
/* sign-only binary (files/data stream) */ /* sign-only binary (files/data stream) */
updateProgress(R.string.progress_signing, 40, 100); updateProgress(R.string.progress_signing, 8, 100);
InputStream in = mData.getInputStream(); InputStream in = mData.getInputStream();
@ -471,15 +476,22 @@ public class PgpSignEncrypt {
pOut = literalGen.open(bcpgOut, PGPLiteralData.BINARY, "", new Date(), pOut = literalGen.open(bcpgOut, PGPLiteralData.BINARY, "", new Date(),
new byte[1 << 16]); new byte[1 << 16]);
long alreadyWritten = 0;
int length;
byte[] buffer = new byte[1 << 16]; byte[] buffer = new byte[1 << 16];
int n; while ((length = in.read(buffer)) > 0) {
while ((n = in.read(buffer)) > 0) { pOut.write(buffer, 0, length);
pOut.write(buffer, 0, n);
if (mSignatureForceV3) { if (mSignatureForceV3) {
signatureV3Generator.update(buffer, 0, n); signatureV3Generator.update(buffer, 0, length);
} else { } else {
signatureGenerator.update(buffer, 0, n); signatureGenerator.update(buffer, 0, length);
}
alreadyWritten += length;
if (mData.getSize() > 0) {
long progress = 100 * alreadyWritten / mData.getSize();
progressScaler.setProgress((int) progress, 100);
} }
} }