diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPConstant.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPConstant.java index 25a027fa..47690412 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPConstant.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPConstant.java @@ -10,7 +10,7 @@ import java.nio.charset.Charset; /** * @author lhunath, 2016-10-29 */ -public class MPConstant { +public final class MPConstant { /* Environment */ @@ -58,17 +58,24 @@ public class MPConstant { /** * mpw: Input character encoding. */ - public static final Charset mpw_charset = Charsets.UTF_8; + public static final Charset mpw_charset = Charsets.UTF_8; /** * mpw: Platform-agnostic byte order. */ - public static final ByteOrder mpw_byteOrder = ByteOrder.BIG_ENDIAN; + public static final ByteOrder mpw_byteOrder = ByteOrder.BIG_ENDIAN; /** * mpw: Site digest. */ - public static final MessageAuthenticationDigests mpw_digest = MessageAuthenticationDigests.HmacSHA256; + public static final MessageAuthenticationDigests mpw_digest = MessageAuthenticationDigests.HmacSHA256; /** * mpw: Key ID hash. */ - public static final MessageDigests mpw_hash = MessageDigests.SHA256; + public static final MessageDigests mpw_hash = MessageDigests.SHA256; + /** + * mpw: validity for the time-based rolling counter. + */ + public static final int mpw_counter_timeout = 5 * 60 /* s */; + + + public static final int MS_PER_S = 1000; } diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java index 41894b67..8375d376 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java @@ -19,12 +19,12 @@ public class MPIdenticon { private static final Logger logger = Logger.get( MPIdenticon.class ); private static final Charset charset = Charsets.UTF_8; - private static final Color[] colors = new Color[]{ + private static final Color[] colors = { Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE, Color.MAGENTA, Color.CYAN, Color.MONO }; - private static final char[] leftArm = new char[]{ '╔', '╚', '╰', '═' }; - private static final char[] rightArm = new char[]{ '╗', '╝', '╯', '═' }; - private static final char[] body = new char[]{ '█', '░', '▒', '▓', '☺', '☻' }; - private static final char[] accessory = new char[]{ + private static final char[] leftArm = { '╔', '╚', '╰', '═' }; + private static final char[] rightArm = { '╗', '╝', '╯', '═' }; + private static final char[] body = { '█', '░', '▒', '▓', '☺', '☻' }; + private static final char[] accessory = { '◈', '◎', '◐', '◑', '◒', '◓', '☀', '☁', '☂', '☃', '☄', '★', '☆', '☎', '☏', '⎈', '⌂', '☘', '☢', '☣', '☕', '⌚', '⌛', '⏰', '⚡', '⛄', '⛅', '☔', '♔', '♕', '♖', '♗', '♘', '♙', '♚', '♛', '♜', '♝', '♞', '♟', '♨', '♩', '♪', '♫', '⚐', '⚑', '⚔', '⚖', '⚙', '⚠', '⌘', '⏎', '✄', '✆', '✈', '✉', '✌' }; @@ -33,11 +33,12 @@ public class MPIdenticon { private final Color color; private final String text; - public MPIdenticon(String fullName, String masterPassword) { + public MPIdenticon(final String fullName, final String masterPassword) { this( fullName, masterPassword.toCharArray() ); } - public MPIdenticon(String fullName, char[] masterPassword) { + @SuppressWarnings("MethodCanBeVariableArityMethod") + public MPIdenticon(final String fullName, final char[] masterPassword) { this.fullName = fullName; byte[] masterPasswordBytes = charset.encode( CharBuffer.wrap( masterPassword ) ).array(); diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteType.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteType.java index f02ff141..ea4794a2 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteType.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteType.java @@ -3,10 +3,10 @@ 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.List; -import java.util.Set; +import java.util.*; import javax.annotation.Nullable; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NonNls; /** @@ -17,12 +17,12 @@ import org.jetbrains.annotations.Contract; public enum MPSiteType { GeneratedMaximum( "Max", "20 characters, contains symbols.", // - ImmutableList.of( "x", "max", "maximum" ), // + ImmutableList.of( "x", "max", "maximum" ), // NON-NLS ImmutableList.of( new MPTemplate( "anoxxxxxxxxxxxxxxxxx" ), new MPTemplate( "axxxxxxxxxxxxxxxxxno" ) ), // MPSiteTypeClass.Generated, 0x0 ), GeneratedLong( "Long", "Copy-friendly, 14 characters, contains symbols.", // - ImmutableList.of( "l", "long" ), // + ImmutableList.of( "l", "long" ), // NON-NLS ImmutableList.of( new MPTemplate( "CvcvnoCvcvCvcv" ), new MPTemplate( "CvcvCvcvnoCvcv" ), new MPTemplate( "CvcvCvcvCvcvno" ), new MPTemplate( "CvccnoCvcvCvcv" ), new MPTemplate( "CvccCvcvnoCvcv" ), new MPTemplate( "CvccCvcvCvcvno" ), @@ -37,43 +37,43 @@ public enum MPSiteType { MPSiteTypeClass.Generated, 0x1 ), GeneratedMedium( "Medium", "Copy-friendly, 8 characters, contains symbols.", // - ImmutableList.of( "m", "med", "medium" ), // + ImmutableList.of( "m", "med", "medium" ), // NON-NLS ImmutableList.of( new MPTemplate( "CvcnoCvc" ), new MPTemplate( "CvcCvcno" ) ), // MPSiteTypeClass.Generated, 0x2 ), GeneratedBasic( "Basic", "8 characters, no symbols.", // - ImmutableList.of( "b", "basic" ), // + ImmutableList.of( "b", "basic" ), // NON-NLS ImmutableList.of( new MPTemplate( "aaanaaan" ), new MPTemplate( "aannaaan" ), new MPTemplate( "aaannaaa" ) ), // MPSiteTypeClass.Generated, 0x3 ), GeneratedShort( "Short", "Copy-friendly, 4 characters, no symbols.", // - ImmutableList.of( "s", "short" ), // + ImmutableList.of( "s", "short" ), // NON-NLS ImmutableList.of( new MPTemplate( "Cvcn" ) ), // MPSiteTypeClass.Generated, 0x4 ), GeneratedPIN( "PIN", "4 numbers.", // - ImmutableList.of( "i", "pin" ), // + ImmutableList.of( "i", "pin" ), // NON-NLS ImmutableList.of( new MPTemplate( "nnnn" ) ), // MPSiteTypeClass.Generated, 0x5 ), GeneratedName( "Name", "9 letter name.", // - ImmutableList.of( "n", "name" ), // + ImmutableList.of( "n", "name" ), // NON-NLS ImmutableList.of( new MPTemplate( "cvccvcvcv" ) ), // MPSiteTypeClass.Generated, 0xE ), GeneratedPhrase( "Phrase", "20 character sentence.", // - ImmutableList.of( "p", "phrase" ), // + ImmutableList.of( "p", "phrase" ), // NON-NLS ImmutableList.of( new MPTemplate( "cvcc cvc cvccvcv cvc" ), new MPTemplate( "cvc cvccvcvcv cvcv" ), new MPTemplate( "cv cvccv cvc cvcvccv" ) ), // MPSiteTypeClass.Generated, 0xF ), StoredPersonal( "Personal", "AES-encrypted, exportable.", // - ImmutableList.of( "personal" ), // + ImmutableList.of( "personal" ), // NON-NLS ImmutableList.of(), // MPSiteTypeClass.Stored, 0x0, MPSiteFeature.ExportContent ), StoredDevicePrivate( "Device", "AES-encrypted, not exported.", // - ImmutableList.of( "device" ), // + ImmutableList.of( "device" ), // NON-NLS ImmutableList.of(), // MPSiteTypeClass.Stored, 0x1, MPSiteFeature.DevicePrivate ); @@ -129,7 +129,7 @@ public enum MPSiteType { public int getType() { int mask = typeIndex | typeClass.getMask(); - for (MPSiteFeature typeFeature : typeFeatures) + for (final MPSiteFeature typeFeature : typeFeatures) mask |= typeFeature.getMask(); return mask; @@ -143,7 +143,7 @@ public enum MPSiteType { public static MPSiteType forOption(final String option) { for (final MPSiteType type : values()) - if (type.getOptions().contains( option.toLowerCase() )) + if (type.getOptions().contains( option.toLowerCase( Locale.ROOT ) )) return type; throw logger.bug( "No type for option: %s", option ); @@ -154,7 +154,7 @@ public enum MPSiteType { * * @return The type registered with the given name. */ - @Contract("!null -> !null, null -> null") + @Contract("!null -> !null") public static MPSiteType forName(@Nullable final String name) { if (name == null) @@ -189,7 +189,7 @@ public enum MPSiteType { */ public static MPSiteType forType(final int type) { - for (MPSiteType siteType : values()) + for (final MPSiteType siteType : values()) if (siteType.getType() == type) return siteType; @@ -205,7 +205,7 @@ public enum MPSiteType { int typeMask = mask & ~0xF; ImmutableList.Builder types = ImmutableList.builder(); - for (MPSiteType siteType : values()) + for (final MPSiteType siteType : values()) if (((siteType.getType() & ~0xF) & typeMask) != 0) types.add( siteType ); diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteVariant.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteVariant.java index 23c3e463..8a93f2fe 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteVariant.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPSiteVariant.java @@ -3,8 +3,10 @@ package com.lyndir.masterpassword; import com.google.common.collect.ImmutableList; import com.lyndir.lhunath.opal.system.logging.Logger; import java.util.List; +import java.util.Locale; import javax.annotation.Nullable; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NonNls; /** @@ -12,11 +14,11 @@ import org.jetbrains.annotations.Contract; */ public enum MPSiteVariant { Password( "Generate a key for authentication.", "Doesn't currently use a context.", // - ImmutableList.of( "p", "password" ), "com.lyndir.masterpassword" ), + ImmutableList.of( "p", "password" ), "com.lyndir.masterpassword" ), // NON-NLS Login( "Generate a name for identification.", "Doesn't currently use a context.", // - ImmutableList.of( "l", "login" ), "com.lyndir.masterpassword.login" ), + ImmutableList.of( "l", "login" ), "com.lyndir.masterpassword.login" ), // NON-NLS Answer( "Generate an answer to a security question.", "Empty for a universal site answer or\nthe most significant word(s) of the question.", // - ImmutableList.of( "a", "answer" ), "com.lyndir.masterpassword.answer" ); + ImmutableList.of( "a", "answer" ), "com.lyndir.masterpassword.answer" ); // NON-NLS static final Logger logger = Logger.get( MPSiteType.class ); @@ -25,7 +27,7 @@ public enum MPSiteVariant { private final List options; private final String scope; - MPSiteVariant(final String description, final String contextDescription, final List options, final String scope) { + MPSiteVariant(final String description, final String contextDescription, final List options, @NonNls final String scope) { this.contextDescription = contextDescription; this.options = options; @@ -57,7 +59,7 @@ public enum MPSiteVariant { public static MPSiteVariant forOption(final String option) { for (final MPSiteVariant variant : values()) - if (variant.getOptions().contains( option.toLowerCase() )) + if (variant.getOptions().contains( option.toLowerCase( Locale.ROOT ) )) return variant; throw logger.bug( "No variant for option: %s", option ); @@ -67,7 +69,7 @@ public enum MPSiteVariant { * * @return The variant registered with the given name. */ - @Contract("!null -> !null, null -> null") + @Contract("!null -> !null") public static MPSiteVariant forName(@Nullable final String name) { if (name == null) diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplate.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplate.java index b605ccab..2b87f384 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplate.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplate.java @@ -4,7 +4,9 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; import com.google.common.collect.ImmutableList; import com.lyndir.lhunath.opal.system.util.MetaObject; +import java.io.Serializable; import java.util.List; +import org.jetbrains.annotations.NonNls; /** @@ -12,12 +14,14 @@ import java.util.List; * * @author lhunath */ -public class MPTemplate extends MetaObject { +public class MPTemplate extends MetaObject implements Serializable { - private final String templateString; + private static final long serialVersionUID = 1L; + + private final String templateString; private final List template; - MPTemplate(final String templateString) { + MPTemplate(@NonNls final String templateString) { ImmutableList.Builder builder = ImmutableList.builder(); for (int i = 0; i < templateString.length(); ++i) diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplateCharacterClass.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplateCharacterClass.java index 54aa04cb..eedd55fc 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplateCharacterClass.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPTemplateCharacterClass.java @@ -1,6 +1,7 @@ package com.lyndir.masterpassword; import com.lyndir.lhunath.opal.system.logging.Logger; +import org.jetbrains.annotations.NonNls; /** @@ -27,7 +28,7 @@ public enum MPTemplateCharacterClass { private final char identifier; private final char[] characters; - MPTemplateCharacterClass(final char identifier, final String characters) { + MPTemplateCharacterClass(final char identifier, @NonNls final String characters) { this.identifier = identifier; this.characters = characters.toCharArray(); @@ -44,7 +45,7 @@ public enum MPTemplateCharacterClass { } public static MPTemplateCharacterClass forIdentifier(final char identifier) { - for (MPTemplateCharacterClass characterClass : values()) + for (final MPTemplateCharacterClass characterClass : values()) if (characterClass.getIdentifier() == identifier) return characterClass; diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKey.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKey.java index 3d086aec..c3a1c88d 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKey.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKey.java @@ -1,5 +1,7 @@ package com.lyndir.masterpassword; +import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; + import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedInteger; import com.lyndir.lhunath.opal.system.*; @@ -7,7 +9,6 @@ import com.lyndir.lhunath.opal.system.logging.Logger; import java.util.Arrays; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import org.jetbrains.annotations.NotNull; /** @@ -26,13 +27,15 @@ public abstract class MasterKey { @Nullable private byte[] masterKey; + @SuppressWarnings("MethodCanBeVariableArityMethod") public static MasterKey create(final String fullName, final char[] masterPassword) { return create( Version.CURRENT, fullName, masterPassword ); } @Nonnull - public static MasterKey create(Version version, final String fullName, final char[] masterPassword) { + @SuppressWarnings("MethodCanBeVariableArityMethod") + public static MasterKey create(final Version version, final String fullName, final char[] masterPassword) { switch (version) { case V0: @@ -45,7 +48,7 @@ public abstract class MasterKey { return new MasterKeyV3( fullName ).revalidate( masterPassword ); } - throw new UnsupportedOperationException( "Unsupported version: " + version ); + throw new UnsupportedOperationException( strf( "Unsupported version: %s", version ) ); } public static boolean isAllowNativeByDefault() { @@ -65,18 +68,19 @@ public abstract class MasterKey { allowNativeByDefault = allowNative; } - protected MasterKey(@NotNull final String fullName) { + protected MasterKey(@Nonnull final String fullName) { this.fullName = fullName; logger.trc( "fullName: %s", fullName ); } @Nullable - protected abstract byte[] deriveKey(final char[] masterPassword); + @SuppressWarnings("MethodCanBeVariableArityMethod") + protected abstract byte[] deriveKey(char[] masterPassword); public abstract Version getAlgorithmVersion(); - @NotNull + @Nonnull public String getFullName() { return fullName; @@ -103,8 +107,8 @@ public abstract class MasterKey { return idForBytes( getKey() ); } - public abstract String encode(@Nonnull final String siteName, final MPSiteType siteType, @Nonnull final UnsignedInteger siteCounter, - final MPSiteVariant siteVariant, @Nullable final String siteContext); + public abstract String encode(@Nonnull String siteName, MPSiteType siteType, @Nonnull UnsignedInteger siteCounter, + MPSiteVariant siteVariant, @Nullable String siteContext); public boolean isValid() { return masterKey != null; @@ -118,6 +122,7 @@ public abstract class MasterKey { } } + @SuppressWarnings("MethodCanBeVariableArityMethod") public MasterKey revalidate(final char[] masterPassword) { invalidate(); @@ -127,19 +132,19 @@ public abstract class MasterKey { masterKey = deriveKey( masterPassword ); if (masterKey == null) - logger.dbg( "masterKey calculation failed after %.2fs.", (System.currentTimeMillis() - start) / 1000D ); + logger.dbg( "masterKey calculation failed after %.2fs.", (double)(System.currentTimeMillis() - start) / MPConstant.MS_PER_S ); else logger.trc( "masterKey ID: %s (derived in %.2fs)", CodeUtils.encodeHex( idForBytes( masterKey ) ), - (System.currentTimeMillis() - start) / 1000D ); + (double)(System.currentTimeMillis() - start) / MPConstant.MS_PER_S ); return this; } - protected abstract byte[] bytesForInt(final int number); + protected abstract byte[] bytesForInt(int number); - protected abstract byte[] bytesForInt(@Nonnull final UnsignedInteger number); + protected abstract byte[] bytesForInt(@Nonnull UnsignedInteger number); - protected abstract byte[] idForBytes(final byte[] bytes); + protected abstract byte[] idForBytes(byte[] bytes); public enum Version { /** @@ -190,7 +195,7 @@ public abstract class MasterKey { return "2.2"; } - throw new UnsupportedOperationException( "Unsupported version: " + this ); + throw new UnsupportedOperationException( strf( "Unsupported version: %s", this ) ); } } } diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV0.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV0.java index 3805efd2..201365cf 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV0.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV0.java @@ -58,6 +58,7 @@ public class MasterKeyV0 extends MasterKey { return scrypt( masterKeySalt, mpBytes ); } + @Nullable protected byte[] scrypt(final byte[] masterKeySalt, final byte[] mpBytes) { try { if (isAllowNative()) @@ -65,7 +66,7 @@ public class MasterKeyV0 extends MasterKey { else return SCrypt.scryptJ( mpBytes, masterKeySalt, MPConstant.scrypt_N, MPConstant.scrypt_r, MPConstant.scrypt_p, MPConstant.mpw_dkLen ); } - catch (GeneralSecurityException e) { + catch (final GeneralSecurityException e) { logger.bug( e ); return null; } @@ -86,18 +87,18 @@ public class MasterKeyV0 extends MasterKey { logger.trc( "siteType: %d (%s)", siteType.ordinal(), siteType ); if (siteCounter.longValue() == 0) - siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (300 * 1000)) * 300 ); + siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (MPConstant.mpw_counter_timeout * 1000)) * MPConstant.mpw_counter_timeout ); String siteScope = siteVariant.getScope(); byte[] siteNameBytes = siteName.getBytes( MPConstant.mpw_charset ); byte[] siteNameLengthBytes = bytesForInt( siteName.length() ); byte[] siteCounterBytes = bytesForInt( siteCounter ); - byte[] siteContextBytes = siteContext == null || siteContext.isEmpty()? null: siteContext.getBytes( MPConstant.mpw_charset ); - byte[] siteContextLengthBytes = bytesForInt( siteContextBytes == null? 0: siteContextBytes.length ); - logger.trc( "site scope: %s, context: %s", siteScope, siteContextBytes == null? "": siteContext ); + byte[] siteContextBytes = ((siteContext == null) || siteContext.isEmpty())? null: siteContext.getBytes( MPConstant.mpw_charset ); + byte[] siteContextLengthBytes = bytesForInt( (siteContextBytes == null)? 0: siteContextBytes.length ); + logger.trc( "site scope: %s, context: %s", siteScope, (siteContextBytes == null)? "": siteContext ); logger.trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)", siteScope, CodeUtils.encodeHex( siteNameLengthBytes ), siteName, CodeUtils.encodeHex( siteCounterBytes ), CodeUtils.encodeHex( siteContextLengthBytes ), - siteContextBytes == null? "(null)": siteContext ); + (siteContextBytes == null)? "(null)": siteContext ); byte[] sitePasswordInfo = Bytes.concat( siteScope.getBytes( MPConstant.mpw_charset ), siteNameLengthBytes, siteNameBytes, siteCounterBytes ); if (siteContextBytes != null) @@ -108,7 +109,7 @@ public class MasterKeyV0 extends MasterKey { int[] sitePasswordSeed = new int[sitePasswordSeedBytes.length]; for (int i = 0; i < sitePasswordSeedBytes.length; ++i) { ByteBuffer buf = ByteBuffer.allocate( Integer.SIZE / Byte.SIZE ).order( ByteOrder.BIG_ENDIAN ); - Arrays.fill( buf.array(), sitePasswordSeedBytes[i] > 0? (byte) 0x00: (byte) 0xFF ); + Arrays.fill( buf.array(), (byte) ((sitePasswordSeedBytes[i] > 0)? 0x00: 0xFF) ); buf.position( 2 ); buf.put( sitePasswordSeedBytes[i] ).rewind(); sitePasswordSeed[i] = buf.getInt() & 0xFFFF; diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV1.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV1.java index 9e68cb52..da88f4e2 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV1.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV1.java @@ -43,18 +43,18 @@ public class MasterKeyV1 extends MasterKeyV0 { logger.trc( "siteType: %d (%s)", siteType.ordinal(), siteType ); if (siteCounter.longValue() == 0) - siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (300 * 1000)) * 300 ); + siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (MPConstant.mpw_counter_timeout * 1000)) * MPConstant.mpw_counter_timeout ); String siteScope = siteVariant.getScope(); byte[] siteNameBytes = siteName.getBytes( MPConstant.mpw_charset ); byte[] siteNameLengthBytes = bytesForInt( siteName.length() ); byte[] siteCounterBytes = bytesForInt( siteCounter ); - byte[] siteContextBytes = siteContext == null || siteContext.isEmpty()? null: siteContext.getBytes( MPConstant.mpw_charset ); - byte[] siteContextLengthBytes = bytesForInt( siteContextBytes == null? 0: siteContextBytes.length ); - logger.trc( "site scope: %s, context: %s", siteScope, siteContextBytes == null? "": siteContext ); + byte[] siteContextBytes = ((siteContext == null) || siteContext.isEmpty())? null: siteContext.getBytes( MPConstant.mpw_charset ); + byte[] siteContextLengthBytes = bytesForInt( (siteContextBytes == null)? 0: siteContextBytes.length ); + logger.trc( "site scope: %s, context: %s", siteScope, (siteContextBytes == null)? "": siteContext ); logger.trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)", siteScope, CodeUtils.encodeHex( siteNameLengthBytes ), siteName, CodeUtils.encodeHex( siteCounterBytes ), CodeUtils.encodeHex( siteContextLengthBytes ), - siteContextBytes == null? "(null)": siteContext ); + (siteContextBytes == null)? "(null)": siteContext ); byte[] sitePasswordInfo = Bytes.concat( siteScope.getBytes( MPConstant.mpw_charset ), siteNameLengthBytes, siteNameBytes, siteCounterBytes ); if (siteContextBytes != null) diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV2.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV2.java index 5761d34f..c2156f16 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV2.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MasterKeyV2.java @@ -42,18 +42,18 @@ public class MasterKeyV2 extends MasterKeyV1 { logger.trc( "siteType: %d (%s)", siteType.ordinal(), siteType ); if (siteCounter.longValue() == 0) - siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (300 * 1000)) * 300 ); + siteCounter = UnsignedInteger.valueOf( (System.currentTimeMillis() / (MPConstant.mpw_counter_timeout * 1000)) * MPConstant.mpw_counter_timeout ); String siteScope = siteVariant.getScope(); byte[] siteNameBytes = siteName.getBytes( MPConstant.mpw_charset ); byte[] siteNameLengthBytes = bytesForInt( siteNameBytes.length ); byte[] siteCounterBytes = bytesForInt( siteCounter ); - byte[] siteContextBytes = siteContext == null || siteContext.isEmpty()? null: siteContext.getBytes( MPConstant.mpw_charset ); - byte[] siteContextLengthBytes = bytesForInt( siteContextBytes == null? 0: siteContextBytes.length ); - logger.trc( "site scope: %s, context: %s", siteScope, siteContextBytes == null? "": siteContext ); + byte[] siteContextBytes = ((siteContext == null) || siteContext.isEmpty())? null: siteContext.getBytes( MPConstant.mpw_charset ); + byte[] siteContextLengthBytes = bytesForInt( (siteContextBytes == null)? 0: siteContextBytes.length ); + logger.trc( "site scope: %s, context: %s", siteScope, (siteContextBytes == null)? "": siteContext ); logger.trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)", siteScope, CodeUtils.encodeHex( siteNameLengthBytes ), siteName, CodeUtils.encodeHex( siteCounterBytes ), CodeUtils.encodeHex( siteContextLengthBytes ), - siteContextBytes == null? "(null)": siteContext ); + (siteContextBytes == null)? "(null)": siteContext ); byte[] sitePasswordInfo = Bytes.concat( siteScope.getBytes( MPConstant.mpw_charset ), siteNameLengthBytes, siteNameBytes, siteCounterBytes ); if (siteContextBytes != null) diff --git a/core/java/model/build.gradle b/core/java/model/build.gradle index 190a6ff0..9c488d26 100644 --- a/core/java/model/build.gradle +++ b/core/java/model/build.gradle @@ -6,7 +6,7 @@ plugins { description = 'Master Password Site Model' dependencies { - compile project(':masterpassword:algorithm') + compile project(':masterpassword-algorithm') compile group: 'joda-time', name: 'joda-time', version:'2.4' compileOnly group: 'com.google.auto.value', name: 'auto-value', version: '1.2' diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java index 9c8ce291..e23a03fe 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java @@ -128,7 +128,7 @@ public class MPSite { @Override public boolean equals(final Object obj) { - return this == obj || obj instanceof MPSite && Objects.equals( siteName, ((MPSite) obj).siteName ); + return (this == obj) || ((obj instanceof MPSite) && Objects.equals( siteName, ((MPSite) obj).siteName )); } @Override diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java index 9f32ca83..6c109004 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java @@ -26,7 +26,7 @@ public class MPSiteMarshaller { public static MPSiteMarshaller marshallSafe(final MPUser user) { MPSiteMarshaller marshaller = new MPSiteMarshaller(); marshaller.marshallHeaderForSafeContent( user ); - for (MPSite site : user.getSites()) + for (final MPSite site : user.getSites()) marshaller.marshallSite( site ); return marshaller; @@ -35,7 +35,7 @@ public class MPSiteMarshaller { public static MPSiteMarshaller marshallVisible(final MPUser user, final MasterKey masterKey) { MPSiteMarshaller marshaller = new MPSiteMarshaller(); marshaller.marshallHeaderForVisibleContentWithKey( user, masterKey ); - for (MPSite site : user.getSites()) + for (final MPSite site : user.getSites()) marshaller.marshallSite( site ); return marshaller; @@ -77,7 +77,7 @@ public class MPSiteMarshaller { return header.toString(); } - public String marshallSite(MPSite site) { + public String marshallSite(final MPSite site) { String exportLine = strf( "%s %8d %8s %25s\t%25s\t%s", // rfc3339.print( site.getLastUsed() ), // lastUsed site.getUses(), // uses @@ -126,6 +126,6 @@ public class MPSiteMarshaller { return description; } - public abstract String contentForSite(final MPSite site, final MasterKey masterKey); + public abstract String contentForSite(MPSite site, MasterKey masterKey); } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java index 50096d83..418caa4c 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java @@ -22,7 +22,7 @@ public class MPSiteResult { @Override public boolean equals(final Object obj) { - return this == obj || obj instanceof MPSiteResult && Objects.equals( site, ((MPSiteResult) obj).site ); + return (this == obj) || ((obj instanceof MPSiteResult) && Objects.equals( site, ((MPSiteResult) obj).site )); } @Override diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteUnmarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteUnmarshaller.java index 6e0e5edc..c0dbc4bc 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteUnmarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteUnmarshaller.java @@ -2,6 +2,7 @@ package com.lyndir.masterpassword.model; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; +import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.io.CharStreams; @@ -31,26 +32,28 @@ public class MPSiteUnmarshaller { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MPSite.class ); private static final DateTimeFormatter rfc3339 = ISODateTimeFormat.dateTimeNoMillis(); - private static final Pattern[] unmarshallFormats = new Pattern[]{ + private static final Pattern[] unmarshallFormats = { Pattern.compile( "^([^ ]+) +(\\d+) +(\\d+)(:\\d+)? +([^\t]+)\t(.*)" ), Pattern.compile( "^([^ ]+) +(\\d+) +(\\d+)(:\\d+)?(:\\d+)? +([^\t]*)\t *([^\t]+)\t(.*)" ) }; private static final Pattern headerFormat = Pattern.compile( "^#\\s*([^:]+): (.*)" ); private final int importFormat; + @SuppressWarnings({ "FieldCanBeLocal", "unused" }) private final int mpVersion; + @SuppressWarnings({ "FieldCanBeLocal", "unused" }) private final boolean clearContent; private final MPUser user; @Nonnull - public static MPSiteUnmarshaller unmarshall(@Nonnull File file) + public static MPSiteUnmarshaller unmarshall(@Nonnull final File file) throws IOException { - try (Reader reader = new FileReader( file )) { + try (Reader reader = new InputStreamReader( new FileInputStream( file ), Charsets.UTF_8 )) { return unmarshall( CharStreams.readLines( reader ) ); } } @Nonnull - public static MPSiteUnmarshaller unmarshall(@Nonnull List lines) { + public static MPSiteUnmarshaller unmarshall(@Nonnull final List lines) { byte[] keyID = null; String fullName = null; int mpVersion = 0, importFormat = 0, avatar = 0; @@ -59,7 +62,7 @@ public class MPSiteUnmarshaller { MPSiteUnmarshaller marshaller = null; final ImmutableList.Builder sites = ImmutableList.builder(); - for (String line : lines) + for (final String line : lines) // Header delimitor. if (line.startsWith( "##" )) if (!headerStarted) @@ -71,7 +74,7 @@ public class MPSiteUnmarshaller { // Comment. else if (line.startsWith( "#" )) { - if (headerStarted && marshaller == null) { + if (headerStarted && (marshaller == null)) { // In header. Matcher headerMatcher = headerFormat.matcher( line ); if (headerMatcher.matches()) { @@ -87,7 +90,7 @@ public class MPSiteUnmarshaller { else if ("Avatar".equalsIgnoreCase( name )) avatar = ConversionUtils.toIntegerNN( value ); else if ("Passwords".equalsIgnoreCase( name )) - clearContent = value.equalsIgnoreCase( "visible" ); + clearContent = "visible".equalsIgnoreCase( value ); else if ("Default Type".equalsIgnoreCase( name )) defaultType = MPSiteType.forType( ConversionUtils.toIntegerNN( value ) ); } @@ -116,7 +119,7 @@ public class MPSiteUnmarshaller { } @Nullable - public MPSite unmarshallSite(@Nonnull String siteLine) { + public MPSite unmarshallSite(@Nonnull final String siteLine) { Matcher siteMatcher = unmarshallFormats[importFormat].matcher( siteLine ); if (!siteMatcher.matches()) return null; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java index 95348621..e75806e2 100755 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java @@ -39,16 +39,16 @@ public class MPUser implements Comparable { public MPUser(final String fullName, @Nullable final byte[] keyID, final MasterKey.Version algorithmVersion, final int avatar, final MPSiteType defaultType, final ReadableInstant lastUsed) { this.fullName = fullName; - this.keyID = keyID; + this.keyID = (keyID == null)? null: keyID.clone(); this.algorithmVersion = algorithmVersion; this.avatar = avatar; this.defaultType = defaultType; this.lastUsed = lastUsed; } - public Collection findSitesByName(String query) { + public Collection findSitesByName(final String query) { ImmutableList.Builder results = ImmutableList.builder(); - for (MPSite site : getSites()) + for (final MPSite site : getSites()) if (site.getSiteName().startsWith( query )) results.add( new MPSiteResult( site ) ); @@ -87,10 +87,11 @@ public class MPUser implements Comparable { * @throws IncorrectMasterPasswordException If authentication fails due to the given master password not matching the user's keyID. */ @Nonnull + @SuppressWarnings("MethodCanBeVariableArityMethod") public MasterKey authenticate(final char[] masterPassword) throws IncorrectMasterPasswordException { MasterKey masterKey = MasterKey.create( algorithmVersion, getFullName(), masterPassword ); - if (keyID == null || keyID.length == 0) + if ((keyID == null) || (keyID.length == 0)) keyID = masterKey.getKeyID(); else if (!Arrays.equals( masterKey.getKeyID(), keyID )) throw new IncorrectMasterPasswordException( this ); @@ -119,7 +120,7 @@ public class MPUser implements Comparable { } public void updateLastUsed() { - this.lastUsed = new Instant(); + lastUsed = new Instant(); } public Iterable getSites() { @@ -128,7 +129,7 @@ public class MPUser implements Comparable { @Override public boolean equals(final Object obj) { - return this == obj || obj instanceof MPUser && Objects.equals( fullName, ((MPUser) obj).fullName ); + return (this == obj) || ((obj instanceof MPUser) && Objects.equals( fullName, ((MPUser) obj).fullName )); } @Override diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserFileManager.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserFileManager.java index 42904e5b..09ce273e 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserFileManager.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserFileManager.java @@ -59,7 +59,7 @@ public class MPUserFileManager extends MPUserManager { try { return MPSiteUnmarshaller.unmarshall( Preconditions.checkNotNull( file ) ).getUser(); } - catch (IOException e) { + catch (final IOException e) { logger.err( e, "Couldn't read user from: %s", file ); return null; } @@ -99,16 +99,17 @@ public class MPUserFileManager extends MPUserManager { @Override public Writer openStream() throws IOException { - return new FileWriter( new File( userFilesDirectory, user.getFullName() + ".mpsites" ) ); + File mpsitesFile = new File( userFilesDirectory, user.getFullName() + ".mpsites" ); + return new OutputStreamWriter( new FileOutputStream( mpsitesFile ), Charsets.UTF_8 ); } }.write( MPSiteMarshaller.marshallSafe( user ).getExport() ); } - catch (IOException e) { + catch (final IOException e) { logger.err( e, "Unable to save sites for user: %s", user ); } // Remove deleted users. - for (File userFile : listUserFiles( userFilesDirectory )) + for (final File userFile : listUserFiles( userFilesDirectory )) if (getUserNamed( userFile.getName().replaceFirst( "\\.mpsites$", "" ) ) == null) if (!userFile.delete()) logger.err( "Couldn't delete file: %s", userFile ); diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java index 246be31e..428ce766 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java @@ -17,7 +17,7 @@ public abstract class MPUserManager { } protected MPUserManager(final Iterable users) { - for (MPUser user : users) + for (final MPUser user : users) usersByName.put( user.getFullName(), user ); } @@ -25,7 +25,7 @@ public abstract class MPUserManager { return FluentIterable.from( usersByName.values() ).toSortedSet( Ordering.natural() ); } - public MPUser getUserNamed(String fullName) { + public MPUser getUserNamed(final String fullName) { return usersByName.get( fullName ); } diff --git a/core/java/tests/build.gradle b/core/java/tests/build.gradle index 317c4824..aae837af 100644 --- a/core/java/tests/build.gradle +++ b/core/java/tests/build.gradle @@ -5,7 +5,7 @@ plugins { description = 'Master Password Test Suite' dependencies { - compile project(':masterpassword:algorithm') + compile project(':masterpassword-algorithm') testCompile group: 'org.testng', name: 'testng', version:'6.8.5' testCompile group: 'ch.qos.logback', name: 'logback-classic', version:'1.1.2' diff --git a/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTestSuite.java b/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTestSuite.java index d6bca428..c624061d 100644 --- a/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTestSuite.java +++ b/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTestSuite.java @@ -20,29 +20,30 @@ import org.xml.sax.ext.DefaultHandler2; /** * @author lhunath, 2015-12-22 */ +@SuppressWarnings("HardCodedStringLiteral") public class MPTestSuite implements Callable { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MPTestSuite.class ); private static final String DEFAULT_RESOURCE_NAME = "mpw_tests.xml"; - private MPTests tests; - private Listener listener; + private final MPTests tests; + private Listener listener; public MPTestSuite() throws UnavailableException { this( DEFAULT_RESOURCE_NAME ); } - public MPTestSuite(String resourceName) + public MPTestSuite(final String resourceName) throws UnavailableException { try { tests = new MPTests(); tests.cases = Lists.newLinkedList(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse( Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ), new DefaultHandler2() { - private Deque currentTags = Lists.newLinkedList(); - private Deque currentTexts = Lists.newLinkedList(); + private final Deque currentTags = Lists.newLinkedList(); + private final Deque currentTexts = Lists.newLinkedList(); private MPTests.Case currentCase; @Override @@ -103,7 +104,7 @@ public class MPTestSuite implements Callable { throw new UnavailableException( e ); } - for (MPTests.Case testCase : tests.getCases()) + for (final MPTests.Case testCase : tests.getCases()) testCase.initializeParentHierarchy( tests ); } @@ -115,7 +116,7 @@ public class MPTestSuite implements Callable { return tests; } - public boolean forEach(String testName, NNFunctionNN testFunction) { + public boolean forEach(final String testName, final NNFunctionNN testFunction) { List cases = tests.getCases(); for (int c = 0; c < cases.size(); c++) { MPTests.Case testCase = cases.get( c ); @@ -164,6 +165,8 @@ public class MPTestSuite implements Callable { public static class UnavailableException extends Exception { + private static final long serialVersionUID = 1L; + public UnavailableException(final Throwable cause) { super( cause ); } diff --git a/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTests.java b/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTests.java index dcc1091d..509f3547 100644 --- a/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTests.java +++ b/core/java/tests/src/main/java/com/lyndir/masterpassword/MPTests.java @@ -2,6 +2,7 @@ package com.lyndir.masterpassword; import static com.google.common.base.Preconditions.checkNotNull; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; +import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; import com.google.common.primitives.UnsignedInteger; import com.lyndir.lhunath.opal.system.logging.Logger; @@ -28,20 +29,20 @@ public class MPTests { return checkNotNull( cases ); } - public Case getCase(String identifier) { - for (Case testCase : getCases()) + public Case getCase(final String identifier) { + for (final Case testCase : getCases()) if (identifier.equals( testCase.getIdentifier() )) return testCase; - throw new IllegalArgumentException( "No case for identifier: " + identifier ); + throw new IllegalArgumentException( strf( "No case for identifier: %s", identifier ) ); } public Case getDefaultCase() { try { return getCase( ID_DEFAULT ); } - catch (IllegalArgumentException e) { - throw new IllegalStateException( "Missing default case in test suite. Add a case with id: " + ID_DEFAULT, e ); + catch (final IllegalArgumentException e) { + throw new IllegalStateException( strf( "Missing default case in test suite. Add a case with id: %d", ID_DEFAULT ), e ); } } @@ -62,7 +63,7 @@ public class MPTests { private transient Case parentCase; - public void initializeParentHierarchy(MPTests tests) { + public void initializeParentHierarchy(final MPTests tests) { if (parent != null) { parentCase = tests.getCase( parent ); @@ -129,14 +130,14 @@ public class MPTests { @Nonnull @Override public String get() { - return parentCase == null? "": checkNotNull( parentCase.siteContext ); + return (parentCase == null)? "": checkNotNull( parentCase.siteContext ); } } ); result = ifNotNullElse( result, new NNSupplier() { @Nonnull @Override public String get() { - return parentCase == null? "": checkNotNull( parentCase.result ); + return (parentCase == null)? "": checkNotNull( parentCase.result ); } } ); } diff --git a/core/java/tests/src/test/java/com/lyndir/masterpassword/MasterKeyTest.java b/core/java/tests/src/test/java/com/lyndir/masterpassword/MasterKeyTest.java index 1966aefd..cce7a590 100644 --- a/core/java/tests/src/test/java/com/lyndir/masterpassword/MasterKeyTest.java +++ b/core/java/tests/src/test/java/com/lyndir/masterpassword/MasterKeyTest.java @@ -6,6 +6,7 @@ import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.lhunath.opal.system.util.NNFunctionNN; import javax.annotation.Nonnull; +import org.jetbrains.annotations.NonNls; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -15,6 +16,7 @@ public class MasterKeyTest { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MasterKeyTest.class ); + @NonNls private MPTestSuite testSuite; @BeforeMethod @@ -86,7 +88,7 @@ public class MasterKeyTest { fail( "[testInvalidate] Master key should have been invalidated, but was still usable." ); } - catch (IllegalStateException ignored) { + catch (final IllegalStateException ignored) { } } } diff --git a/gradle/.idea/codeStyleSettings.xml b/gradle/.idea/codeStyleSettings.xml new file mode 100644 index 00000000..6411f7cf --- /dev/null +++ b/gradle/.idea/codeStyleSettings.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/gradle/.idea/copyright/GPLv3.xml b/gradle/.idea/copyright/GPLv3.xml new file mode 100644 index 00000000..89aa51ff --- /dev/null +++ b/gradle/.idea/copyright/GPLv3.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/gradle/.idea/copyright/profiles_settings.xml b/gradle/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000..c8a01997 --- /dev/null +++ b/gradle/.idea/copyright/profiles_settings.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/gradle/.idea/inspectionProfiles/profiles_settings.xml b/gradle/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..59059c7a --- /dev/null +++ b/gradle/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/gradle/.idea/misc.xml b/gradle/.idea/misc.xml new file mode 100644 index 00000000..cdb95b5a --- /dev/null +++ b/gradle/.idea/misc.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gradle/.idea/scopes/masterpassword.xml b/gradle/.idea/scopes/masterpassword.xml new file mode 100644 index 00000000..978deba8 --- /dev/null +++ b/gradle/.idea/scopes/masterpassword.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/gradle/gradle/wrapper/gradle-wrapper.properties b/gradle/gradle/wrapper/gradle-wrapper.properties index 6b4950e0..b5288155 100644 --- a/gradle/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Mar 06 18:26:17 EST 2017 +#Sun Mar 26 09:11:08 EDT 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/gradle/local.properties b/gradle/local.properties deleted file mode 100644 index 087c38b2..00000000 --- a/gradle/local.properties +++ /dev/null @@ -1,2 +0,0 @@ -sdk.dir=/usr/local/opt/android-sdk -ndk.dir=/usr/local/opt/android-ndk diff --git a/gradle/settings.gradle b/gradle/settings.gradle index 0d74348b..151e9057 100644 --- a/gradle/settings.gradle +++ b/gradle/settings.gradle @@ -1,19 +1,19 @@ -rootProject.name = ':masterpassword' +rootProject.name = 'masterpassword' -include ':masterpassword:algorithm' -project(':masterpassword:algorithm').projectDir = new File( '../core/java/algorithm' ) +include 'masterpassword-algorithm' +project(':masterpassword-algorithm').projectDir = new File( '../core/java/algorithm' ) -include ':masterpassword:model' -project(':masterpassword:model').projectDir = new File( '../core/java/model' ) +include 'masterpassword-model' +project(':masterpassword-model').projectDir = new File( '../core/java/model' ) -include ':masterpassword:tests' -project(':masterpassword:tests').projectDir = new File( '../core/java/tests' ) +include 'masterpassword-tests' +project(':masterpassword-tests').projectDir = new File( '../core/java/tests' ) -include ':masterpassword:android' -project(':masterpassword:android').projectDir = new File( '../platform-android' ) +include 'masterpassword-cli' +project(':masterpassword-cli').projectDir = new File( '../platform-independent/cli-java' ) -include ':masterpassword:cli' -project(':masterpassword:cli').projectDir = new File( '../platform-independent/cli-java' ) +include 'masterpassword-gui' +project(':masterpassword-gui').projectDir = new File( '../platform-independent/gui-java' ) -include ':masterpassword:gui' -project(':masterpassword:gui').projectDir = new File( '../platform-independent/gui-java' ) +include 'masterpassword-android' +project(':masterpassword-android').projectDir = new File( '../platform-android' ) diff --git a/platform-android/build.gradle b/platform-android/build.gradle index 7fc8bdfd..6dac70eb 100644 --- a/platform-android/build.gradle +++ b/platform-android/build.gradle @@ -36,8 +36,8 @@ android { } dependencies { - compile project( ':masterpassword:algorithm' ) - compile project( ':masterpassword:tests' ) + compile project( ':masterpassword-algorithm' ) + compile project( ':masterpassword-tests' ) compile group: 'org.slf4j', name: 'slf4j-android', version:'1.7.13-underscore' compile group: 'com.jakewharton', name: 'butterknife', version:'8.5.1' diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/EmergencyActivity.java b/platform-android/src/main/java/com/lyndir/masterpassword/EmergencyActivity.java index 981a1908..06087e34 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/EmergencyActivity.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/EmergencyActivity.java @@ -32,6 +32,7 @@ public class EmergencyActivity extends Activity { private static final Logger logger = Logger.get( EmergencyActivity.class ); private static final ClipData EMPTY_CLIP = new ClipData( new ClipDescription( "", new String[0] ), new ClipData.Item( "" ) ); private static final int PASSWORD_NOTIFICATION = 0; + public static final int CLIPBOARD_CLEAR_DELAY = 20 /* s */ * MPConstant.MS_PER_S; private final Preferences preferences = Preferences.get( this ); private final ListeningExecutorService executor = MoreExecutors.listeningDecorator( Executors.newSingleThreadExecutor() ); @@ -81,14 +82,13 @@ public class EmergencyActivity extends Activity { private int id_version; private String sitePassword; - public static void start(Context context) { + public static void start(final Context context) { context.startActivity( new Intent( context, EmergencyActivity.class ) ); } @Override - public void onCreate(@Nullable Bundle savedInstanceState) { + public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - Res.init( getResources() ); getWindow().setFlags( WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE ); setContentView( R.layout.activity_emergency ); @@ -157,13 +157,13 @@ public class EmergencyActivity extends Activity { } } ); - fullNameField.setTypeface( Res.exo_Thin ); + fullNameField.setTypeface( Res.get( this ).exo_Thin ); fullNameField.setPaintFlags( fullNameField.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG ); - masterPasswordField.setTypeface( Res.sourceCodePro_ExtraLight ); + masterPasswordField.setTypeface( Res.get( this ).sourceCodePro_ExtraLight ); masterPasswordField.setPaintFlags( masterPasswordField.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG ); - siteNameField.setTypeface( Res.exo_Regular ); + siteNameField.setTypeface( Res.get( this ).exo_Regular ); siteNameField.setPaintFlags( siteNameField.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG ); - sitePasswordField.setTypeface( Res.sourceCodePro_Black ); + sitePasswordField.setTypeface( Res.get( this ).sourceCodePro_Black ); sitePasswordField.setPaintFlags( sitePasswordField.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG ); rememberFullNameField.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @@ -243,9 +243,11 @@ public class EmergencyActivity extends Activity { final String fullName = fullNameField.getText().toString(); final char[] masterPassword = masterPasswordField.getText().toString().toCharArray(); final MasterKey.Version version = (MasterKey.Version) siteVersionButton.getTag(); - if (fullName.hashCode() == id_userName && Arrays.hashCode( masterPassword ) == id_masterPassword && - version.ordinal() == id_version && masterKeyFuture != null && !masterKeyFuture.isCancelled()) - return; + if ((id_userName == fullName.hashCode()) + && (id_masterPassword == Arrays.hashCode( masterPassword )) + && (id_version == version.ordinal())) + if ((masterKeyFuture != null) && !masterKeyFuture.isCancelled()) + return; id_userName = fullName.hashCode(); id_masterPassword = Arrays.hashCode( masterPassword ); @@ -257,7 +259,7 @@ public class EmergencyActivity extends Activity { if (masterKeyFuture != null) masterKeyFuture.cancel( true ); - if (fullName.isEmpty() || masterPassword.length == 0) { + if (fullName.isEmpty() || (masterPassword.length == 0)) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); return; @@ -272,7 +274,7 @@ public class EmergencyActivity extends Activity { try { return MasterKey.create( version, fullName, masterPassword ); } - catch (Exception e) { + catch (final Exception e) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); logger.err( e, "While generating master key." ); @@ -297,7 +299,7 @@ public class EmergencyActivity extends Activity { final MPSiteType type = (MPSiteType) siteTypeButton.getTag(); final UnsignedInteger counter = UnsignedInteger.valueOf( siteCounterButton.getText().toString() ); - if (masterKeyFuture == null || siteName.isEmpty() || type == null) { + if ((masterKeyFuture == null) || siteName.isEmpty() || (type == null)) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); @@ -322,17 +324,17 @@ public class EmergencyActivity extends Activity { } } ); } - catch (InterruptedException ignored) { + catch (final InterruptedException ignored) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); } - catch (ExecutionException e) { + catch (final ExecutionException e) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); logger.err( e, "While generating site password." ); throw Throwables.propagate( e ); } - catch (RuntimeException e) { + catch (final RuntimeException e) { sitePasswordField.setText( "" ); progressView.setVisibility( View.INVISIBLE ); logger.err( e, "While generating site password." ); @@ -342,7 +344,7 @@ public class EmergencyActivity extends Activity { } ); } - public void integrityTests(View view) { + public void integrityTests(final View view) { if (masterKeyFuture != null) { masterKeyFuture.cancel( true ); masterKeyFuture = null; @@ -350,8 +352,8 @@ public class EmergencyActivity extends Activity { TestActivity.startNoSkip( this ); } - public void copySitePassword(View view) { - final String currentSitePassword = this.sitePassword; + public void copySitePassword(final View view) { + final String currentSitePassword = sitePassword; if (TextUtils.isEmpty( currentSitePassword )) return; @@ -384,7 +386,7 @@ public class EmergencyActivity extends Activity { notificationManager.cancel( PASSWORD_NOTIFICATION ); timer.cancel(); } - }, 20000 ); + }, CLIPBOARD_CLEAR_DELAY ); Intent startMain = new Intent( Intent.ACTION_MAIN ); startMain.addCategory( Intent.CATEGORY_HOME ); @@ -392,7 +394,7 @@ public class EmergencyActivity extends Activity { startActivity( startMain ); } - private abstract class ValueChangedListener + private abstract static class ValueChangedListener implements TextWatcher, NumberPicker.OnValueChangeListener, AdapterView.OnItemSelectedListener, View.OnFocusChangeListener { abstract void update(); diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/MainThreadExecutor.java b/platform-android/src/main/java/com/lyndir/masterpassword/MainThreadExecutor.java index 98b59437..f9543222 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/MainThreadExecutor.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/MainThreadExecutor.java @@ -53,7 +53,7 @@ public class MainThreadExecutor extends AbstractExecutorService { synchronized (commands) { ImmutableList pendingTasks = ImmutableList.copyOf( commands ); commands.clear(); - commands.notify(); + commands.notifyAll(); return pendingTasks; } } diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java b/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java index cd6e18a6..5de46519 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java @@ -12,7 +12,7 @@ import javax.annotation.Nullable; /** * @author lhunath, 2016-02-20 */ -public class Preferences { +public final class Preferences { private static final String PREF_TESTS_PASSED = "integrityTestsPassed"; private static final String PREF_NATIVE_KDF = "nativeKDF"; @@ -35,7 +35,7 @@ public class Preferences { return instance; } - private Preferences(Context context) { + private Preferences(final Context context) { this.context = context; } @@ -47,7 +47,7 @@ public class Preferences { return prefs; } - public boolean setNativeKDFEnabled(boolean enabled) { + public boolean setNativeKDFEnabled(final boolean enabled) { if (isAllowNativeKDF() == enabled) return false; @@ -71,7 +71,7 @@ public class Preferences { return prefs().getStringSet( PREF_TESTS_PASSED, ImmutableSet.of() ); } - public boolean setRememberFullName(boolean enabled) { + public boolean setRememberFullName(final boolean enabled) { if (isRememberFullName() == enabled) return false; @@ -83,7 +83,7 @@ public class Preferences { return prefs().getBoolean( PREF_REMEMBER_FULL_NAME, false ); } - public boolean setForgetPassword(boolean enabled) { + public boolean setForgetPassword(final boolean enabled) { if (isForgetPassword() == enabled) return false; @@ -95,7 +95,7 @@ public class Preferences { return prefs().getBoolean( PREF_FORGET_PASSWORD, false ); } - public boolean setMaskPassword(boolean enabled) { + public boolean setMaskPassword(final boolean enabled) { if (isMaskPassword() == enabled) return false; @@ -107,7 +107,7 @@ public class Preferences { return prefs().getBoolean( PREF_MASK_PASSWORD, false ); } - public boolean setFullName(@Nullable String value) { + public boolean setFullName(@Nullable final String value) { if (getFullName().equals( value )) return false; @@ -120,8 +120,8 @@ public class Preferences { return prefs().getString( PREF_FULL_NAME, "" ); } - public boolean setDefaultSiteType(@Nonnull MPSiteType value) { - if (getDefaultSiteType().equals( value )) + public boolean setDefaultSiteType(@Nonnull final MPSiteType value) { + if (getDefaultSiteType() == value) return false; prefs().edit().putInt( PREF_SITE_TYPE, value.ordinal() ).apply(); @@ -133,8 +133,8 @@ public class Preferences { return MPSiteType.values()[prefs().getInt( PREF_SITE_TYPE, MPSiteType.GeneratedLong.ordinal() )]; } - public boolean setDefaultVersion(@Nonnull MasterKey.Version value) { - if (getDefaultVersion().equals( value )) + public boolean setDefaultVersion(@Nonnull final MasterKey.Version value) { + if (getDefaultVersion() == value) return false; prefs().edit().putInt( PREF_ALGORITHM_VERSION, value.ordinal() ).apply(); diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/Res.java b/platform-android/src/main/java/com/lyndir/masterpassword/Res.java index 5ac37a42..cd5de902 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/Res.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/Res.java @@ -1,5 +1,6 @@ package com.lyndir.masterpassword; +import android.content.Context; import android.content.res.Resources; import android.graphics.Typeface; @@ -7,28 +8,32 @@ import android.graphics.Typeface; /** * @author lhunath, 2014-08-25 */ -public class Res { +public final class Res { - public static Typeface sourceCodePro_Black; - public static Typeface sourceCodePro_ExtraLight; - public static Typeface exo_Bold; - public static Typeface exo_ExtraBold; - public static Typeface exo_Regular; - public static Typeface exo_Thin; + public final Typeface sourceCodePro_Black; + public final Typeface sourceCodePro_ExtraLight; + public final Typeface exo_Bold; + public final Typeface exo_ExtraBold; + public final Typeface exo_Regular; + public final Typeface exo_Thin; - private static boolean initialized; + private static Res res; - public static void init(Resources resources) { + public static synchronized Res get(final Context context) { + if (res == null) + res = new Res( context ); - if (initialized) - return; - initialized = true; + return res; + } - sourceCodePro_Black = Typeface.createFromAsset( resources.getAssets(), "SourceCodePro-Black.otf" ); - sourceCodePro_ExtraLight = Typeface.createFromAsset( resources.getAssets(), "SourceCodePro-ExtraLight.otf" ); - exo_Bold = Typeface.createFromAsset( resources.getAssets(), "Exo2.0-Bold.otf" ); - exo_ExtraBold = Typeface.createFromAsset( resources.getAssets(), "Exo2.0-ExtraBold.otf" ); - exo_Regular = Typeface.createFromAsset( resources.getAssets(), "Exo2.0-Regular.otf" ); - exo_Thin = Typeface.createFromAsset( resources.getAssets(), "Exo2.0-Thin.otf" ); + @SuppressWarnings("HardCodedStringLiteral") + private Res(final Context context) { + + sourceCodePro_Black = Typeface.createFromAsset( context.getResources().getAssets(), "SourceCodePro-Black.otf" ); + sourceCodePro_ExtraLight = Typeface.createFromAsset( context.getResources().getAssets(), "SourceCodePro-ExtraLight.otf" ); + exo_Bold = Typeface.createFromAsset( context.getResources().getAssets(), "Exo2.0-Bold.otf" ); + exo_ExtraBold = Typeface.createFromAsset( context.getResources().getAssets(), "Exo2.0-ExtraBold.otf" ); + exo_Regular = Typeface.createFromAsset( context.getResources().getAssets(), "Exo2.0-Regular.otf" ); + exo_Thin = Typeface.createFromAsset( context.getResources().getAssets(), "Exo2.0-Thin.otf" ); } } diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/TestActivity.java b/platform-android/src/main/java/com/lyndir/masterpassword/TestActivity.java index a54753e4..6dea8e80 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/TestActivity.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/TestActivity.java @@ -47,14 +47,13 @@ public class TestActivity extends Activity implements MPTestSuite.Listener { private Runnable action; private ImmutableSet testNames; - public static void startNoSkip(Context context) { + public static void startNoSkip(final Context context) { context.startActivity( new Intent( context, TestActivity.class ) ); } @Override - public void onCreate(@Nullable Bundle savedInstanceState) { + public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - Res.init( getResources() ); setContentView( R.layout.activity_test ); ButterKnife.bind( this ); @@ -76,11 +75,11 @@ public class TestActivity extends Activity implements MPTestSuite.Listener { @Nullable @Override public String apply(@Nullable final MPTests.Case input) { - return input == null? null: input.identifier; + return (input == null)? null: input.identifier; } } ).filter( Predicates.notNull() ).toSet(); } - catch (MPTestSuite.UnavailableException e) { + catch (final MPTestSuite.UnavailableException e) { logger.err( e, "While loading test suite" ); setStatus( R.string.tests_unavailable, R.string.tests_btn_unavailable, new Runnable() { @Override @@ -111,7 +110,7 @@ public class TestActivity extends Activity implements MPTestSuite.Listener { Futures.addCallback( testFuture = backgroundExecutor.submit( testSuite ), new FutureCallback() { @Override public void onSuccess(@Nullable final Boolean result) { - if (result != null && result) + if ((result != null) && result) setStatus( R.string.tests_passed, R.string.tests_btn_passed, new Runnable() { @Override public void run() { @@ -141,12 +140,12 @@ public class TestActivity extends Activity implements MPTestSuite.Listener { }, mainExecutor ); } - public void onAction(View v) { + public void onAction(final View v) { if (action != null) action.run(); } - private void setStatus(int statusId, int buttonId, @Nullable Runnable action) { + private void setStatus(final int statusId, final int buttonId, @Nullable final Runnable action) { this.action = action; if (statusId == 0) @@ -166,7 +165,7 @@ public class TestActivity extends Activity implements MPTestSuite.Listener { runOnUiThread( new Runnable() { @Override public void run() { - logView.append( strf( '\n' + messageFormat, args ) ); + logView.append( strf( "%n" + messageFormat, args ) ); progressView.setMax( max ); progressView.setProgress( current ); diff --git a/platform-android/src/main/res/layout/activity_emergency.xml b/platform-android/src/main/res/layout/activity_emergency.xml index 4a963366..d2312d33 100644 --- a/platform-android/src/main/res/layout/activity_emergency.xml +++ b/platform-android/src/main/res/layout/activity_emergency.xml @@ -3,8 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:fillViewport="true" - android:background="@drawable/background"> + android:fillViewport="true"> + android:fillViewport="true"> diff --git a/platform-android/src/main/res/values/styles.xml b/platform-android/src/main/res/values/styles.xml index c37983b4..934ccb5c 100644 --- a/platform-android/src/main/res/values/styles.xml +++ b/platform-android/src/main/res/values/styles.xml @@ -2,5 +2,6 @@ diff --git a/platform-independent/cli-java/build.gradle b/platform-independent/cli-java/build.gradle index 4f4a6323..41a2f099 100644 --- a/platform-independent/cli-java/build.gradle +++ b/platform-independent/cli-java/build.gradle @@ -8,7 +8,7 @@ description = 'Master Password CLI' mainClassName = 'com.lyndir.masterpassword.CLI' dependencies { - compile project(':masterpassword:algorithm') + compile project(':masterpassword-algorithm') compile group: 'ch.qos.logback', name: 'logback-classic', version:'1.1.2' } diff --git a/platform-independent/cli-java/src/main/java/com/lyndir/masterpassword/CLI.java b/platform-independent/cli-java/src/main/java/com/lyndir/masterpassword/CLI.java index c58b1b2d..ebe65ac9 100644 --- a/platform-independent/cli-java/src/main/java/com/lyndir/masterpassword/CLI.java +++ b/platform-independent/cli-java/src/main/java/com/lyndir/masterpassword/CLI.java @@ -19,6 +19,7 @@ package com.lyndir.masterpassword; 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.Charsets; import com.google.common.base.Joiner; import com.google.common.collect.Maps; import com.google.common.io.LineReader; @@ -35,9 +36,10 @@ import java.util.Map; * * @author mbillemo */ -public class CLI { +@SuppressWarnings({ "UseOfSystemOutOrSystemErr", "HardCodedStringLiteral" }) +public final class CLI { - public static void main(final String[] args) + public static void main(final String... args) throws IOException { // Read information from the environment. @@ -96,68 +98,68 @@ public class CLI { // Help else if ("-h".equals( arg ) || "--help".equals( arg )) { System.out.println(); - System.out.format( "Usage: mpw [-u name] [-t type] [-c counter] site\n\n" ); - System.out.format( " -u name Specify the full name of the user.\n" ); - System.out.format( " Defaults to %s in env.\n\n", MPConstant.env_userName ); - System.out.format( " -t type Specify the password's template.\n" ); - System.out.format( " Defaults to %s in env or 'long' for password, 'name' for login.\n", MPConstant.env_siteType ); + System.out.format( "Usage: mpw [-u name] [-t type] [-c counter] site%n%n" ); + System.out.format( " -u name Specify the full name of the user.%n" ); + System.out.format( " Defaults to %s in env.%n%n", MPConstant.env_userName ); + System.out.format( " -t type Specify the password's template.%n" ); + System.out.format( " Defaults to %s in env or 'long' for password, 'name' for login.%n", MPConstant.env_siteType ); int optionsLength = 0; Map typeMap = Maps.newLinkedHashMap(); - for (MPSiteType elementType : MPSiteType.values()) { + for (final MPSiteType elementType : MPSiteType.values()) { String options = Joiner.on( ", " ).join( elementType.getOptions() ); typeMap.put( options, elementType ); optionsLength = Math.max( optionsLength, options.length() ); } - for (Map.Entry entry : typeMap.entrySet()) { + for (final Map.Entry entry : typeMap.entrySet()) { String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() ); - String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | "; - infoString += entry.getValue().getDescription().replaceAll( "\n", infoNewline ); + String infoNewline = strf( "%n%s | ", StringUtils.repeat( " ", infoString.length() - 3 ) ); + infoString += entry.getValue().getDescription().replaceAll( strf( "%n" ), infoNewline ); System.out.println( infoString ); } System.out.println(); - System.out.format( " -c counter The value of the counter.\n" ); - System.out.format( " Defaults to %s in env or '1'.\n\n", MPConstant.env_siteCounter ); - System.out.format( " -v variant The kind of content to generate.\n" ); - System.out.format( " Defaults to 'password'.\n" ); + System.out.format( " -c counter The value of the counter.%n" ); + System.out.format( " Defaults to %s in env or '1'.%n%n", MPConstant.env_siteCounter ); + System.out.format( " -v variant The kind of content to generate.%n" ); + System.out.format( " Defaults to 'password'.%n" ); optionsLength = 0; Map variantMap = Maps.newLinkedHashMap(); - for (MPSiteVariant elementVariant : MPSiteVariant.values()) { + for (final MPSiteVariant elementVariant : MPSiteVariant.values()) { String options = Joiner.on( ", " ).join( elementVariant.getOptions() ); variantMap.put( options, elementVariant ); optionsLength = Math.max( optionsLength, options.length() ); } - for (Map.Entry entry : variantMap.entrySet()) { + for (final Map.Entry entry : variantMap.entrySet()) { String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() ); - String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | "; - infoString += entry.getValue().getDescription().replaceAll( "\n", infoNewline ); + String infoNewline = strf( "%n%s | ", StringUtils.repeat( " ", infoString.length() - 3 ) ); + infoString += entry.getValue().getDescription().replaceAll( strf( "%n" ), infoNewline ); System.out.println( infoString ); } System.out.println(); - System.out.format( " -C context A variant-specific context.\n" ); - System.out.format( " Defaults to empty.\n" ); - for (Map.Entry entry : variantMap.entrySet()) { + System.out.format( " -C context A variant-specific context.%n" ); + System.out.format( " Defaults to empty.%n" ); + for (final Map.Entry entry : variantMap.entrySet()) { String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() ); - String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | "; - infoString += entry.getValue().getContextDescription().replaceAll( "\n", infoNewline ); + String infoNewline = strf( "%n%s | ", StringUtils.repeat( " ", infoString.length() - 3 ) ); + infoString += entry.getValue().getContextDescription().replaceAll( strf( "%n" ), infoNewline ); System.out.println( infoString ); } System.out.println(); - System.out.format( " ENVIRONMENT\n\n" ); - System.out.format( " MP_USERNAME | The full name of the user.\n" ); - System.out.format( " MP_SITETYPE | The default password template.\n" ); - System.out.format( " MP_SITECOUNTER | The default counter value.\n\n" ); + System.out.format( " ENVIRONMENT%n%n" ); + System.out.format( " MP_USERNAME | The full name of the user.%n" ); + System.out.format( " MP_SITETYPE | The default password template.%n" ); + System.out.format( " MP_SITECOUNTER | The default counter value.%n%n" ); return; } else siteName = arg; // Read missing information from the console. Console console = System.console(); - try (InputStreamReader inReader = new InputStreamReader( System.in )) { + try (InputStreamReader inReader = new InputStreamReader( System.in, Charsets.UTF_8 )) { LineReader lineReader = new LineReader( inReader ); if (siteName == null) { diff --git a/platform-independent/gui-java/build.gradle b/platform-independent/gui-java/build.gradle index faef5e30..867ec31d 100644 --- a/platform-independent/gui-java/build.gradle +++ b/platform-independent/gui-java/build.gradle @@ -8,7 +8,7 @@ description = 'Master Password GUI' mainClassName = 'com.lyndir.masterpassword.gui.GUI' dependencies { - compile project(':masterpassword:model') + compile project(':masterpassword-model') compile group: 'ch.qos.logback', name: 'logback-classic', version:'1.1.2' compile group: 'com.yuvimasory', name: 'orange-extensions', version:'1.3.0' diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/GUI.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/GUI.java index 3f99082d..ee0f9150 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/GUI.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/GUI.java @@ -46,8 +46,7 @@ public class GUI implements UnlockFrame.SignInCallback { private final UnlockFrame unlockFrame = new UnlockFrame( this ); private PasswordFrame passwordFrame; - public static void main(final String[] args) - throws IOException { + public static void main(final String... args) { if (Config.get().checkForUpdates()) checkUpdate(); @@ -76,7 +75,7 @@ public class GUI implements UnlockFrame.SignInCallback { String upstreamRevision = upstream.readFirstLine(); logger.inf( "Local Revision: <%s>", manifestRevision ); logger.inf( "Upstream Revision: <%s>", upstreamRevision ); - if (manifestRevision != null && !manifestRevision.equalsIgnoreCase( upstreamRevision )) { + if ((manifestRevision != null) && !manifestRevision.equalsIgnoreCase( upstreamRevision )) { logger.wrn( "You are not running the current official version. Please update from:\n" + "http://masterpasswordapp.com/masterpassword-gui.jar" ); JOptionPane.showMessageDialog( null, "A new version of Master Password is available.\n" @@ -85,7 +84,7 @@ public class GUI implements UnlockFrame.SignInCallback { } } } - catch (IOException e) { + catch (final IOException e) { logger.wrn( e, "Couldn't check for version update." ); } } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/Res.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/Res.java index cd2f3aa3..f9b66ea8 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/Res.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/Res.java @@ -23,14 +23,17 @@ import java.util.concurrent.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.*; +import org.jetbrains.annotations.NonNls; /** * @author lhunath, 2014-06-11 */ +@SuppressWarnings("HardcodedFileSeparator") public abstract class Res { - private static final WeakHashMap executorByWindow = new WeakHashMap<>(); + private static final int AVATAR_COUNT = 19; + private static final Map executorByWindow = new WeakHashMap<>(); private static final Logger logger = Logger.get( Res.class ); private static final Colors colors = new Colors(); @@ -45,7 +48,7 @@ public abstract class Res { try { job.run(); } - catch (Throwable t) { + catch (final Throwable t) { logger.err( t, "Unexpected: %s", t.getLocalizedMessage() ); } } @@ -65,9 +68,9 @@ public abstract class Res { try { return job.call(); } - catch (Throwable t) { + catch (final Throwable t) { logger.err( t, "Unexpected: %s", t.getLocalizedMessage() ); - throw t; + throw Throwables.propagate( t ); } } }, delay, timeUnit ), executor ); @@ -109,7 +112,7 @@ public abstract class Res { } public static int avatars() { - return 19; + return AVATAR_COUNT; } public static Font emoticonsFont() { @@ -180,10 +183,10 @@ public abstract class Res { return font( "fonts/Arimo-Regular.ttf" ); } - private static Font font(String fontResourceName) { + private static Font font(@NonNls final String fontResourceName) { Map> fontsByResourceName = Maps.newHashMap(); SoftReference fontRef = fontsByResourceName.get( fontResourceName ); - Font font = fontRef == null? null: fontRef.get(); + Font font = (fontRef == null)? null: fontRef.get(); if (font == null) try { fontsByResourceName.put( fontResourceName, new SoftReference<>( @@ -203,17 +206,15 @@ public abstract class Res { private static final class RetinaIcon extends ImageIcon { private static final Pattern scalePattern = Pattern.compile( ".*@(\\d+)x.[^.]+$" ); + private static final long serialVersionUID = 1L; private final float scale; - public RetinaIcon(final URL url) { + private RetinaIcon(final URL url) { super( url ); Matcher scaleMatcher = scalePattern.matcher( url.getPath() ); - if (scaleMatcher.matches()) - scale = Float.parseFloat( scaleMatcher.group( 1 ) ); - else - scale = 1; + scale = scaleMatcher.matches()? Float.parseFloat( scaleMatcher.group( 1 ) ): 1; } //private static URL retinaURL(final URL url) { @@ -242,13 +243,14 @@ public abstract class Res { return (int) (super.getIconHeight() / scale); } - public synchronized void paintIcon(Component c, Graphics g, int x, int y) { + @Override + public synchronized void paintIcon(final Component c, final Graphics g, final int x, final int y) { ImageObserver observer = ifNotNullElse( getImageObserver(), c ); Image image = getImage(); int width = image.getWidth( observer ); int height = image.getHeight( observer ); - final Graphics2D g2d = (Graphics2D) g.create( x, y, width, height ); + Graphics2D g2d = (Graphics2D) g.create( x, y, width, height ); g2d.scale( 1 / scale, 1 / scale ); g2d.drawImage( image, 0, 0, observer ); @@ -276,7 +278,7 @@ public abstract class Res { return controlBorder; } - public Color fromIdenticonColor(MPIdenticon.Color identiconColor, BackgroundMode backgroundMode) { + public Color fromIdenticonColor(final MPIdenticon.Color identiconColor, final BackgroundMode backgroundMode) { switch (identiconColor) { case RED: switch (backgroundMode) { diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java index 49322203..95356782 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java @@ -23,18 +23,22 @@ public class IncognitoSite extends Site { this.algorithmVersion = algorithmVersion; } + @Override public String getSiteName() { return siteName; } + @Override public void setSiteName(final String siteName) { this.siteName = siteName; } + @Override public MPSiteType getSiteType() { return siteType; } + @Override public void setSiteType(final MPSiteType siteType) { this.siteType = siteType; } @@ -49,10 +53,12 @@ public class IncognitoSite extends Site { this.algorithmVersion = algorithmVersion; } + @Override public UnsignedInteger getSiteCounter() { return siteCounter; } + @Override public void setSiteCounter(final UnsignedInteger siteCounter) { this.siteCounter = siteCounter; } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java index 83318ed1..c5d70fc5 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java @@ -11,12 +11,14 @@ import javax.annotation.Nullable; public class IncognitoUser extends User { private final String fullName; + @Nullable private char[] masterPassword; public IncognitoUser(final String fullName) { this.fullName = fullName; } + @Override public String getFullName() { return fullName; } @@ -30,7 +32,7 @@ public class IncognitoUser extends User { @Override public void authenticate(final char[] masterPassword) throws IncorrectMasterPasswordException { - this.masterPassword = masterPassword; + this.masterPassword = masterPassword.clone(); } @Override @@ -43,6 +45,6 @@ public class IncognitoUser extends User { } @Override - public void deleteSite(Site site) { + public void deleteSite(final Site site) { } } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelSite.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelSite.java index 488a5f5a..be41ce12 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelSite.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelSite.java @@ -14,13 +14,14 @@ public class ModelSite extends Site { private final MPSite model; public ModelSite(final MPSiteResult result) { - this.model = result.getSite(); + model = result.getSite(); } public MPSite getModel() { return model; } + @Override public String getSiteName() { return model.getSiteName(); } @@ -31,6 +32,7 @@ public class ModelSite extends Site { MPUserFileManager.get().save(); } + @Override public MPSiteType getSiteType() { return model.getSiteType(); } @@ -56,6 +58,7 @@ public class ModelSite extends Site { } } + @Override public UnsignedInteger getSiteCounter() { return model.getSiteCounter(); } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelUser.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelUser.java index 30d555ac..a1716348 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelUser.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/ModelUser.java @@ -19,7 +19,7 @@ public class ModelUser extends User { @Nullable private char[] masterPassword; - public ModelUser(MPUser model) { + public ModelUser(final MPUser model) { this.model = model; } @@ -48,10 +48,11 @@ public class ModelUser extends User { MPUserFileManager.get().save(); } + @Override public void authenticate(final char[] masterPassword) throws IncorrectMasterPasswordException { putKey( model.authenticate( masterPassword ) ); - this.masterPassword = masterPassword; + this.masterPassword = masterPassword.clone(); MPUserFileManager.get().save(); } @@ -66,8 +67,8 @@ public class ModelUser extends User { } @Override - public Iterable findSitesByName(final String query) { - return FluentIterable.from( model.findSitesByName( query ) ).transform( new Function() { + public Iterable findSitesByName(final String siteName) { + return FluentIterable.from( model.findSitesByName( siteName ) ).transform( new Function() { @Nullable @Override public Site apply(@Nullable final MPSiteResult site) { @@ -84,7 +85,7 @@ public class ModelUser extends User { } @Override - public void deleteSite(Site site) { + public void deleteSite(final Site site) { if (site instanceof ModelSite) { model.deleteSite(((ModelSite) site).getModel()); MPUserFileManager.get().save(); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/Site.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/Site.java index cc09851c..62f0dea6 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/Site.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/Site.java @@ -14,19 +14,19 @@ public abstract class Site { public abstract String getSiteName(); - public abstract void setSiteName(final String siteName); + public abstract void setSiteName(String siteName); public abstract MPSiteType getSiteType(); - public abstract void setSiteType(final MPSiteType siteType); + public abstract void setSiteType(MPSiteType siteType); public abstract MasterKey.Version getAlgorithmVersion(); - public abstract void setAlgorithmVersion(final MasterKey.Version algorithmVersion); + public abstract void setAlgorithmVersion(MasterKey.Version algorithmVersion); public abstract UnsignedInteger getSiteCounter(); - public abstract void setSiteCounter(final UnsignedInteger siteCounter); + public abstract void setSiteCounter(UnsignedInteger siteCounter); @Override public String toString() { diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/User.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/User.java index 1355757c..12f43587 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/User.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/User.java @@ -4,8 +4,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import com.lyndir.masterpassword.MasterKey; import com.lyndir.masterpassword.model.IncorrectMasterPasswordException; -import java.util.EnumMap; -import java.util.Objects; +import java.util.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -16,14 +15,15 @@ import javax.annotation.Nullable; public abstract class User { @Nonnull - private final EnumMap keyByVersion = Maps.newEnumMap( MasterKey.Version.class ); + private final Map keyByVersion = Maps.newEnumMap( MasterKey.Version.class ); public abstract String getFullName(); @Nullable protected abstract char[] getMasterPassword(); - public abstract void authenticate(final char[] masterPassword) + @SuppressWarnings("MethodCanBeVariableArityMethod") + public abstract void authenticate(char[] masterPassword) throws IncorrectMasterPasswordException; public int getAvatar() { @@ -35,7 +35,7 @@ public abstract class User { } @Nonnull - public MasterKey getKey(MasterKey.Version algorithmVersion) { + public MasterKey getKey(final MasterKey.Version algorithmVersion) { char[] masterPassword = Preconditions.checkNotNull( getMasterPassword(), "User is not authenticated: " + getFullName() ); MasterKey key = keyByVersion.get( algorithmVersion ); @@ -47,26 +47,26 @@ public abstract class User { return key; } - protected void putKey(MasterKey masterKey) { + protected void putKey(final MasterKey masterKey) { MasterKey oldKey = keyByVersion.put( masterKey.getAlgorithmVersion(), masterKey ); if (oldKey != null) oldKey.invalidate(); } public void reset() { - for (MasterKey key : keyByVersion.values()) + for (final MasterKey key : keyByVersion.values()) key.invalidate(); } - public abstract Iterable findSitesByName(final String siteName); + public abstract Iterable findSitesByName(String siteName); - public abstract void addSite(final Site site); + public abstract void addSite(Site site); - public abstract void deleteSite(final Site site); + public abstract void deleteSite(Site site); @Override public boolean equals(final Object obj) { - return this == obj || obj instanceof User && Objects.equals( getFullName(), ((User) obj).getFullName() ); + return (this == obj) || ((obj instanceof User) && Objects.equals( getFullName(), ((User) obj).getFullName() )); } @Override diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/package-info.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/package-info.java new file mode 100644 index 00000000..0edad886 --- /dev/null +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/package-info.java @@ -0,0 +1,9 @@ +/** + * + * @author lhunath, 15-02-04 + */ + +@ParametersAreNonnullByDefault +package com.lyndir.masterpassword.gui.model; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/platform/mac/AppleGUI.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/platform/mac/AppleGUI.java index e5b5c7bd..514461a4 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/platform/mac/AppleGUI.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/platform/mac/AppleGUI.java @@ -19,17 +19,17 @@ public class AppleGUI extends GUI { application.addAppEventListener( new AppForegroundListener() { @Override - public void appMovedToBackground(AppEvent.AppForegroundEvent arg0) { + public void appMovedToBackground(final AppEvent.AppForegroundEvent arg0) { } @Override - public void appRaisedToForeground(AppEvent.AppForegroundEvent arg0) { + public void appRaisedToForeground(final AppEvent.AppForegroundEvent arg0) { open(); } } ); application.addAppEventListener( new AppReOpenedListener() { @Override - public void appReOpened(AppEvent.AppReOpenedEvent arg0) { + public void appReOpened(final AppEvent.AppReOpenedEvent arg0) { open(); } } ); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/util/Components.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/util/Components.java index 1110a354..55fdce2d 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/util/Components.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/util/Components.java @@ -13,11 +13,11 @@ import javax.swing.border.CompoundBorder; */ public abstract class Components { - public static GradientPanel boxLayout(int axis, Component... components) { + public static GradientPanel boxLayout(final int axis, final Component... components) { GradientPanel container = gradientPanel( null, null ); // container.setBackground( Color.red ); container.setLayout( new BoxLayout( container, axis ) ); - for (Component component : components) + for (final Component component : components) container.add( component ); return container; @@ -27,7 +27,7 @@ public abstract class Components { return borderPanel( component, border, null ); } - public static GradientPanel borderPanel(final JComponent component, @Nullable final Border border, @Nullable Color background) { + public static GradientPanel borderPanel(final JComponent component, @Nullable final Border border, @Nullable final Color background) { GradientPanel box = boxLayout( BoxLayout.LINE_AXIS, component ); if (border != null) @@ -83,7 +83,7 @@ public abstract class Components { }; } - public static JButton button(String label) { + public static JButton button(final String label) { return new JButton( label ) { { setFont( Res.controlFont().deriveFont( 12f ) ); @@ -126,18 +126,18 @@ public abstract class Components { }; } - public static JLabel label(@Nullable String label) { + public static JLabel label(@Nullable final String label) { return label( label, SwingConstants.LEADING ); } /** * @param horizontalAlignment One of the following constants - * defined in SwingConstants: - * LEFT, - * CENTER, - * RIGHT, - * LEADING or - * TRAILING. + * defined in {@code SwingConstants}: + * {@code LEFT}, + * {@code CENTER}, + * {@code RIGHT}, + * {@code LEADING} or + * {@code TRAILING}. */ public static JLabel label(@Nullable final String label, final int horizontalAlignment) { return new JLabel( label, horizontalAlignment ) { diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/AuthenticationPanel.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/AuthenticationPanel.java index aceb1274..55911622 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/AuthenticationPanel.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/AuthenticationPanel.java @@ -9,7 +9,6 @@ import java.awt.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.swing.*; -import org.jetbrains.annotations.NotNull; /** @@ -20,7 +19,7 @@ public abstract class AuthenticationPanel extends Components.GradientPanel { protected final UnlockFrame unlockFrame; protected final JLabel avatarLabel; - public AuthenticationPanel(final UnlockFrame unlockFrame) { + protected AuthenticationPanel(final UnlockFrame unlockFrame) { super( null, null ); this.unlockFrame = unlockFrame; @@ -39,7 +38,7 @@ public abstract class AuthenticationPanel extends Components.GradientPanel { avatarLabel.setToolTipText( "The avatar for your user. Click to change it." ); } - protected void updateUser(boolean repack) { + protected void updateUser(final boolean repack) { unlockFrame.updateUser( getSelectedUser() ); validate(); @@ -50,7 +49,6 @@ public abstract class AuthenticationPanel extends Components.GradientPanel { @Nullable protected abstract User getSelectedUser(); - @NotNull @Nonnull public abstract char[] getMasterPassword(); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/IncognitoAuthenticationPanel.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/IncognitoAuthenticationPanel.java index 3798f623..451c6399 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/IncognitoAuthenticationPanel.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/IncognitoAuthenticationPanel.java @@ -7,10 +7,10 @@ import com.lyndir.masterpassword.gui.util.Components; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.annotation.Nonnull; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; -import org.jetbrains.annotations.NotNull; /** @@ -62,7 +62,7 @@ public class IncognitoAuthenticationPanel extends AuthenticationPanel implements return new IncognitoUser( fullNameField.getText() ); } - @NotNull + @Nonnull @Override public char[] getMasterPassword() { return masterPasswordField.getPassword(); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java index 61e6b031..93d83c94 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java @@ -13,12 +13,12 @@ import com.lyndir.masterpassword.model.MPUserFileManager; import com.lyndir.masterpassword.gui.util.Components; import java.awt.*; import java.awt.event.*; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.plaf.metal.MetalComboBoxEditor; -import org.jetbrains.annotations.NotNull; /** @@ -109,7 +109,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite return userField.getModel().getElementAt( selectedIndex ); } - @NotNull + @Nonnull @Override public char[] getMasterPassword() { return masterPasswordField.getPassword(); @@ -175,7 +175,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite masterPasswordField.setText( "" ); } - private ModelUser[] readConfigUsers() { + private static ModelUser[] readConfigUsers() { return FluentIterable.from( MPUserFileManager.get().getUsers() ).transform( new Function() { @Nullable @Override diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java index 1e6ea749..38343d1a 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java @@ -28,7 +28,7 @@ import javax.swing.event.*; */ public class PasswordFrame extends JFrame implements DocumentListener { - private final User user; + @SuppressWarnings("FieldCanBeLocal") private final Components.GradientPanel root; private final JTextField siteNameField; private final JButton siteActionButton; @@ -41,13 +41,13 @@ public class PasswordFrame extends JFrame implements DocumentListener { private final JCheckBox maskPasswordField; private final char passwordEchoChar; private final Font passwordEchoFont; + private final User user; @Nullable private Site currentSite; private boolean updatingUI; - public PasswordFrame(User user) - throws HeadlessException { + public PasswordFrame(final User user) { super( "Master Password" ); this.user = user; @@ -104,7 +104,7 @@ public class PasswordFrame extends JFrame implements DocumentListener { public void actionPerformed(final ActionEvent e) { if (currentSite == null) return; - else if (currentSite instanceof ModelSite) + if (currentSite instanceof ModelSite) PasswordFrame.this.user.deleteSite( currentSite ); else PasswordFrame.this.user.addSite( currentSite ); @@ -160,7 +160,7 @@ public class PasswordFrame extends JFrame implements DocumentListener { maskPasswordField.setSelected( true ); maskPasswordField.addItemListener( new ItemListener() { @Override - public void itemStateChanged(ItemEvent e) { + public void itemStateChanged(final ItemEvent e) { updateMask(); } } ); @@ -203,33 +203,33 @@ public class PasswordFrame extends JFrame implements DocumentListener { } @Nonnull - private ListenableFuture updatePassword(boolean allowNameCompletion) { + private ListenableFuture updatePassword(final boolean allowNameCompletion) { final String siteNameQuery = siteNameField.getText(); if (updatingUI) return Futures.immediateCancelledFuture(); - if (siteNameQuery == null || siteNameQuery.isEmpty() || !user.isKeyAvailable()) { + if ((siteNameQuery == null) || siteNameQuery.isEmpty() || !user.isKeyAvailable()) { siteActionButton.setVisible( false ); tipLabel.setText( null ); passwordField.setText( null ); return Futures.immediateCancelledFuture(); } - final MPSiteType siteType = siteTypeField.getModel().getElementAt( siteTypeField.getSelectedIndex() ); - final MasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() ); - final UnsignedInteger siteCounter = siteCounterModel.getNumber(); + MPSiteType siteType = siteTypeField.getModel().getElementAt( siteTypeField.getSelectedIndex() ); + MasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() ); + UnsignedInteger siteCounter = siteCounterModel.getNumber(); Iterable siteResults = user.findSitesByName( siteNameQuery ); if (!allowNameCompletion) siteResults = FluentIterable.from( siteResults ).filter( new Predicate() { @Override - public boolean apply(@Nullable Site siteResult) { - return siteResult != null && siteNameQuery.equals( siteResult.getSiteName() ); + public boolean apply(@Nullable final Site siteResult) { + return (siteResult != null) && siteNameQuery.equals( siteResult.getSiteName() ); } } ); final Site site = ifNotNullElse( Iterables.getFirst( siteResults, null ), new IncognitoSite( siteNameQuery, siteType, siteCounter, siteVersion ) ); - if (currentSite != null && currentSite.getSiteName().equals( site.getSiteName() )) { + if ((currentSite != null) && currentSite.getSiteName().equals( site.getSiteName() )) { site.setSiteType( siteType ); site.setAlgorithmVersion( siteVersion ); site.setSiteCounter( siteCounter ); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java index 84105789..9d5d57f8 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java @@ -28,22 +28,21 @@ public class UnlockFrame extends JFrame { private AuthenticationPanel authenticationPanel; private Future identiconFuture; private boolean incognito; - public User user; + private User user; - public UnlockFrame(final SignInCallback signInCallback) - throws HeadlessException { + public UnlockFrame(final SignInCallback signInCallback) { super( "Unlock Master Password" ); this.signInCallback = signInCallback; setDefaultCloseOperation( DISPOSE_ON_CLOSE ); addWindowFocusListener( new WindowAdapter() { @Override - public void windowGainedFocus(WindowEvent e) { + public void windowGainedFocus(final WindowEvent e) { root.setGradientColor( Res.colors().frameBg() ); } @Override - public void windowLostFocus(WindowEvent e) { + public void windowLostFocus(final WindowEvent e) { root.setGradientColor( Color.RED ); } } ); @@ -118,7 +117,7 @@ public class UnlockFrame extends JFrame { JComponent toolsPanel = Components.boxLayout( BoxLayout.LINE_AXIS, incognitoCheckBox, Box.createGlue() ); authenticationContainer.add( toolsPanel ); - for (JButton button : authenticationPanel.getButtons()) { + for (final JButton button : authenticationPanel.getButtons()) { toolsPanel.add( button ); button.setBorder( BorderFactory.createEmptyBorder() ); button.setMargin( new Insets( 0, 0, 0, 0 ) ); @@ -138,7 +137,7 @@ public class UnlockFrame extends JFrame { } ); } - void updateUser(@Nullable User user) { + void updateUser(@Nullable final User user) { this.user = user; checkSignIn(); } @@ -152,10 +151,10 @@ public class UnlockFrame extends JFrame { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { - String fullName = user == null? "": user.getFullName(); + String fullName = (user == null)? "": user.getFullName(); char[] masterPassword = authenticationPanel.getMasterPassword(); - if (fullName.isEmpty() || masterPassword.length == 0) { + if (fullName.isEmpty() || (masterPassword.length == 0)) { identiconLabel.setText( " " ); return; } @@ -169,9 +168,9 @@ public class UnlockFrame extends JFrame { } }, 300, TimeUnit.MILLISECONDS ); - String fullName = user == null? "": user.getFullName(); + String fullName = (user == null)? "": user.getFullName(); char[] masterPassword = authenticationPanel.getMasterPassword(); - boolean enabled = !fullName.isEmpty() && masterPassword.length > 0; + boolean enabled = !fullName.isEmpty() && (masterPassword.length > 0); signInButton.setEnabled( enabled ); return enabled; @@ -181,7 +180,7 @@ public class UnlockFrame extends JFrame { if (!checkSignIn()) return; - for (JComponent signInComponent : signInComponents) + for (final JComponent signInComponent : signInComponents) signInComponent.setEnabled( false ); signInButton.setEnabled( false ); @@ -208,7 +207,7 @@ public class UnlockFrame extends JFrame { JOptionPane.showMessageDialog( null, e.getLocalizedMessage(), "Sign In Failed", JOptionPane.ERROR_MESSAGE ); authenticationPanel.reset(); signInButton.setText( "Sign In" ); - for (JComponent signInComponent : signInComponents) + for (final JComponent signInComponent : signInComponents) signInComponent.setEnabled( true ); checkSignIn(); }