2
0

Replace Version API with MPAlgorithm, make configuration instance-specific.

This commit is contained in:
Maarten Billemont
2018-04-26 15:56:12 -04:00
parent 33f2e0edda
commit 82e2d0b5ac
29 changed files with 397 additions and 226 deletions

View File

@@ -35,7 +35,7 @@ public class MPFileSite extends MPSite {
private String siteContent;
private UnsignedInteger siteCounter;
private MPResultType resultType;
private MPMasterKey.Version algorithmVersion;
private MPAlgorithm algorithm;
@Nullable
private String loginContent;
@@ -49,24 +49,24 @@ public class MPFileSite extends MPSite {
public MPFileSite(final MPFileUser user, final String siteName) {
this( user, siteName,
user.getAlgorithmVersion().getAlgorithm().mpw_default_counter,
user.getAlgorithmVersion().getAlgorithm().mpw_default_type,
user.getAlgorithmVersion() );
user.getAlgorithm().mpw_default_counter(),
user.getAlgorithm().mpw_default_type(),
user.getAlgorithm() );
}
public MPFileSite(final MPFileUser user, final String siteName, final UnsignedInteger siteCounter, final MPResultType resultType,
final MPMasterKey.Version algorithmVersion) {
final MPAlgorithm algorithm) {
this.user = user;
this.siteName = siteName;
this.siteCounter = siteCounter;
this.resultType = resultType;
this.algorithmVersion = algorithmVersion;
this.algorithm = algorithm;
this.lastUsed = new Instant();
}
protected MPFileSite(final MPFileUser user, final String siteName, @Nullable final String siteContent,
final UnsignedInteger siteCounter,
final MPResultType resultType, final MPMasterKey.Version algorithmVersion,
final MPResultType resultType, final MPAlgorithm algorithm,
@Nullable final String loginContent, @Nullable final MPResultType loginType,
@Nullable final String url, final int uses, final Instant lastUsed) {
this.user = user;
@@ -74,7 +74,7 @@ public class MPFileSite extends MPSite {
this.siteContent = siteContent;
this.siteCounter = siteCounter;
this.resultType = resultType;
this.algorithmVersion = algorithmVersion;
this.algorithm = algorithm;
this.loginContent = loginContent;
this.loginType = loginType;
this.url = url;
@@ -129,7 +129,7 @@ public class MPFileSite extends MPSite {
this.siteContent = null;
else
this.siteContent = masterKey.siteState(
getSiteName(), getSiteCounter(), MPKeyPurpose.Authentication, null, getResultType(), result, getAlgorithmVersion() );
getSiteName(), getSiteCounter(), MPKeyPurpose.Authentication, null, getResultType(), result, algorithm );
}
@Override
@@ -153,13 +153,13 @@ public class MPFileSite extends MPSite {
}
@Override
public MPMasterKey.Version getAlgorithmVersion() {
return algorithmVersion;
public MPAlgorithm getAlgorithm() {
return algorithm;
}
@Override
public void setAlgorithmVersion(final MPMasterKey.Version algorithmVersion) {
this.algorithmVersion = algorithmVersion;
public void setAlgorithm(final MPAlgorithm algorithm) {
this.algorithm = algorithm;
}
@Nullable
@@ -180,8 +180,8 @@ public class MPFileSite extends MPSite {
this.loginContent = null;
else
this.loginContent = masterKey.siteState(
siteName, MPAlgorithm.mpw_default_counter, MPKeyPurpose.Identification, null, this.loginType, result,
algorithmVersion );
siteName, algorithm.mpw_default_counter(), MPKeyPurpose.Identification, null, this.loginType, result,
algorithm );
}
@Nullable

View File

@@ -22,8 +22,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.masterpassword.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.joda.time.Instant;
@@ -43,7 +42,7 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
@Nullable
private byte[] keyID;
private MPMasterKey.Version algorithmVersion;
private MPAlgorithm algorithm;
private MPMarshalFormat format;
private int avatar;
@@ -51,18 +50,18 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
private ReadableInstant lastUsed;
public MPFileUser(final String fullName) {
this( fullName, null, MPMasterKey.Version.CURRENT );
this( fullName, null, MPMasterKey.Version.CURRENT.getAlgorithm() );
}
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPMasterKey.Version algorithmVersion) {
this( fullName, keyID, algorithmVersion, 0, MPAlgorithm.mpw_default_type, new Instant(), MPMarshalFormat.DEFAULT );
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPAlgorithm algorithm) {
this( fullName, keyID, algorithm, 0, algorithm.mpw_default_type(), new Instant(), MPMarshalFormat.DEFAULT );
}
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPMasterKey.Version algorithmVersion, final int avatar,
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPAlgorithm algorithm, final int avatar,
final MPResultType defaultType, final ReadableInstant lastUsed, final MPMarshalFormat format) {
this.fullName = fullName;
this.keyID = (keyID == null)? null: keyID.clone();
this.algorithmVersion = algorithmVersion;
this.algorithm = algorithm;
this.avatar = avatar;
this.defaultType = defaultType;
this.lastUsed = lastUsed;
@@ -75,12 +74,12 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
}
@Override
public MPMasterKey.Version getAlgorithmVersion() {
return algorithmVersion;
public MPAlgorithm getAlgorithm() {
return algorithm;
}
public void setAlgorithmVersion(final MPMasterKey.Version algorithmVersion) {
this.algorithmVersion = algorithmVersion;
public void setAlgorithm(final MPAlgorithm algorithm) {
this.algorithm = algorithm;
}
public MPMarshalFormat getFormat() {
@@ -117,7 +116,7 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
}
public Iterable<MPFileSite> getSites() {
return sites;
return Collections.unmodifiableCollection( sites );
}
@Override
@@ -158,8 +157,8 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
try {
key = new MPMasterKey( getFullName(), masterPassword );
if ((keyID == null) || (keyID.length == 0))
keyID = key.getKeyID( algorithmVersion );
else if (!Arrays.equals( key.getKeyID( algorithmVersion ), keyID ))
keyID = key.getKeyID( algorithm );
else if (!Arrays.equals( key.getKeyID( algorithm ), keyID ))
throw new MPIncorrectMasterPasswordException( this );
return key;

View File

@@ -127,7 +127,7 @@ public class MPFileUserManager extends MPUserManager {
}
}.write( user.getFormat().marshaller().marshall( user, masterKey, MPMarshaller.ContentMode.PROTECTED ) );
}
catch (final IOException e) {
catch (final MPMarshalException | IOException e) {
logger.err( e, "Unable to save sites for user: %s", user );
}
}

View File

@@ -48,7 +48,7 @@ public class MPFlatMarshaller implements MPMarshaller {
content.append( "# Full Name: " ).append( user.getFullName() ).append( '\n' );
content.append( "# Avatar: " ).append( user.getAvatar() ).append( '\n' );
content.append( "# Key ID: " ).append( user.exportKeyID() ).append( '\n' );
content.append( "# Algorithm: " ).append( user.getAlgorithmVersion().toInt() ).append( '\n' );
content.append( "# Algorithm: " ).append( user.getAlgorithm().version().toInt() ).append( '\n' );
content.append( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' );
content.append( "# Passwords: " ).append( contentMode.name() ).append( '\n' );
content.append( "##\n" );
@@ -69,7 +69,7 @@ public class MPFlatMarshaller implements MPMarshaller {
site.getUses(), // uses
strf( "%d:%d:%d", //
site.getResultType().getType(), // type
site.getAlgorithmVersion().toInt(), // algorithm
site.getAlgorithm().version().toInt(), // algorithm
site.getSiteCounter().intValue() ), // counter
ifNotNullElse( loginName, "" ), // loginName
site.getSiteName(), // siteName

View File

@@ -62,7 +62,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
String fullName = null;
int mpVersion = 0, importFormat = 0, avatar = 0;
boolean clearContent = false, headerStarted = false;
MPResultType defaultType = MPAlgorithm.mpw_default_type;
MPResultType defaultType = null;
//noinspection HardcodedLineSeparator
for (final String line : Splitter.on( CharMatcher.anyOf( "\r\n" ) ).omitEmptyStrings().split( content ))
@@ -73,8 +73,8 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
headerStarted = true;
else
// Ends the header.
user = new MPFileUser( fullName, keyID, MPMasterKey.Version.fromInt( mpVersion ), avatar, defaultType,
new DateTime( 0 ), MPMarshalFormat.Flat );
user = new MPFileUser( fullName, keyID, MPMasterKey.Version.fromInt( mpVersion ).getAlgorithm(),
avatar, defaultType, new DateTime( 0 ), MPMarshalFormat.Flat );
// Comment.
else if (line.startsWith( "#" )) {
@@ -114,10 +114,10 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
case 0:
site = new MPFileSite( user, //
siteMatcher.group( 5 ), siteMatcher.group( 6 ),
user.getAlgorithmVersion().getAlgorithm().mpw_default_counter,
user.getAlgorithm().mpw_default_counter(),
MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN(
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ),
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(),
null, null, null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ),
MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() );
break;
@@ -128,7 +128,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
UnsignedInteger.valueOf( colon.matcher( siteMatcher.group( 5 ) ).replaceAll( "" ) ),
MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN(
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ),
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(),
siteMatcher.group( 6 ), MPResultType.GeneratedName, null,
ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ),
MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() );

View File

@@ -43,24 +43,24 @@ public abstract class MPSite {
public abstract void setResultType(MPResultType resultType);
public abstract MPMasterKey.Version getAlgorithmVersion();
public abstract MPAlgorithm getAlgorithm();
public abstract void setAlgorithmVersion(MPMasterKey.Version algorithmVersion);
public abstract void setAlgorithm(MPAlgorithm algorithm);
public String resultFor(final MPMasterKey masterKey, final MPKeyPurpose keyPurpose, @Nullable final String keyContext,
@Nullable final String siteContent)
throws MPInvalidatedException {
return masterKey.siteResult(
getSiteName(), getSiteCounter(), keyPurpose, keyContext, getResultType(), siteContent, getAlgorithmVersion() );
getSiteName(), getSiteCounter(), keyPurpose, keyContext, getResultType(), siteContent, getAlgorithm() );
}
public String loginFor(final MPMasterKey masterKey, final MPResultType loginType, @Nullable final String loginContent)
throws MPInvalidatedException {
return masterKey.siteResult(
getSiteName(), MPAlgorithm.mpw_default_counter, MPKeyPurpose.Identification, null, loginType, loginContent,
getAlgorithmVersion() );
getSiteName(), getAlgorithm().mpw_default_counter(), MPKeyPurpose.Identification, null, loginType, loginContent,
getAlgorithm() );
}
@Override

View File

@@ -22,8 +22,7 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.base.Preconditions;
import com.lyndir.lhunath.opal.system.CodeUtils;
import com.lyndir.masterpassword.MPInvalidatedException;
import com.lyndir.masterpassword.MPMasterKey;
import com.lyndir.masterpassword.*;
import java.util.Collection;
import java.util.Objects;
import javax.annotation.Nonnull;
@@ -51,10 +50,10 @@ public abstract class MPUser<S extends MPSite> {
public String exportKeyID()
throws MPInvalidatedException {
return CodeUtils.encodeHex( getMasterKey().getKeyID( getAlgorithmVersion() ) );
return CodeUtils.encodeHex( getMasterKey().getKeyID( getAlgorithm() ) );
}
public abstract MPMasterKey.Version getAlgorithmVersion();
public abstract MPAlgorithm getAlgorithm();
public int getAvatar() {
return 0;