2
0

Fix warnings and inspections.

This commit is contained in:
Maarten Billemont
2018-04-27 11:32:54 -04:00
parent 82e2d0b5ac
commit cb74b1f3fc
30 changed files with 225 additions and 142 deletions

View File

@@ -18,7 +18,6 @@
package com.lyndir.masterpassword;
import com.google.common.base.Charsets;
import com.google.common.primitives.UnsignedInteger;
import com.lyndir.lhunath.opal.system.MessageAuthenticationDigests;
import com.lyndir.lhunath.opal.system.MessageDigests;
@@ -30,25 +29,28 @@ import javax.annotation.Nullable;
/**
* @see MPMasterKey.Version
*/
@SuppressWarnings({ "FieldMayBeStatic", "NewMethodNamingConvention" })
@SuppressWarnings({ "FieldMayBeStatic", "NewMethodNamingConvention", "MethodReturnAlwaysConstant" })
public abstract class MPAlgorithm {
public abstract byte[] masterKey(String fullName, char[] masterPassword);
public abstract byte[] siteKey(byte[] masterKey, String siteName, UnsignedInteger siteCounter, MPKeyPurpose keyPurpose,
@Nullable String keyContext);
public abstract byte[] siteKey(byte[] masterKey, String siteName, UnsignedInteger siteCounter,
MPKeyPurpose keyPurpose, @Nullable String keyContext);
public abstract String siteResult(byte[] masterKey, byte[] siteKey, String siteName, UnsignedInteger siteCounter, MPKeyPurpose keyPurpose,
@Nullable String keyContext, MPResultType resultType, @Nullable String resultParam);
public abstract String siteResult(byte[] masterKey, byte[] siteKey, String siteName, UnsignedInteger siteCounter,
MPKeyPurpose keyPurpose, @Nullable String keyContext,
MPResultType resultType, @Nullable String resultParam);
public abstract String sitePasswordFromTemplate(byte[] masterKey, byte[] siteKey, MPResultType resultType, @Nullable String resultParam);
public abstract String sitePasswordFromTemplate(byte[] masterKey, byte[] siteKey,
MPResultType resultType, @Nullable String resultParam);
public abstract String sitePasswordFromCrypt(byte[] masterKey, byte[] siteKey, MPResultType resultType, @Nullable String resultParam);
public abstract String sitePasswordFromDerive(byte[] masterKey, byte[] siteKey, MPResultType resultType, @Nullable String resultParam);
public abstract String siteState(byte[] masterKey, byte[] siteKey, String siteName, UnsignedInteger siteCounter, MPKeyPurpose keyPurpose,
@Nullable String keyContext, MPResultType resultType, String resultParam);
public abstract String siteState(byte[] masterKey, byte[] siteKey, String siteName, UnsignedInteger siteCounter,
MPKeyPurpose keyPurpose, @Nullable String keyContext,
MPResultType resultType, String resultParam);
// Configuration

View File

@@ -47,10 +47,10 @@ public class MPAlgorithmV3 extends MPAlgorithmV2 {
// Calculate the master key.
logger.trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%d, r=%d, p=%d )",
scrypt_N(), scrypt_r(), scrypt_p() );
byte[] mpBytes = toBytes( masterPassword );
byte[] masterKey = scrypt( masterKeySalt, mpBytes );
byte[] masterPasswordBytes = toBytes( masterPassword );
byte[] masterKey = scrypt( masterKeySalt, masterPasswordBytes );
Arrays.fill( masterKeySalt, (byte) 0 );
Arrays.fill( mpBytes, (byte) 0 );
Arrays.fill( masterPasswordBytes, (byte) 0 );
logger.trc( " => masterKey.id: %s", CodeUtils.encodeHex( toID( masterKey ) ) );
return masterKey;

View File

