Style changes.

This commit is contained in:
Brian Pellin committed 2010-04-29 15:03:51 -05:00
1 parent ebe8424d11
commit 2e4d009e40
26 files changed
+260 -272

No files matched your search

+2 -181
View File
@@ -26,20 +26,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
package com.keepassdroid.database;
// Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Vector;
import com.keepassdroid.database.exception.InvalidKeyFileException;
/**
* @author Naomaru Itoi <[email protected]>
@@ -63,17 +55,9 @@ public class PwDatabaseV3 extends PwDatabase {
public Vector<PwEntryV3> entries = new Vector<PwEntryV3>();
// all groups
public Vector<PwGroupV3> groups = new Vector<PwGroupV3>();
// Last modified entry, use GetLastEditedEntry() to get it
// PwEntryV3 lastEditedEntry = null;
// Pseudo-random number generator
//CNewRandom m_random;
// Used for in-memory encryption of passwords
// private byte sessionKey[] = new byte[PWM_SESSION_KEY_SIZE];
// Master key used to encrypt the whole database
public byte masterKey[] = new byte[32];
// Algorithm used to encrypt the database
public int algorithm;
public int mNumKeyEncRounds;
public int numKeyEncRounds;
// Debugging entries
public PwDbHeaderV3 dbHeader;
@@ -88,172 +72,9 @@ public class PwDatabaseV3 extends PwDatabase {
}
public int getNumKeyEncRecords() {
return mNumKeyEncRounds;
return numKeyEncRounds;
}
public void setMasterKey( String key, String keyFileName ) throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
masterKey = getMasterKey(key, keyFileName);
}
public static byte[] getMasterKey(String key, String keyFileName) throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
if ( key.length() > 0 && keyFileName.length() > 0 ) {
return getCompositeKey(key, keyFileName);
} else if ( key.length() > 0 ) {
return getPasswordKey(key);
} else if ( keyFileName.length() > 0 ) {
return getFileKey(keyFileName);
} else {
throw new IllegalArgumentException( "Key cannot be empty." );
}
}
private static byte[] getCompositeKey( String key, String keyFileName) throws InvalidKeyFileException, IOException {
assert(key != null && keyFileName != null);
byte[] fileKey = getFileKey(keyFileName);
byte[] passwordKey = getPasswordKey(key);
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not supported");
}
md.update(passwordKey);
return md.digest(fileKey);
/*
SHA256Digest md = new SHA256Digest();
md.update(passwordKey, 0, 32);
md.update(fileKey, 0, 32);
byte[] outputKey = new byte[md.getDigestSize()];
md.doFinal(outputKey, 0);
return outputKey;
*/
}
private static byte[] getFileKey(String fileName) throws InvalidKeyFileException, IOException {
assert(fileName != null);
File keyfile = new File(fileName);
long fileSize = keyfile.length();
if ( ! keyfile.exists() ) {
throw new InvalidKeyFileException("Key file does not exist.");
}
FileInputStream fis;
try {
fis = new FileInputStream(keyfile);
} catch (FileNotFoundException e) {
throw new InvalidKeyFileException("Key file does not exist.");
}
if ( fileSize == 0 ) {
throw new InvalidKeyFileException("Key file is empty.");
} else if ( fileSize == 32 ) {
byte[] outputKey = new byte[32];
if ( fis.read(outputKey, 0, 32) != 32 ) {
throw new IOException("Error reading key.");
}
return outputKey;
} else if ( fileSize == 64 ) {
byte[] hex = new byte[64];
if ( fis.read(hex, 0, 64) != 64 ) {
throw new IOException("Error reading key.");
}
return hexStringToByteArray(new String(hex));
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not supported");
}
//SHA256Digest md = new SHA256Digest();
byte[] buffer = new byte[2048];
int offset = 0;
try {
while (true) {
int bytesRead = fis.read(buffer, 0, 2048);
if ( bytesRead == -1 ) break; // End of file
md.update(buffer, 0, bytesRead);
offset += bytesRead;
}
} catch (Exception e) {
System.out.println(e.toString());
}
return md.digest();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
private static byte[] getPasswordKey(String key) throws IOException {
assert(key!=null);
if ( key.length() == 0 )
throw new IllegalArgumentException( "Key cannot be empty." );
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not supported");
}
//SHA256Digest md = new SHA256Digest();
byte[] bKey;
try {
bKey = key.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
assert false;
bKey = key.getBytes();
}
md.update(bKey, 0, bKey.length );
//byte[] outputKey = new byte[md.getDigestSize()];
return md.digest();
}
/*
//
// Erase all members and buffers, then null pointers.
// Ensures no memory (that we control) contains leftover keys.
//
void secureErase() {
// TODO finish this!
}
*/
public Vector<PwGroupV3> getGrpRoots() {
int target = 0;
Vector<PwGroupV3> kids = new Vector<PwGroupV3>();