2
0

Some generic code fix-ups throughout.

This commit is contained in:
Maarten Billemont 2015-02-05 13:14:17 -05:00
parent 145008406d
commit b84ae532f2
29 changed files with 100 additions and 94 deletions

View File

@ -13,7 +13,6 @@
<name>Master Password Algorithm Implementation</name>
<description>The implementation of the Master Password algorithm</description>
<groupId>com.lyndir.masterpassword</groupId>
<artifactId>masterpassword-algorithm</artifactId>
<packaging>jar</packaging>

View File

@ -147,7 +147,7 @@ public enum MPSiteType {
*
* @return The type registered with the given name.
*/
public static MPSiteType forName(final String name) {
public static MPSiteType forName(@Nullable final String name) {
if (name == null)
return null;

View File

@ -3,6 +3,7 @@ package com.lyndir.masterpassword;
import com.google.common.collect.ImmutableList;
import com.lyndir.lhunath.opal.system.logging.Logger;
import java.util.List;
import javax.annotation.Nullable;
/**
@ -65,7 +66,7 @@ public enum MPSiteVariant {
*
* @return The variant registered with the given name.
*/
public static MPSiteVariant forName(final String name) {
public static MPSiteVariant forName(@Nullable final String name) {
if (name == null)
return null;

View File

@ -3,10 +3,8 @@ package com.lyndir.masterpassword;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.lyndir.lhunath.opal.system.util.MetaObject;
import java.util.List;
import java.util.Map;
/**

View File

@ -1,8 +1,6 @@
package com.lyndir.masterpassword;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.MetaObject;
import com.lyndir.lhunath.opal.system.util.ObjectMeta;
/**

View File

@ -54,7 +54,7 @@ public abstract class MasterKey {
@Nullable
protected abstract byte[] deriveKey(final char[] masterPassword);
protected abstract Version getAlgorithm();
public abstract Version getAlgorithmVersion();
@NotNull
public String getFullName() {
@ -63,18 +63,18 @@ public abstract class MasterKey {
}
@Nonnull
protected byte[] getMasterKey() {
protected byte[] getKey() {
return Preconditions.checkNotNull( masterKey );
}
public byte[] getKeyID() {
return idForBytes( getMasterKey() );
return idForBytes( getKey() );
}
public abstract String encode(@Nonnull final String siteName, final MPSiteType siteType, int siteCounter, final MPSiteVariant siteVariant,
@Nullable final String siteContext);
public abstract String encode(@Nonnull final String siteName, final MPSiteType siteType, int siteCounter,
final MPSiteVariant siteVariant, @Nullable final String siteContext);
public boolean isValid() {
return masterKey != null;
@ -95,6 +95,10 @@ public abstract class MasterKey {
long start = System.currentTimeMillis();
masterKey = deriveKey( masterPassword );
if (masterKey == null)
logger.dbg( "masterKey calculation failed after %.2fs.", (System.currentTimeMillis() - start) / 1000D );
else
logger.trc( "masterKey ID: %s (derived in %.2fs)", CodeUtils.encodeHex( idForBytes( masterKey ) ),
(System.currentTimeMillis() - start) / 1000D );

View File

@ -41,7 +41,7 @@ public class MasterKeyV0 extends MasterKey {
}
@Override
protected Version getAlgorithm() {
public Version getAlgorithmVersion() {
return Version.V0;
}
@ -101,7 +101,7 @@ public class MasterKeyV0 extends MasterKey {
sitePasswordInfo = Bytes.concat( sitePasswordInfo, siteContextLengthBytes, siteContextBytes );
logger.trc( "sitePasswordInfo ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordInfo ) ) );
byte[] sitePasswordSeed = MP_mac.of( getMasterKey(), sitePasswordInfo );
byte[] sitePasswordSeed = MP_mac.of( getKey(), sitePasswordInfo );
logger.trc( "sitePasswordSeed ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordSeed ) ) );
Preconditions.checkState( sitePasswordSeed.length > 0 );

View File

@ -1,15 +1,9 @@
package com.lyndir.masterpassword;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Bytes;
import com.lambdaworks.crypto.SCrypt;
import com.lyndir.lhunath.opal.system.*;
import com.lyndir.lhunath.opal.system.logging.Logger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import javax.annotation.Nullable;
@ -30,7 +24,7 @@ public class MasterKeyV1 extends MasterKeyV0 {
}
@Override
protected Version getAlgorithm() {
public Version getAlgorithmVersion() {
return Version.V1;
}
@ -64,7 +58,7 @@ public class MasterKeyV1 extends MasterKeyV0 {
sitePasswordInfo = Bytes.concat( sitePasswordInfo, siteContextLengthBytes, siteContextBytes );
logger.trc( "sitePasswordInfo ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordInfo ) ) );
byte[] sitePasswordSeed = MP_mac.of( getMasterKey(), sitePasswordInfo );
byte[] sitePasswordSeed = MP_mac.of( getKey(), sitePasswordInfo );
logger.trc( "sitePasswordSeed ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordSeed ) ) );
Preconditions.checkState( sitePasswordSeed.length > 0 );

View File

@ -23,7 +23,7 @@ public class MasterKeyV2 extends MasterKeyV1 {
}
@Override
protected Version getAlgorithm() {
public Version getAlgorithmVersion() {
return Version.V2;
}
@ -57,7 +57,7 @@ public class MasterKeyV2 extends MasterKeyV1 {
sitePasswordInfo = Bytes.concat( sitePasswordInfo, siteContextLengthBytes, siteContextBytes );
logger.trc( "sitePasswordInfo ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordInfo ) ) );
byte[] sitePasswordSeed = MP_mac.of( getMasterKey(), sitePasswordInfo );
byte[] sitePasswordSeed = MP_mac.of( getKey(), sitePasswordInfo );
logger.trc( "sitePasswordSeed ID: %s", CodeUtils.encodeHex( idForBytes( sitePasswordSeed ) ) );
Preconditions.checkState( sitePasswordSeed.length > 0 );

View File

@ -26,7 +26,7 @@ public class MasterKeyV3 extends MasterKeyV2 {
}
@Override
protected Version getAlgorithm() {
public Version getAlgorithmVersion() {
return Version.V3;
}

View File

@ -149,7 +149,7 @@ public class MPWTests {
}
public char[] getMasterPassword() {
return masterPassword.toCharArray();
return masterPassword == null? null: masterPassword.toCharArray();
}
public String getKeyID() {
@ -161,7 +161,7 @@ public class MPWTests {
}
public int getSiteCounter() {
return siteCounter;
return ifNotNullElse( siteCounter, 1 );
}
public MPSiteType getSiteType() {

View File

@ -13,13 +13,19 @@
<name>Master Password Android</name>
<description>An Android application to the Master Password algorithm</description>
<groupId>com.lyndir.masterpassword</groupId>
<artifactId>masterpassword-android</artifactId>
<packaging>apk</packaging>
<!-- BUILD CONFIGURATION -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<configuration>
<timestampFormat>YYYYMMddHHmm</timestampFormat>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
@ -30,9 +36,23 @@
<skip>false</skip>
</zipalign>
<sdk>
<platform>19</platform>
<platform>21</platform>
</sdk>
</configuration>
<executions>
<execution>
<id>manifest-merger</id>
<phase>process-resources</phase>
<goals>
<goal>manifest-update</goal>
</goals>
<configuration>
<manifestVersionCode>${timestamp}</manifestVersionCode>
<manifestVersionName>${project.version}</manifestVersionName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -1,15 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true">
android:fillViewport="true"
android:background="@drawable/background">
<LinearLayout
android:orientation="horizontal"
@ -33,4 +28,3 @@
android:layout_height="0dp" />
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>

View File

@ -205,7 +205,7 @@ public class EmergencyActivity extends Activity {
final MasterKey.Version version = (MasterKey.Version) siteVersionField.getSelectedItem();
try {
if (fullName.hashCode() == hc_userName && Arrays.hashCode( masterPassword ) == hc_masterPassword &&
masterKeyFuture != null && masterKeyFuture.get().getAlgorithm() == version)
masterKeyFuture != null && masterKeyFuture.get().getAlgorithmVersion() == version)
return;
}
catch (InterruptedException | ExecutionException e) {

View File

@ -1,7 +1,6 @@
package com.lyndir.masterpassword;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;

View File

@ -2,16 +2,14 @@ package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import java.util.Objects;
/**
* @author lhunath, 2014-08-20
*/
public class User {
private String name;
private Avatar avatar;
private final String name;
private final Avatar avatar;
public User(final String name, final Avatar avatar) {
this.name = name;

View File

@ -13,7 +13,6 @@
<name>Master Password CLI</name>
<description>A CLI interface to the Master Password algorithm</description>
<groupId>com.lyndir.masterpassword</groupId>
<artifactId>masterpassword-cli</artifactId>
<packaging>jar</packaging>

View File

@ -45,7 +45,7 @@ public class CLI {
throws IOException {
// Read information from the environment.
char[] masterPassword = null;
char[] masterPassword;
String siteName = null, context = null;
String userName = System.getenv( ENV_USERNAME );
String siteTypeName = ifNotNullElse( System.getenv( ENV_SITETYPE ), "" );

View File

@ -13,7 +13,6 @@
<name>Master Password GUI</name>
<description>A GUI interface to the Master Password algorithm</description>
<groupId>com.lyndir.masterpassword</groupId>
<artifactId>masterpassword-gui</artifactId>
<packaging>jar</packaging>

View File

@ -21,8 +21,6 @@ import com.google.common.base.Charsets;
import com.google.common.io.*;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.TypeUtils;
import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.IncorrectMasterPasswordException;
import java.io.*;
import java.net.URI;
import java.net.URL;
@ -41,7 +39,7 @@ public class GUI implements UnlockFrame.SignInCallback {
@SuppressWarnings("UnusedDeclaration")
private static final Logger logger = Logger.get( GUI.class );
private UnlockFrame unlockFrame = new UnlockFrame( this );
private final UnlockFrame unlockFrame = new UnlockFrame( this );
private PasswordFrame passwordFrame;
public static void main(final String[] args)

View File

@ -1,6 +1,7 @@
package com.lyndir.masterpassword.gui;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.masterpassword.model.MPUser;
@ -137,8 +138,8 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite
return FluentIterable.from( MPUserFileManager.get().getUsers() ).transform( new Function<MPUser, ModelUser>() {
@Nullable
@Override
public ModelUser apply(final MPUser model) {
return new ModelUser( model );
public ModelUser apply(@Nullable final MPUser model) {
return new ModelUser( Preconditions.checkNotNull( model ) );
}
} ).toArray( ModelUser.class );
}

View File

@ -48,7 +48,7 @@ public class ModelUser extends User {
public void authenticate(final char[] masterPassword)
throws IncorrectMasterPasswordException {
model.authenticate( masterPassword );
putKey( model.authenticate( masterPassword ) );
this.masterPassword = masterPassword;
}
@ -81,6 +81,7 @@ public class ModelUser extends User {
}
public boolean keySaved() {
// TODO
return false;
}
}

View File

@ -1,7 +1,6 @@
package com.lyndir.masterpassword.gui;
import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.IncorrectMasterPasswordException;
@ -17,7 +16,7 @@ import javax.annotation.Nullable;
public abstract class User {
@Nonnull
private static final EnumMap<MasterKey.Version, MasterKey> keyByVersion = Maps.newEnumMap( MasterKey.Version.class );
private final EnumMap<MasterKey.Version, MasterKey> keyByVersion = Maps.newEnumMap( MasterKey.Version.class );
public abstract String getFullName();
@ -37,17 +36,23 @@ public abstract class User {
@Nonnull
public MasterKey getKey(MasterKey.Version algorithmVersion) {
char[] masterPassword = getMasterPassword();
char[] masterPassword = Preconditions.checkNotNull( getMasterPassword(), "User is not authenticated: " + getFullName() );
MasterKey key = keyByVersion.get( algorithmVersion );
if (key == null)
keyByVersion.put( algorithmVersion, key = MasterKey.create( algorithmVersion, getFullName(), masterPassword ) );
putKey( key = MasterKey.create( algorithmVersion, getFullName(), masterPassword ) );
if (!key.isValid())
key.revalidate( masterPassword );
return key;
}
protected void putKey(MasterKey masterKey) {
MasterKey oldKey = keyByVersion.put( masterKey.getAlgorithmVersion(), masterKey );
if (oldKey != null)
oldKey.invalidate();
}
public void reset() {
for (MasterKey key : keyByVersion.values())
key.invalidate();

View File

@ -13,7 +13,6 @@
<name>Master Password Site Model</name>
<description>A persistence model for Master Password sites.</description>
<groupId>com.lyndir.masterpassword</groupId>
<artifactId>masterpassword-model</artifactId>
<packaging>jar</packaging>

View File

@ -1,6 +1,5 @@
package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.lyndir.masterpassword.*;
@ -56,7 +55,7 @@ public class MPSite {
return resultFor( masterKey, MPSiteVariant.Password, null );
}
public String resultFor(final MasterKey masterKey, final MPSiteVariant variant, final String context) {
public String resultFor(final MasterKey masterKey, final MPSiteVariant variant, @Nullable final String context) {
return masterKey.encode( siteName, siteType, siteCounter, variant, context );
}

View File

@ -4,7 +4,6 @@ import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.google.common.base.Preconditions;
import com.lyndir.lhunath.opal.system.CodeUtils;
import com.lyndir.masterpassword.MasterKey;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -51,11 +50,12 @@ public class MPSiteMarshaller {
}
private String marshallHeader(final ContentMode contentMode, final MPUser user, @Nullable final MasterKey masterKey) {
this.contentMode = contentMode;
this.masterKey = masterKey;
StringBuilder header = new StringBuilder();
header.append( "# Master Password site export\n" );
header.append( "# " ).append( contentMode.description() ).append( '\n' );
header.append( "# " ).append( this.contentMode.description() ).append( '\n' );
header.append( "# \n" );
header.append( "##\n" );
header.append( "# Format: 1\n" );
@ -67,7 +67,7 @@ public class MPSiteMarshaller {
header.append( "# Version: " ).append( MasterKey.Version.CURRENT.toBundleVersion() ).append( '\n' );
header.append( "# Algorithm: " ).append( MasterKey.Version.CURRENT.toInt() ).append( '\n' );
header.append( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' );
header.append( "# Passwords: " ).append( contentMode.name() ).append( '\n' );
header.append( "# Passwords: " ).append( this.contentMode.name() ).append( '\n' );
header.append( "##\n" );
header.append( "#\n" );
header.append( "# Last Times Password Login\t Site\tSite\n" );

View File

@ -23,7 +23,7 @@ public class MPUser implements Comparable<MPUser> {
@Nullable
private byte[] keyID;
private MasterKey.Version algorithmVersion;
private final MasterKey.Version algorithmVersion;
private int avatar;
private MPSiteType defaultType;
private ReadableInstant lastUsed;

View File

@ -58,9 +58,9 @@ public class MPUserFileManager extends MPUserManager {
} ) ) ).transform( new Function<File, MPUser>() {
@Nullable
@Override
public MPUser apply(final File file) {
public MPUser apply(@Nullable final File file) {
try {
return MPSiteUnmarshaller.unmarshall( file ).getUser();
return MPSiteUnmarshaller.unmarshall( Preconditions.checkNotNull( file ) ).getUser();
}
catch (IOException e) {
logger.err( e, "Couldn't read user from: %s", file );

View File

@ -16,7 +16,7 @@ public abstract class MPUserManager {
return instance;
}
public MPUserManager(final Iterable<MPUser> users) {
protected MPUserManager(final Iterable<MPUser> users) {
for (MPUser user : users)
addUser( user );
}