@@ -22,7 +22,6 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.base.Charsets;
import com.google.common.primitives.UnsignedBytes;
import com.google.common.primitives.UnsignedInteger;
import com.lyndir.lhunath.opal.system.MessageAuthenticationDigests;
import com.lyndir.lhunath.opal.system.logging.Logger;
import java.nio.*;

View File

@@ -21,7 +21,8 @@ package com.lyndir.masterpassword;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.lyndir.lhunath.opal.system.logging.Logger;
import java.util.*;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import org.jetbrains.annotations.Contract;
@@ -31,7 +32,7 @@ import org.jetbrains.annotations.Contract;
*
* @author lhunath
*/
@SuppressWarnings("RedundantTypeArguments" /* IDEA-191043 */)
@SuppressWarnings({ "RedundantTypeArguments", "SpellCheckingInspection" })
public enum MPResultType {
// bit 0-3 | MPResultTypeClass | MPSiteFeature
@@ -130,11 +131,11 @@ public enum MPResultType {
static final Logger logger = Logger.get( MPResultType.class );
private final String shortName;
private final String description;
private final List<MPTemplate> templates;
private final MPResultTypeClass typeClass;
private final int typeIndex;
private final String shortName;
private final String description;
private final List<MPTemplate> templates;
private final MPResultTypeClass typeClass;
private final int typeIndex;
private final ImmutableSet<MPSiteFeature> typeFeatures;
MPResultType(final String shortName, final String description, final List<MPTemplate> templates,
@@ -167,7 +168,7 @@ public enum MPResultType {
return typeClass;
}
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType" /* IDEA-191042 */ )
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType" /* IDEA-191042 */)
public ImmutableSet<MPSiteFeature> getTypeFeatures() {
return typeFeatures;

View File

@@ -27,6 +27,7 @@ import org.jetbrains.annotations.NonNls;
*
* @author lhunath
*/
@SuppressWarnings({ "HardcodedFileSeparator", "SpellCheckingInspection" })
public enum MPTemplateCharacterClass {
UpperVowel( 'V', "AEIOU" ),

View File

@@ -29,13 +29,13 @@ import org.joda.time.Instant;
*/
public class MPFileSite extends MPSite {
private final MPFileUser user;
private String siteName;
private final MPFileUser user;
private String siteName;
@Nullable
private String siteContent;
private UnsignedInteger siteCounter;
private MPResultType resultType;
private MPAlgorithm algorithm;
private String siteContent;
private UnsignedInteger siteCounter;
private MPResultType resultType;
private MPAlgorithm algorithm;
@Nullable
private String loginContent;
@@ -122,14 +122,15 @@ public class MPFileSite extends MPSite {
return siteContent;
}
public void setSitePassword(final MPMasterKey masterKey, @Nullable final MPResultType resultType, @Nullable final String result)
public void setSitePassword(final MPMasterKey masterKey, final MPResultType resultType, @Nullable final String result)
throws MPInvalidatedException {
this.resultType = resultType;
if (result == null)
this.siteContent = null;
else
this.siteContent = masterKey.siteState(
getSiteName(), getSiteCounter(), MPKeyPurpose.Authentication, null, getResultType(), result, algorithm );
siteName, siteCounter, MPKeyPurpose.Authentication, null, resultType, result, algorithm );
}
@Override

View File

@@ -32,6 +32,7 @@ import org.joda.time.ReadableInstant;
/**
* @author lhunath, 14-12-07
*/
@SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileUser> {
@SuppressWarnings("UnusedDeclaration")
@@ -41,9 +42,9 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
private final Collection<MPFileSite> sites = Sets.newHashSet();
@Nullable
private byte[] keyID;
private MPAlgorithm algorithm;
private MPMarshalFormat format;
private byte[] keyID;
private MPAlgorithm algorithm;
private MPMarshalFormat format;
private int avatar;
private MPResultType defaultType;

View File

@@ -36,6 +36,7 @@ import javax.annotation.Nonnull;
*
* @author lhunath, 14-12-07
*/
@SuppressWarnings("CallToSystemGetenv")
public class MPFileUserManager extends MPUserManager {
@SuppressWarnings("UnusedDeclaration")
@@ -50,21 +51,20 @@ public class MPFileUserManager extends MPUserManager {
instance = create( new File( ifNotNullElseNullable( System.getProperty( "user.home" ), System.getenv( "HOME" ) ), ".mpw.d" ) );
}
private final File userFilesDirectory;
private final File path;
public static MPFileUserManager get() {
MPUserManager.instance = instance;
return instance;
}
public static MPFileUserManager create(final File userFilesDirectory) {
return new MPFileUserManager( userFilesDirectory );
public static MPFileUserManager create(final File path) {
return new MPFileUserManager( path );
}
protected MPFileUserManager(final File userFilesDirectory) {
protected MPFileUserManager(final File path) {
super( unmarshallUsers( userFilesDirectory ) );
this.userFilesDirectory = userFilesDirectory;
super( unmarshallUsers( path ) );
this.path = path;
}
private static Iterable<MPFileUser> unmarshallUsers(final File userFilesDirectory) {
@@ -76,7 +76,7 @@ public class MPFileUserManager extends MPUserManager {
Map<String, MPFileUser> users = new HashMap<>();
for (final File userFile : listUserFiles( userFilesDirectory ))
for (final MPMarshalFormat format : MPMarshalFormat.values())
if (userFile.getName().endsWith( '.' + format.fileExtension() ))
if (userFile.getName().endsWith( format.fileSuffix() ))
try {
MPFileUser user = format.unmarshaller().unmarshall( userFile );
MPFileUser previousUser = users.put( user.getFullName(), user );
@@ -95,7 +95,7 @@ public class MPFileUserManager extends MPUserManager {
@Override
public boolean accept(final File dir, final String name) {
for (final MPMarshalFormat format : MPMarshalFormat.values())
if (name.endsWith( '.' + format.fileExtension() ))
if (name.endsWith( format.fileSuffix() ))
return true;
return false;
@@ -134,13 +134,13 @@ public class MPFileUserManager extends MPUserManager {
@Nonnull
private File getUserFile(final MPFileUser user) {
return new File( userFilesDirectory, user.getFullName() + ".mpsites" );
return new File( path, user.getFullName() + ".mpsites" );
}
/**
* @return The location on the file system where the user models are stored.
*/
public File getPath() {
return userFilesDirectory;
return path;
}
}

View File

@@ -29,6 +29,7 @@ import org.joda.time.Instant;
/**
* @author lhunath, 2017-09-20
*/
@SuppressWarnings({ "HardcodedLineSeparator", "MagicCharacter" })
public class MPFlatMarshaller implements MPMarshaller {
private static final int FORMAT = 1;

View File

@@ -39,8 +39,8 @@ public enum MPMarshalFormat {
}
@Override
public String fileExtension() {
return "mpsites";
public String fileSuffix() {
return ".mpsites";
}
},
@@ -59,8 +59,8 @@ public enum MPMarshalFormat {
}
@Override
public String fileExtension() {
return "mpsites.json";
public String fileSuffix() {
return ".mpsites.json";
}
};
@@ -70,5 +70,6 @@ public enum MPMarshalFormat {
public abstract MPUnmarshaller unmarshaller();
public abstract String fileExtension();
@SuppressWarnings("MethodReturnAlwaysConstant")
public abstract String fileSuffix();
}

View File

@@ -33,14 +33,15 @@ public interface MPMarshaller {
throws MPInvalidatedException, MPMarshalException;
enum ContentMode {
PROTECTED( "Export of site names and stored passwords (unless device-private) encrypted with the master key." ),
VISIBLE( "Export of site names and passwords in clear-text." );
PROTECTED( "Export of site names and stored passwords (unless device-private) encrypted with the master key.", true ),
VISIBLE( "Export of site names and passwords in clear-text.", false );
private final String description;
private boolean redacted;
private final boolean redacted;
ContentMode(final String description) {
ContentMode(final String description, final boolean redacted) {
this.description = description;
this.redacted = redacted;
}
public String description() {

View File

@@ -45,7 +45,7 @@ public abstract class MPUser<S extends MPSite> {
@Nonnull
public MPMasterKey getMasterKey() {
return Preconditions.checkNotNull( key, "User is not authenticated: " + getFullName() );
return Preconditions.checkNotNull( key, "User is not authenticated: %s", getFullName() );
}
public String exportKeyID()

View File

@@ -29,11 +29,6 @@ import java.util.SortedSet;
public abstract class MPUserManager {
private final Map<String, MPFileUser> usersByName = Maps.newHashMap();
static MPUserManager instance;
public static MPUserManager get() {
return instance;
}
protected MPUserManager(final Iterable<MPFileUser> users) {
for (final MPFileUser user : users)

View File

@@ -24,8 +24,8 @@ import com.google.common.primitives.UnsignedInteger;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.ConversionUtils;
import java.io.IOException;
import java.util.Deque;
import java.util.List;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;
import javax.xml.parsers.*;
import org.xml.sax.Attributes;
@@ -56,7 +56,8 @@ public class MPTestSuite implements Callable<Boolean> {
try {
tests = new MPTests();
tests.cases = Lists.newLinkedList();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources( "." );
parser.parse( Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ), new DefaultHandler2() {
private final Deque<String> currentTags = Lists.newLinkedList();
private final Deque<StringBuilder> currentTexts = Lists.newLinkedList();
@@ -81,7 +82,7 @@ public class MPTestSuite implements Callable<Boolean> {
throws SAXException {
super.endElement( uri, localName, qName );
Preconditions.checkState( qName.equals( currentTags.pop() ) );
String text = currentTexts.pop().toString();
String text = Preconditions.checkNotNull( currentTexts.pop() ).toString();
if ("case".equals( qName ))
tests.cases.add( currentCase );
@@ -112,11 +113,11 @@ public class MPTestSuite implements Callable<Boolean> {
throws SAXException {
super.characters( ch, start, length );
currentTexts.peek().append( ch, start, length );
Preconditions.checkNotNull( currentTexts.peek() ).append( ch, start, length );
}
} );
}
catch (IllegalArgumentException | ParserConfigurationException | SAXException | IOException e) {
catch (final IllegalArgumentException | ParserConfigurationException | SAXException | IOException e) {
throw new UnavailableException( e );
}

View File

@@ -28,6 +28,7 @@ import com.lyndir.lhunath.opal.system.util.NNSupplier;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlTransient;
/**
@@ -66,20 +67,23 @@ public class MPTests {
public static class Case {
String identifier;
String parent;
Integer algorithm;
String fullName;
String masterPassword;
String keyID;
String siteName;
String identifier;
String parent;
@Nullable
Integer algorithm;
String fullName;
String masterPassword;
String keyID;
String siteName;
@Nullable
UnsignedInteger siteCounter;
String resultType;
String keyPurpose;
String keyContext;
String result;
String resultType;
String keyPurpose;
String keyContext;
String result;
private transient Case parentCase;
@XmlTransient
private Case parentCase;
public void initializeParentHierarchy(final MPTests tests) {

View File

@@ -108,7 +108,7 @@ public class MPMasterKeyTest {
char[] masterPassword = testCase.getMasterPassword().toCharArray();
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), masterPassword );
String password = randomString( 8 );
String password = randomString( 8 );
MPResultType resultType = MPResultType.StoredPersonal;
for (final MPMasterKey.Version version : MPMasterKey.Version.values()) {
MPAlgorithm algorithm = version.getAlgorithm();
@@ -126,7 +126,7 @@ public class MPMasterKeyTest {
}
}
public static String randomString(int length) {
private static String randomString(int length) {
Random random = new Random();
StringBuilder builder = new StringBuilder();