From b1ead76d3cd8aa575a6f0f75f6b3bdae75539196 Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Sat, 2 May 2009 16:07:32 -0500 Subject: [PATCH] Update bouncycastle library names. --- src/com/android/keepass/Database.java | 2 +- src/com/android/keepass/PasswordActivity.java | 2 +- .../keepass/keepasslib/PwDbHeaderOutput.java | 4 + .../keepass/keepasslib/PwManagerOutput.java | 126 +++++++++++++----- .../crypto/BlockCipher.java | 2 +- .../crypto/BufferedBlockCipher.java | 2 +- .../crypto/CipherParameters.java | 2 +- .../crypto/CryptoException.java | 2 +- .../crypto/DataLengthException.java | 2 +- .../crypto/Digest.java | 2 +- .../crypto/ExtendedDigest.java | 2 +- .../crypto/InvalidCipherTextException.java | 2 +- .../crypto/Mac.java | 2 +- .../crypto/PBEParametersGenerator.java | 2 +- .../crypto/RuntimeCryptoException.java | 2 +- .../crypto/SecureRandom.java | 10 +- .../crypto/digests/GeneralDigest.java | 4 +- .../crypto/digests/LongDigest.java | 4 +- .../crypto/digests/SHA1Digest.java | 4 +- .../crypto/digests/SHA256Digest.java | 6 +- .../crypto/digests/SHA512Digest.java | 2 +- .../crypto/engines/AESEngine.java | 10 +- .../crypto/engines/TwofishEngine.java | 10 +- .../PKCS5S1ParametersGenerator.java | 12 +- .../PKCS5S2ParametersGenerator.java | 16 +-- .../crypto/macs/HMac.java | 12 +- .../crypto/modes/CBCBlockCipher.java | 10 +- .../crypto/paddings/BlockCipherPadding.java | 6 +- .../crypto/paddings/PKCS7Padding.java | 6 +- .../paddings/PaddedBufferedBlockCipher.java | 14 +- .../crypto/params/KeyParameter.java | 4 +- .../crypto/params/ParametersWithIV.java | 4 +- .../crypto/params/ParametersWithRandom.java | 6 +- .../crypto/prng/DigestRandomGenerator.java | 4 +- .../crypto/prng/RandomGenerator.java | 2 +- .../util/Arrays.java | 2 +- .../util/encoders/Encoder.java | 2 +- .../util/encoders/Hex.java | 2 +- .../util/encoders/HexEncoder.java | 2 +- src/org/phoneid/keepassj2me/ImporterV3.java | 25 ++-- src/org/phoneid/keepassj2me/PwManager.java | 6 +- .../keepass/tests/PwManagerOutputTest.java | 37 +++-- .../com/android/keepass/tests/TestData.java | 2 +- 43 files changed, 236 insertions(+), 144 deletions(-) rename src/org/{bouncycastle1 => bouncycastle}/crypto/BlockCipher.java (98%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/BufferedBlockCipher.java (99%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/CipherParameters.java (72%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/CryptoException.java (93%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/DataLengthException.java (95%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/Digest.java (97%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/ExtendedDigest.java (89%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/InvalidCipherTextException.java (93%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/Mac.java (98%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/PBEParametersGenerator.java (99%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/RuntimeCryptoException.java (93%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/SecureRandom.java (92%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/digests/GeneralDigest.java (96%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/digests/LongDigest.java (99%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/digests/SHA1Digest.java (98%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/digests/SHA256Digest.java (97%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/digests/SHA512Digest.java (97%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/engines/AESEngine.java (99%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/engines/TwofishEngine.java (99%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/generators/PKCS5S1ParametersGenerator.java (92%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/generators/PKCS5S2ParametersGenerator.java (90%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/macs/HMac.java (94%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/modes/CBCBlockCipher.java (96%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/paddings/BlockCipherPadding.java (90%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/paddings/PKCS7Padding.java (89%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/paddings/PaddedBufferedBlockCipher.java (95%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/params/KeyParameter.java (83%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/params/ParametersWithIV.java (89%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/params/ParametersWithRandom.java (82%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/prng/DigestRandomGenerator.java (96%) rename src/org/{bouncycastle1 => bouncycastle}/crypto/prng/RandomGenerator.java (95%) rename src/org/{bouncycastle1 => bouncycastle}/util/Arrays.java (97%) rename src/org/{bouncycastle1 => bouncycastle}/util/encoders/Encoder.java (91%) rename src/org/{bouncycastle1 => bouncycastle}/util/encoders/Hex.java (98%) rename src/org/{bouncycastle1 => bouncycastle}/util/encoders/HexEncoder.java (98%) diff --git a/src/com/android/keepass/Database.java b/src/com/android/keepass/Database.java index 55676a162..e6e1f85a0 100644 --- a/src/com/android/keepass/Database.java +++ b/src/com/android/keepass/Database.java @@ -27,7 +27,7 @@ import java.util.HashMap; import java.util.UUID; import java.util.Vector; -import org.bouncycastle1.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.InvalidCipherTextException; import org.phoneid.keepassj2me.ImporterV3; import org.phoneid.keepassj2me.PwEntry; import org.phoneid.keepassj2me.PwGroup; diff --git a/src/com/android/keepass/PasswordActivity.java b/src/com/android/keepass/PasswordActivity.java index d5196821c..cfeadd2b9 100644 --- a/src/com/android/keepass/PasswordActivity.java +++ b/src/com/android/keepass/PasswordActivity.java @@ -23,7 +23,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import org.bouncycastle1.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.InvalidCipherTextException; import android.app.Activity; import android.app.ProgressDialog; diff --git a/src/com/android/keepass/keepasslib/PwDbHeaderOutput.java b/src/com/android/keepass/keepasslib/PwDbHeaderOutput.java index 02c513828..013b909d7 100644 --- a/src/com/android/keepass/keepasslib/PwDbHeaderOutput.java +++ b/src/com/android/keepass/keepasslib/PwDbHeaderOutput.java @@ -48,4 +48,8 @@ public class PwDbHeaderOutput { mOS.write(Types.writeInt(mHeader.numKeyEncRounds)); } + + public void close() throws IOException { + mOS.close(); + } } diff --git a/src/com/android/keepass/keepasslib/PwManagerOutput.java b/src/com/android/keepass/keepasslib/PwManagerOutput.java index 6c6eff56f..bab1201d6 100644 --- a/src/com/android/keepass/keepasslib/PwManagerOutput.java +++ b/src/com/android/keepass/keepasslib/PwManagerOutput.java @@ -19,8 +19,6 @@ */ package com.android.keepass.keepasslib; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.security.DigestOutputStream; @@ -28,6 +26,13 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import org.bouncycastle.crypto.engines.AESEngine; +import org.bouncycastle.crypto.modes.CBCBlockCipher; +import org.bouncycastle.crypto.paddings.PKCS7Padding; +import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.crypto.params.ParametersWithIV; +import org.phoneid.keepassj2me.ImporterV3; import org.phoneid.keepassj2me.PwDbHeader; import org.phoneid.keepassj2me.PwEntry; import org.phoneid.keepassj2me.PwGroup; @@ -51,22 +56,79 @@ public class PwManagerOutput { mDebug = debug; } - public void output() throws IOException, PwManagerOutputException { - - - - - /* - int filePadding = (int)(16 - (fileSize % 16)); // Pad file to 16-byte boundary - if ( filePadding > 0 ) { - byte[] padding = new byte[filePadding]; - fos.write(padding); + public void close() throws PwManagerOutputException { + try { + mOS.close(); + } catch (IOException e) { + throw new PwManagerOutputException("Failed to close stream."); } - */ - } - public void outputHeader(OutputStream os) throws PwManagerOutputException { + public void output() throws PwManagerOutputException, IOException { + + PwDbHeader header = outputHeader(mOS); + + // Write checksum Checksum + MessageDigest md = null; + try { + md = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + assert true; + throw new PwManagerOutputException("SHA-256 not implemented here."); + } + NullOutputStream nos = new NullOutputStream(); + DigestOutputStream dos = new DigestOutputStream(nos, md); + + byte[] transformedMasterKey = ImporterV3.transformMasterKey(header.masterSeed2, mPM.masterKey, header.numKeyEncRounds); + try { + dos.write(header.masterSeed); + dos.write(transformedMasterKey); + dos.close(); + nos.close(); + } catch ( IOException e ) { + throw new PwManagerOutputException("Failed to build final key."); + } + + byte[] finalKey = md.digest(); + // Bouncy Castle implementation + PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); + cipher.init(true, new ParametersWithIV(new KeyParameter(finalKey), header.encryptionIV)); + BufferedBlockCipherOutputStream bbcos = new BufferedBlockCipherOutputStream(mOS, cipher); + outputPlanGroupAndEntries(bbcos); + + bbcos.close(); + /* + try { + bbcos.close(); + } catch (IOException e) { + throw new PwManagerOutputException("Failed to close encryption stream."); + } + */ + + /* Why doesn't java native work? + Cipher cipher; + try { + cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + } catch (Exception e) { + throw new PwManagerOutputException("Algorithm not supported."); + } + + try { + cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES" ), new IvParameterSpec(header.encryptionIV) ); + CipherOutputStream cos = new CipherOutputStream(mOS, cipher); + outputPlanGroupAndEntries(cos); + cos.close(); + } catch (InvalidKeyException e) { + throw new PwManagerOutputException("Invalid key"); + } catch (InvalidAlgorithmParameterException e) { + throw new PwManagerOutputException("Invalid algorithm parameter."); + } catch (IOException e) { + throw new PwManagerOutputException("Failed to output final encrypted part."); + } + */ + } + + public PwDbHeader outputHeader(OutputStream os) throws PwManagerOutputException { // Build header PwDbHeader header = new PwDbHeader(); header.signature1 = PwDbHeader.PWM_DBSIG_1; @@ -113,16 +175,13 @@ public class PwManagerOutput { throw new PwManagerOutputException("SHA-256 not implemented here."); } - FileOutputStream fos; - try { - fos = new FileOutputStream("/dev/null"); - } catch (FileNotFoundException e1) { - throw new PwManagerOutputException("Could not open /dev/null"); - } - DigestOutputStream dos = new DigestOutputStream(fos, md); + NullOutputStream nos; + nos = new NullOutputStream(); + DigestOutputStream dos = new DigestOutputStream(nos, md); try { outputPlanGroupAndEntries(dos); dos.close(); + nos.close(); } catch (IOException e) { throw new PwManagerOutputException("Failed to generate checksum."); } @@ -134,36 +193,41 @@ public class PwManagerOutput { PwDbHeaderOutput pho = new PwDbHeaderOutput(header, os); try { pho.output(); + pho.close(); } catch (IOException e) { throw new PwManagerOutputException("Failed to output the header."); } - + return header; } - public void outputPlanGroupAndEntries(OutputStream os) throws IOException { + public void outputPlanGroupAndEntries(OutputStream os) throws PwManagerOutputException { //long size = 0; // Groups for (int i = 0; i < mPM.groups.size(); i++ ) { PwGroup pg = mPM.groups.get(i); PwGroupOutput pgo = new PwGroupOutput(pg, os); - pgo.output(); - //size += pgo.getLength(); + try { + pgo.output(); + } catch (IOException e) { + throw new PwManagerOutputException("Failed to output a group."); + } } // Entries for (int i = 0; i < mPM.entries.size(); i++ ) { PwEntry pe = mPM.entries.get(i); PwEntryOutput peo = new PwEntryOutput(pe, os); - peo.output(); - //size += peo.getLength(); + try { + peo.output(); + } catch (IOException e) { + throw new PwManagerOutputException("Failed to output an entry."); + } } - - //return size; } - class PwManagerOutputException extends Exception { + public class PwManagerOutputException extends Exception { public PwManagerOutputException(String string) { super(string); diff --git a/src/org/bouncycastle1/crypto/BlockCipher.java b/src/org/bouncycastle/crypto/BlockCipher.java similarity index 98% rename from src/org/bouncycastle1/crypto/BlockCipher.java rename to src/org/bouncycastle/crypto/BlockCipher.java index a2b9085ac..3cfa25a09 100644 --- a/src/org/bouncycastle1/crypto/BlockCipher.java +++ b/src/org/bouncycastle/crypto/BlockCipher.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** diff --git a/src/org/bouncycastle1/crypto/BufferedBlockCipher.java b/src/org/bouncycastle/crypto/BufferedBlockCipher.java similarity index 99% rename from src/org/bouncycastle1/crypto/BufferedBlockCipher.java rename to src/org/bouncycastle/crypto/BufferedBlockCipher.java index 3809f87ba..53d776ac7 100644 --- a/src/org/bouncycastle1/crypto/BufferedBlockCipher.java +++ b/src/org/bouncycastle/crypto/BufferedBlockCipher.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** diff --git a/src/org/bouncycastle1/crypto/CipherParameters.java b/src/org/bouncycastle/crypto/CipherParameters.java similarity index 72% rename from src/org/bouncycastle1/crypto/CipherParameters.java rename to src/org/bouncycastle/crypto/CipherParameters.java index 2583eb66a..5be873047 100644 --- a/src/org/bouncycastle1/crypto/CipherParameters.java +++ b/src/org/bouncycastle/crypto/CipherParameters.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * all parameter classes implement this. diff --git a/src/org/bouncycastle1/crypto/CryptoException.java b/src/org/bouncycastle/crypto/CryptoException.java similarity index 93% rename from src/org/bouncycastle1/crypto/CryptoException.java rename to src/org/bouncycastle/crypto/CryptoException.java index 8183079c6..dc4a8dff4 100644 --- a/src/org/bouncycastle1/crypto/CryptoException.java +++ b/src/org/bouncycastle/crypto/CryptoException.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * the foundation class for the hard exceptions thrown by the crypto packages. diff --git a/src/org/bouncycastle1/crypto/DataLengthException.java b/src/org/bouncycastle/crypto/DataLengthException.java similarity index 95% rename from src/org/bouncycastle1/crypto/DataLengthException.java rename to src/org/bouncycastle/crypto/DataLengthException.java index 2bf11be73..fbf047cf5 100644 --- a/src/org/bouncycastle1/crypto/DataLengthException.java +++ b/src/org/bouncycastle/crypto/DataLengthException.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * this exception is thrown if a buffer that is meant to have output diff --git a/src/org/bouncycastle1/crypto/Digest.java b/src/org/bouncycastle/crypto/Digest.java similarity index 97% rename from src/org/bouncycastle1/crypto/Digest.java rename to src/org/bouncycastle/crypto/Digest.java index 004d2621b..f44fad0d2 100644 --- a/src/org/bouncycastle1/crypto/Digest.java +++ b/src/org/bouncycastle/crypto/Digest.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * interface that a message digest conforms to. diff --git a/src/org/bouncycastle1/crypto/ExtendedDigest.java b/src/org/bouncycastle/crypto/ExtendedDigest.java similarity index 89% rename from src/org/bouncycastle1/crypto/ExtendedDigest.java rename to src/org/bouncycastle/crypto/ExtendedDigest.java index 5b6ae1e54..c5e9e8b0c 100644 --- a/src/org/bouncycastle1/crypto/ExtendedDigest.java +++ b/src/org/bouncycastle/crypto/ExtendedDigest.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; public interface ExtendedDigest extends Digest diff --git a/src/org/bouncycastle1/crypto/InvalidCipherTextException.java b/src/org/bouncycastle/crypto/InvalidCipherTextException.java similarity index 93% rename from src/org/bouncycastle1/crypto/InvalidCipherTextException.java rename to src/org/bouncycastle/crypto/InvalidCipherTextException.java index 996ae183a..59e4b26be 100644 --- a/src/org/bouncycastle1/crypto/InvalidCipherTextException.java +++ b/src/org/bouncycastle/crypto/InvalidCipherTextException.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * this exception is thrown whenever we find something we don't expect in a diff --git a/src/org/bouncycastle1/crypto/Mac.java b/src/org/bouncycastle/crypto/Mac.java similarity index 98% rename from src/org/bouncycastle1/crypto/Mac.java rename to src/org/bouncycastle/crypto/Mac.java index 708b09eaf..8c122c86e 100644 --- a/src/org/bouncycastle1/crypto/Mac.java +++ b/src/org/bouncycastle/crypto/Mac.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** diff --git a/src/org/bouncycastle1/crypto/PBEParametersGenerator.java b/src/org/bouncycastle/crypto/PBEParametersGenerator.java similarity index 99% rename from src/org/bouncycastle1/crypto/PBEParametersGenerator.java rename to src/org/bouncycastle/crypto/PBEParametersGenerator.java index 4cb40b3f9..12000aaa2 100644 --- a/src/org/bouncycastle1/crypto/PBEParametersGenerator.java +++ b/src/org/bouncycastle/crypto/PBEParametersGenerator.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * super class for all Password Based Encryption (PBE) parameter generator classes. diff --git a/src/org/bouncycastle1/crypto/RuntimeCryptoException.java b/src/org/bouncycastle/crypto/RuntimeCryptoException.java similarity index 93% rename from src/org/bouncycastle1/crypto/RuntimeCryptoException.java rename to src/org/bouncycastle/crypto/RuntimeCryptoException.java index e684ffb93..c1572020b 100644 --- a/src/org/bouncycastle1/crypto/RuntimeCryptoException.java +++ b/src/org/bouncycastle/crypto/RuntimeCryptoException.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; /** * the foundation class for the exceptions thrown by the crypto packages. diff --git a/src/org/bouncycastle1/crypto/SecureRandom.java b/src/org/bouncycastle/crypto/SecureRandom.java similarity index 92% rename from src/org/bouncycastle1/crypto/SecureRandom.java rename to src/org/bouncycastle/crypto/SecureRandom.java index 0741ee220..9bbf09a89 100644 --- a/src/org/bouncycastle1/crypto/SecureRandom.java +++ b/src/org/bouncycastle/crypto/SecureRandom.java @@ -1,12 +1,12 @@ //package java.security; -package org.bouncycastle1.crypto; +package org.bouncycastle.crypto; import java.util.Random; -import org.bouncycastle1.crypto.digests.SHA1Digest; -import org.bouncycastle1.crypto.digests.SHA256Digest; -import org.bouncycastle1.crypto.prng.RandomGenerator; -import org.bouncycastle1.crypto.prng.DigestRandomGenerator; +import org.bouncycastle.crypto.digests.SHA1Digest; +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.prng.DigestRandomGenerator; +import org.bouncycastle.crypto.prng.RandomGenerator; /** * An implementation of SecureRandom specifically for the light-weight API, JDK diff --git a/src/org/bouncycastle1/crypto/digests/GeneralDigest.java b/src/org/bouncycastle/crypto/digests/GeneralDigest.java similarity index 96% rename from src/org/bouncycastle1/crypto/digests/GeneralDigest.java rename to src/org/bouncycastle/crypto/digests/GeneralDigest.java index 9b0e5e0bb..f2c996744 100644 --- a/src/org/bouncycastle1/crypto/digests/GeneralDigest.java +++ b/src/org/bouncycastle/crypto/digests/GeneralDigest.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.digests; +package org.bouncycastle.crypto.digests; -import org.bouncycastle1.crypto.ExtendedDigest; +import org.bouncycastle.crypto.ExtendedDigest; /** * base implementation of MD4 family style digest as outlined in diff --git a/src/org/bouncycastle1/crypto/digests/LongDigest.java b/src/org/bouncycastle/crypto/digests/LongDigest.java similarity index 99% rename from src/org/bouncycastle1/crypto/digests/LongDigest.java rename to src/org/bouncycastle/crypto/digests/LongDigest.java index 5c0fa259b..23af6053d 100644 --- a/src/org/bouncycastle1/crypto/digests/LongDigest.java +++ b/src/org/bouncycastle/crypto/digests/LongDigest.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.digests; +package org.bouncycastle.crypto.digests; -import org.bouncycastle1.crypto.ExtendedDigest; +import org.bouncycastle.crypto.ExtendedDigest; /** * Base class for SHA-384 and SHA-512. diff --git a/src/org/bouncycastle1/crypto/digests/SHA1Digest.java b/src/org/bouncycastle/crypto/digests/SHA1Digest.java similarity index 98% rename from src/org/bouncycastle1/crypto/digests/SHA1Digest.java rename to src/org/bouncycastle/crypto/digests/SHA1Digest.java index e4eb42925..a4bf8e20b 100644 --- a/src/org/bouncycastle1/crypto/digests/SHA1Digest.java +++ b/src/org/bouncycastle/crypto/digests/SHA1Digest.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto.digests; +package org.bouncycastle.crypto.digests; /** * implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349. @@ -7,7 +7,7 @@ package org.bouncycastle1.crypto.digests; * is the "endienness" of the word processing! */ public class SHA1Digest - extends org.bouncycastle1.crypto.digests.GeneralDigest + extends org.bouncycastle.crypto.digests.GeneralDigest { private static final int DIGEST_LENGTH = 20; diff --git a/src/org/bouncycastle1/crypto/digests/SHA256Digest.java b/src/org/bouncycastle/crypto/digests/SHA256Digest.java similarity index 97% rename from src/org/bouncycastle1/crypto/digests/SHA256Digest.java rename to src/org/bouncycastle/crypto/digests/SHA256Digest.java index 2e9d7a4c9..3db68ee9c 100644 --- a/src/org/bouncycastle1/crypto/digests/SHA256Digest.java +++ b/src/org/bouncycastle/crypto/digests/SHA256Digest.java @@ -1,7 +1,7 @@ -package org.bouncycastle1.crypto.digests; +package org.bouncycastle.crypto.digests; -import org.bouncycastle1.crypto.digests.GeneralDigest; +import org.bouncycastle.crypto.digests.GeneralDigest; /** @@ -16,7 +16,7 @@ import org.bouncycastle1.crypto.digests.GeneralDigest; * */ public class SHA256Digest - extends org.bouncycastle1.crypto.digests.GeneralDigest + extends org.bouncycastle.crypto.digests.GeneralDigest { private static final int DIGEST_LENGTH = 32; diff --git a/src/org/bouncycastle1/crypto/digests/SHA512Digest.java b/src/org/bouncycastle/crypto/digests/SHA512Digest.java similarity index 97% rename from src/org/bouncycastle1/crypto/digests/SHA512Digest.java rename to src/org/bouncycastle/crypto/digests/SHA512Digest.java index 021f354ef..1f4ae41a9 100644 --- a/src/org/bouncycastle1/crypto/digests/SHA512Digest.java +++ b/src/org/bouncycastle/crypto/digests/SHA512Digest.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto.digests; +package org.bouncycastle.crypto.digests; /** diff --git a/src/org/bouncycastle1/crypto/engines/AESEngine.java b/src/org/bouncycastle/crypto/engines/AESEngine.java similarity index 99% rename from src/org/bouncycastle1/crypto/engines/AESEngine.java rename to src/org/bouncycastle/crypto/engines/AESEngine.java index 88d936f45..9183dad01 100644 --- a/src/org/bouncycastle1/crypto/engines/AESEngine.java +++ b/src/org/bouncycastle/crypto/engines/AESEngine.java @@ -1,9 +1,9 @@ -package org.bouncycastle1.crypto.engines; +package org.bouncycastle.crypto.engines; -import org.bouncycastle1.crypto.BlockCipher; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.DataLengthException; -import org.bouncycastle1.crypto.params.KeyParameter; +import org.bouncycastle.crypto.BlockCipher; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.DataLengthException; +import org.bouncycastle.crypto.params.KeyParameter; /** * an implementation of the AES (Rijndael), from FIPS-197. diff --git a/src/org/bouncycastle1/crypto/engines/TwofishEngine.java b/src/org/bouncycastle/crypto/engines/TwofishEngine.java similarity index 99% rename from src/org/bouncycastle1/crypto/engines/TwofishEngine.java rename to src/org/bouncycastle/crypto/engines/TwofishEngine.java index bfd6f243f..5dc55c203 100644 --- a/src/org/bouncycastle1/crypto/engines/TwofishEngine.java +++ b/src/org/bouncycastle/crypto/engines/TwofishEngine.java @@ -1,9 +1,9 @@ -package org.bouncycastle1.crypto.engines; +package org.bouncycastle.crypto.engines; -import org.bouncycastle1.crypto.BlockCipher; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.DataLengthException; -import org.bouncycastle1.crypto.params.KeyParameter; +import org.bouncycastle.crypto.BlockCipher; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.DataLengthException; +import org.bouncycastle.crypto.params.KeyParameter; /** * A class that provides Twofish encryption operations. diff --git a/src/org/bouncycastle1/crypto/generators/PKCS5S1ParametersGenerator.java b/src/org/bouncycastle/crypto/generators/PKCS5S1ParametersGenerator.java similarity index 92% rename from src/org/bouncycastle1/crypto/generators/PKCS5S1ParametersGenerator.java rename to src/org/bouncycastle/crypto/generators/PKCS5S1ParametersGenerator.java index e7a933103..1c62eccc2 100644 --- a/src/org/bouncycastle1/crypto/generators/PKCS5S1ParametersGenerator.java +++ b/src/org/bouncycastle/crypto/generators/PKCS5S1ParametersGenerator.java @@ -1,10 +1,10 @@ -package org.bouncycastle1.crypto.generators; +package org.bouncycastle.crypto.generators; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.Digest; -import org.bouncycastle1.crypto.PBEParametersGenerator; -import org.bouncycastle1.crypto.params.KeyParameter; -import org.bouncycastle1.crypto.params.ParametersWithIV; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.Digest; +import org.bouncycastle.crypto.PBEParametersGenerator; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.crypto.params.ParametersWithIV; /** * Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 1. diff --git a/src/org/bouncycastle1/crypto/generators/PKCS5S2ParametersGenerator.java b/src/org/bouncycastle/crypto/generators/PKCS5S2ParametersGenerator.java similarity index 90% rename from src/org/bouncycastle1/crypto/generators/PKCS5S2ParametersGenerator.java rename to src/org/bouncycastle/crypto/generators/PKCS5S2ParametersGenerator.java index 834781101..9b4972d5f 100644 --- a/src/org/bouncycastle1/crypto/generators/PKCS5S2ParametersGenerator.java +++ b/src/org/bouncycastle/crypto/generators/PKCS5S2ParametersGenerator.java @@ -1,12 +1,12 @@ -package org.bouncycastle1.crypto.generators; +package org.bouncycastle.crypto.generators; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.Mac; -import org.bouncycastle1.crypto.PBEParametersGenerator; -import org.bouncycastle1.crypto.digests.SHA1Digest; -import org.bouncycastle1.crypto.macs.HMac; -import org.bouncycastle1.crypto.params.KeyParameter; -import org.bouncycastle1.crypto.params.ParametersWithIV; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.Mac; +import org.bouncycastle.crypto.PBEParametersGenerator; +import org.bouncycastle.crypto.digests.SHA1Digest; +import org.bouncycastle.crypto.macs.HMac; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.crypto.params.ParametersWithIV; /** * Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 2. diff --git a/src/org/bouncycastle1/crypto/macs/HMac.java b/src/org/bouncycastle/crypto/macs/HMac.java similarity index 94% rename from src/org/bouncycastle1/crypto/macs/HMac.java rename to src/org/bouncycastle/crypto/macs/HMac.java index d06f8f780..0bd4d39b2 100644 --- a/src/org/bouncycastle1/crypto/macs/HMac.java +++ b/src/org/bouncycastle/crypto/macs/HMac.java @@ -1,12 +1,12 @@ -package org.bouncycastle1.crypto.macs; +package org.bouncycastle.crypto.macs; import java.util.Hashtable; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.Digest; -import org.bouncycastle1.crypto.ExtendedDigest; -import org.bouncycastle1.crypto.Mac; -import org.bouncycastle1.crypto.params.KeyParameter; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.Digest; +import org.bouncycastle.crypto.ExtendedDigest; +import org.bouncycastle.crypto.Mac; +import org.bouncycastle.crypto.params.KeyParameter; /** * HMAC implementation based on RFC2104 diff --git a/src/org/bouncycastle1/crypto/modes/CBCBlockCipher.java b/src/org/bouncycastle/crypto/modes/CBCBlockCipher.java similarity index 96% rename from src/org/bouncycastle1/crypto/modes/CBCBlockCipher.java rename to src/org/bouncycastle/crypto/modes/CBCBlockCipher.java index ddb486e1d..764004572 100644 --- a/src/org/bouncycastle1/crypto/modes/CBCBlockCipher.java +++ b/src/org/bouncycastle/crypto/modes/CBCBlockCipher.java @@ -1,9 +1,9 @@ -package org.bouncycastle1.crypto.modes; +package org.bouncycastle.crypto.modes; -import org.bouncycastle1.crypto.BlockCipher; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.DataLengthException; -import org.bouncycastle1.crypto.params.ParametersWithIV; +import org.bouncycastle.crypto.BlockCipher; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.DataLengthException; +import org.bouncycastle.crypto.params.ParametersWithIV; /** * implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher. diff --git a/src/org/bouncycastle1/crypto/paddings/BlockCipherPadding.java b/src/org/bouncycastle/crypto/paddings/BlockCipherPadding.java similarity index 90% rename from src/org/bouncycastle1/crypto/paddings/BlockCipherPadding.java rename to src/org/bouncycastle/crypto/paddings/BlockCipherPadding.java index ff3c8bca9..c14bdb66c 100644 --- a/src/org/bouncycastle1/crypto/paddings/BlockCipherPadding.java +++ b/src/org/bouncycastle/crypto/paddings/BlockCipherPadding.java @@ -1,8 +1,8 @@ -package org.bouncycastle1.crypto.paddings; +package org.bouncycastle.crypto.paddings; -import org.bouncycastle1.crypto.SecureRandom; +import org.bouncycastle.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.SecureRandom; -import org.bouncycastle1.crypto.InvalidCipherTextException; /** * Block cipher padders are expected to conform to this interface diff --git a/src/org/bouncycastle1/crypto/paddings/PKCS7Padding.java b/src/org/bouncycastle/crypto/paddings/PKCS7Padding.java similarity index 89% rename from src/org/bouncycastle1/crypto/paddings/PKCS7Padding.java rename to src/org/bouncycastle/crypto/paddings/PKCS7Padding.java index 2f49dab58..b3fbd75df 100644 --- a/src/org/bouncycastle1/crypto/paddings/PKCS7Padding.java +++ b/src/org/bouncycastle/crypto/paddings/PKCS7Padding.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.paddings; +package org.bouncycastle.crypto.paddings; -import org.bouncycastle1.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.InvalidCipherTextException; /** * A padder that adds PKCS7/PKCS5 padding to a block. @@ -13,7 +13,7 @@ public class PKCS7Padding * * @param random - a SecureRandom if available. */ - public void init(org.bouncycastle1.crypto.SecureRandom random) + public void init(org.bouncycastle.crypto.SecureRandom random) throws IllegalArgumentException { // nothing to do. diff --git a/src/org/bouncycastle1/crypto/paddings/PaddedBufferedBlockCipher.java b/src/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.java similarity index 95% rename from src/org/bouncycastle1/crypto/paddings/PaddedBufferedBlockCipher.java rename to src/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.java index e165b34ff..092820e13 100644 --- a/src/org/bouncycastle1/crypto/paddings/PaddedBufferedBlockCipher.java +++ b/src/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.java @@ -1,11 +1,11 @@ -package org.bouncycastle1.crypto.paddings; +package org.bouncycastle.crypto.paddings; -import org.bouncycastle1.crypto.BlockCipher; -import org.bouncycastle1.crypto.BufferedBlockCipher; -import org.bouncycastle1.crypto.CipherParameters; -import org.bouncycastle1.crypto.DataLengthException; -import org.bouncycastle1.crypto.InvalidCipherTextException; -import org.bouncycastle1.crypto.params.ParametersWithRandom; +import org.bouncycastle.crypto.BlockCipher; +import org.bouncycastle.crypto.BufferedBlockCipher; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.DataLengthException; +import org.bouncycastle.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.params.ParametersWithRandom; /** * A wrapper class that allows block ciphers to be used to process data in diff --git a/src/org/bouncycastle1/crypto/params/KeyParameter.java b/src/org/bouncycastle/crypto/params/KeyParameter.java similarity index 83% rename from src/org/bouncycastle1/crypto/params/KeyParameter.java rename to src/org/bouncycastle/crypto/params/KeyParameter.java index 51222f074..5c4fe0e0b 100644 --- a/src/org/bouncycastle1/crypto/params/KeyParameter.java +++ b/src/org/bouncycastle/crypto/params/KeyParameter.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.params; +package org.bouncycastle.crypto.params; -import org.bouncycastle1.crypto.CipherParameters; +import org.bouncycastle.crypto.CipherParameters; public class KeyParameter implements CipherParameters diff --git a/src/org/bouncycastle1/crypto/params/ParametersWithIV.java b/src/org/bouncycastle/crypto/params/ParametersWithIV.java similarity index 89% rename from src/org/bouncycastle1/crypto/params/ParametersWithIV.java rename to src/org/bouncycastle/crypto/params/ParametersWithIV.java index ee77d8fb5..4a1e6e9a3 100644 --- a/src/org/bouncycastle1/crypto/params/ParametersWithIV.java +++ b/src/org/bouncycastle/crypto/params/ParametersWithIV.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.params; +package org.bouncycastle.crypto.params; -import org.bouncycastle1.crypto.CipherParameters; +import org.bouncycastle.crypto.CipherParameters; public class ParametersWithIV implements CipherParameters diff --git a/src/org/bouncycastle1/crypto/params/ParametersWithRandom.java b/src/org/bouncycastle/crypto/params/ParametersWithRandom.java similarity index 82% rename from src/org/bouncycastle1/crypto/params/ParametersWithRandom.java rename to src/org/bouncycastle/crypto/params/ParametersWithRandom.java index 2c29decee..d26ba703c 100644 --- a/src/org/bouncycastle1/crypto/params/ParametersWithRandom.java +++ b/src/org/bouncycastle/crypto/params/ParametersWithRandom.java @@ -1,8 +1,8 @@ -package org.bouncycastle1.crypto.params; +package org.bouncycastle.crypto.params; -import org.bouncycastle1.crypto.CipherParameters; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.SecureRandom; -import org.bouncycastle1.crypto.SecureRandom; public class ParametersWithRandom implements CipherParameters diff --git a/src/org/bouncycastle1/crypto/prng/DigestRandomGenerator.java b/src/org/bouncycastle/crypto/prng/DigestRandomGenerator.java similarity index 96% rename from src/org/bouncycastle1/crypto/prng/DigestRandomGenerator.java rename to src/org/bouncycastle/crypto/prng/DigestRandomGenerator.java index 135da94d0..ed1e9d6f1 100644 --- a/src/org/bouncycastle1/crypto/prng/DigestRandomGenerator.java +++ b/src/org/bouncycastle/crypto/prng/DigestRandomGenerator.java @@ -1,6 +1,6 @@ -package org.bouncycastle1.crypto.prng; +package org.bouncycastle.crypto.prng; -import org.bouncycastle1.crypto.Digest; +import org.bouncycastle.crypto.Digest; /** * Random generation based on the digest with counter. Calling addSeedMaterial will diff --git a/src/org/bouncycastle1/crypto/prng/RandomGenerator.java b/src/org/bouncycastle/crypto/prng/RandomGenerator.java similarity index 95% rename from src/org/bouncycastle1/crypto/prng/RandomGenerator.java rename to src/org/bouncycastle/crypto/prng/RandomGenerator.java index 689fb3f97..47ff68e3e 100644 --- a/src/org/bouncycastle1/crypto/prng/RandomGenerator.java +++ b/src/org/bouncycastle/crypto/prng/RandomGenerator.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.crypto.prng; +package org.bouncycastle.crypto.prng; /** * Generic interface for objects generating random bytes. diff --git a/src/org/bouncycastle1/util/Arrays.java b/src/org/bouncycastle/util/Arrays.java similarity index 97% rename from src/org/bouncycastle1/util/Arrays.java rename to src/org/bouncycastle/util/Arrays.java index 9c5ee7301..ea9af1448 100644 --- a/src/org/bouncycastle1/util/Arrays.java +++ b/src/org/bouncycastle/util/Arrays.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.util; +package org.bouncycastle.util; /** * General array utilities. diff --git a/src/org/bouncycastle1/util/encoders/Encoder.java b/src/org/bouncycastle/util/encoders/Encoder.java similarity index 91% rename from src/org/bouncycastle1/util/encoders/Encoder.java rename to src/org/bouncycastle/util/encoders/Encoder.java index c54498c0a..b06612105 100644 --- a/src/org/bouncycastle1/util/encoders/Encoder.java +++ b/src/org/bouncycastle/util/encoders/Encoder.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.util.encoders; +package org.bouncycastle.util.encoders; import java.io.IOException; import java.io.OutputStream; diff --git a/src/org/bouncycastle1/util/encoders/Hex.java b/src/org/bouncycastle/util/encoders/Hex.java similarity index 98% rename from src/org/bouncycastle1/util/encoders/Hex.java rename to src/org/bouncycastle/util/encoders/Hex.java index 75f1b26c5..d69f7739f 100644 --- a/src/org/bouncycastle1/util/encoders/Hex.java +++ b/src/org/bouncycastle/util/encoders/Hex.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.util.encoders; +package org.bouncycastle.util.encoders; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/src/org/bouncycastle1/util/encoders/HexEncoder.java b/src/org/bouncycastle/util/encoders/HexEncoder.java similarity index 98% rename from src/org/bouncycastle1/util/encoders/HexEncoder.java rename to src/org/bouncycastle/util/encoders/HexEncoder.java index c951fc36e..0dcae2917 100644 --- a/src/org/bouncycastle1/util/encoders/HexEncoder.java +++ b/src/org/bouncycastle/util/encoders/HexEncoder.java @@ -1,4 +1,4 @@ -package org.bouncycastle1.util.encoders; +package org.bouncycastle.util.encoders; import java.io.IOException; import java.io.OutputStream; diff --git a/src/org/phoneid/keepassj2me/ImporterV3.java b/src/org/phoneid/keepassj2me/ImporterV3.java index e39152b0e..5350f86cb 100644 --- a/src/org/phoneid/keepassj2me/ImporterV3.java +++ b/src/org/phoneid/keepassj2me/ImporterV3.java @@ -36,14 +36,14 @@ import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.ShortBufferException; -import org.bouncycastle1.crypto.BufferedBlockCipher; -import org.bouncycastle1.crypto.InvalidCipherTextException; -import org.bouncycastle1.crypto.digests.SHA256Digest; -import org.bouncycastle1.crypto.engines.AESEngine; -import org.bouncycastle1.crypto.modes.CBCBlockCipher; -import org.bouncycastle1.crypto.paddings.PKCS7Padding; -import org.bouncycastle1.crypto.params.KeyParameter; -import org.bouncycastle1.crypto.params.ParametersWithIV; +import org.bouncycastle.crypto.BufferedBlockCipher; +import org.bouncycastle.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.engines.AESEngine; +import org.bouncycastle.crypto.modes.CBCBlockCipher; +import org.bouncycastle.crypto.paddings.PKCS7Padding; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.crypto.params.ParametersWithIV; import org.phoneid.PhoneIDUtil; import android.util.Log; @@ -181,7 +181,12 @@ public class ImporterV3 { int encryptedPartSize = 0; //try { PKCS7Padding padding = new PKCS7Padding(); - encryptedPartSize = paddedEncryptedPartSize - padding.padCount(filebuf); + int paddingSize = padding.padCount(filebuf); + encryptedPartSize = paddedEncryptedPartSize - paddingSize; + if ( mDebug ) { + newManager.paddingBytes = paddingSize; + } + //} catch (Exception e) { //} // NI @@ -320,7 +325,7 @@ public class ImporterV3 { * @throws ShortBufferException */ - static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) + public static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) /*throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, diff --git a/src/org/phoneid/keepassj2me/PwManager.java b/src/org/phoneid/keepassj2me/PwManager.java index cbc13cfd2..004462443 100644 --- a/src/org/phoneid/keepassj2me/PwManager.java +++ b/src/org/phoneid/keepassj2me/PwManager.java @@ -32,7 +32,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.Vector; -import org.bouncycastle1.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.digests.SHA256Digest; import com.android.keepass.keepasslib.InvalidKeyFileException; @@ -65,12 +65,14 @@ public class PwManager { // Used for in-memory encryption of passwords // private byte sessionKey[] = new byte[PWM_SESSION_KEY_SIZE]; // Master key used to encrypt the whole database - byte masterKey[] = new byte[32]; + public byte masterKey[] = new byte[32]; // Algorithm used to encrypt the database int algorithm; int numKeyEncRounds; + // Debugging entries public PwDbHeader dbHeader; + public long paddingBytes; // root group PwGroup rootGroup; diff --git a/tests/src/com/android/keepass/tests/PwManagerOutputTest.java b/tests/src/com/android/keepass/tests/PwManagerOutputTest.java index 551468f7c..13c2755d4 100644 --- a/tests/src/com/android/keepass/tests/PwManagerOutputTest.java +++ b/tests/src/com/android/keepass/tests/PwManagerOutputTest.java @@ -22,6 +22,9 @@ package com.android.keepass.tests; import static org.junit.Assert.assertArrayEquals; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.security.DigestOutputStream; @@ -30,10 +33,12 @@ import java.security.NoSuchAlgorithmException; import junit.framework.TestCase; +import org.phoneid.keepassj2me.PwDbHeader; import org.phoneid.keepassj2me.PwManager; import com.android.keepass.keepasslib.PwDbHeaderOutput; import com.android.keepass.keepasslib.PwManagerOutput; +import com.android.keepass.keepasslib.PwManagerOutput.PwManagerOutputException; public class PwManagerOutputTest extends TestCase { PwManager mPM; @@ -46,7 +51,7 @@ public class PwManagerOutputTest extends TestCase { } - public void testPlainContent() throws IOException { + public void testPlainContent() throws IOException, PwManagerOutputException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PwManagerOutput pos = new PwManagerOutput(mPM, bos, PwManagerOutput.DEBUG); @@ -56,7 +61,7 @@ public class PwManagerOutputTest extends TestCase { } - public void testChecksum() throws NoSuchAlgorithmException, IOException { + public void testChecksum() throws NoSuchAlgorithmException, IOException, PwManagerOutputException { FileOutputStream fos = new FileOutputStream("/dev/null"); MessageDigest md = MessageDigest.getInstance("SHA-256"); @@ -68,7 +73,7 @@ public class PwManagerOutputTest extends TestCase { assertArrayEquals("Hash of groups and entries failed.", md.digest(), mPM.dbHeader.contentsHash); } - public void testHeader() throws Exception { + public void testHeader() throws PwManagerOutputException, IOException { ByteArrayOutputStream bActual = new ByteArrayOutputStream(); PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); pActual.outputHeader(bActual); @@ -80,17 +85,29 @@ public class PwManagerOutputTest extends TestCase { assertArrayEquals("Header does not match.", bExpected.toByteArray(), bActual.toByteArray()); } - /* - public void testEncryptedPart() throws Exception { + public void testFullWrite() throws IOException, PwManagerOutputException { File file = new File("/sdcard/test1.kdb"); - long length = file.length(); FileInputStream fis = new FileInputStream(file); - byte[] expected = new byte[(int)(length-PwDbHeader.BUF_SIZE)]; + + // Pull file into byte array (for streaming fun) + ByteArrayOutputStream bExpected = new ByteArrayOutputStream(); + while (true) { + int data = fis.read(); + if ( data == -1 ) { + break; + } + bExpected.write(data); + } - fis.skip(PwDbHeader.BUF_SIZE); - fis.read(expected); + ByteArrayOutputStream bActual = new ByteArrayOutputStream(); + PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); + pActual.output(); + pActual.close(); + bActual.close(); + + assertArrayEquals("Databases do not match.", bExpected.toByteArray(), bActual.toByteArray()); + } - */ } \ No newline at end of file diff --git a/tests/src/com/android/keepass/tests/TestData.java b/tests/src/com/android/keepass/tests/TestData.java index 4c742c586..0ddee73c1 100644 --- a/tests/src/com/android/keepass/tests/TestData.java +++ b/tests/src/com/android/keepass/tests/TestData.java @@ -23,7 +23,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import org.bouncycastle1.crypto.InvalidCipherTextException; +import org.bouncycastle.crypto.InvalidCipherTextException; import org.phoneid.keepassj2me.ImporterV3; import org.phoneid.keepassj2me.PwManager;