Skip to content

Commit

Permalink
Add checkstyle rules and check
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfav committed Nov 17, 2017
1 parent bf8ecea commit b654868
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ before_install:
- tar xvf secrets.tar

script:
- mvn clean install -X -Djarsigner.skip=false
- mvn clean install -X -Djarsigner.skip=false checkstyle:check

after_success:
- mvn test jacoco:report coveralls:report

deploy:
- provider: script
script: mvn -s ci-settings.xml deploy -X
script: mvn -s ci-settings.xml deploy -X checkstyle:checkstyle
skip_cleanup: true
on:
branch: master
Expand Down
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributing to HKDF

We ❤ pull requests from everyone.

If possible proof features and bugfixes with unit tests.
This repo validates against checkstyle (import the xml found in the root to your IDE if possible)

To run the tests (and checkstyle):

```shell
mvn test checkstyle:check
```

Tests are automatically run against branches and pull requests
via TravisCI, so you can also depend on that.
125 changes: 125 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" ?><!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
<module name="NewlineAtEndOfFile"/> <!-- force newline, important for git merge and POSIX compatibility: http://checkstyle.sourceforge.net/config_misc.html#NewlineAtEndOfFile -->
<module name="FileTabCharacter"/> <!-- e.g. disallow tab character outside of strings -->
<module name="UniqueProperties"><!-- must not have duplicate properties in .properties files: http://checkstyle.sourceforge.net/config_misc.html#UniqueProperties -->
<property name="fileExtensions" value="properties"/>
</module>

<module name="FileLength"><!-- max line length for single file: http://checkstyle.sourceforge.net/config_sizes.html#FileLength -->
<property name="max" value="1500"/>
</module>

<module name="TreeWalker">
<module name="SuppressionCommentFilter"/> <!-- use //CHECKSTYLE:OFF (...) //CHECKSTYLE:ON to disable checkstyle: http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter -->

<!-- Annotations -->
<module name="MissingDeprecated"> <!-- if @deprecated and javadoc is there, must be explained in javadoc: http://checkstyle.sourceforge.net/config_annotation.html#MissingDeprecated -->
<property name="skipNoJavadoc" value="true"/>
</module>
<module name="MissingOverride"/> <!-- if has @inheritDoc in javadoc must have @Override http://checkstyle.sourceforge.net/config_annotation.html#MissingOverride -->
<module name="PackageAnnotation"/> <!-- must only be in package-info: http://checkstyle.sourceforge.net/config_annotation.html#PackageAnnotation -->

<!-- Blocks -->
<module name="AvoidNestedBlocks"/> <!-- fails for useless {...} blocks: http://checkstyle.sourceforge.net/config_blocks.html#AvoidNestedBlocks -->
<module name="EmptyCatchBlock"> <!-- empty catch blocks exception var name must be 'ignored' or must not be empty: http://checkstyle.sourceforge.net/config_blocks.html#EmptyCatchBlock -->
<property name="exceptionVariableName" value="expected|ignore"/>
</module>

<!-- Misc -->
<module name="ArrayTypeStyle"/> <!-- e.g. int[] array is ok int array[] not: http://checkstyle.sourceforge.net/config_misc.html#ArrayTypeStyle -->
<module
name="MutableException"/> <!-- e.g. int[] array is ok int array[] not: http://checkstyle.sourceforge.net/config_misc.html#ArrayTypeStyle -->
<module name="UpperEll"/> <!-- long values must be postfixed with 'L' not 'l': http://checkstyle.sourceforge.net/config_misc.html#UpperEll -->
<module name="Indentation"> <!-- Checks correct indentation of Java code: http://checkstyle.sourceforge.net/config_misc.html#Indentation -->
<property name="basicOffset" value="4"/>
<property name="arrayInitIndent" value="8"/>
<property name="braceAdjustment" value="0"/>
<property name="caseIndent" value="4"/>
<property name="throwsIndent" value="4"/>
<property name="lineWrappingIndentation" value="4"/>
<property name="forceStrictCondition" value="false"/>
</module>

<!-- Modifier -->
<module name="ModifierOrder"/> <!-- Checks that the order of modifiers conforms to the suggestions in the Java Language specification: http://checkstyle.sourceforge.net/config_modifier.html#ModifierOrder -->
<module name="RedundantModifier"> <!-- Checks for redundant modifiers: http://checkstyle.sourceforge.net/config_modifier.html#RedundantModifier -->
<property name="tokens" value="METHOD_DEF, VARIABLE_DEF, INTERFACE_DEF, ANNOTATION_FIELD_DEF, ENUM_DEF, CLASS_DEF"/>
</module>

<!-- Classes -->
<module name="FinalClass"/> <!-- class with only private constructor must be final: http://checkstyle.sourceforge.net/config_design.html#FinalClass -->
<module
name="OneStatementPerLine"/> <!-- you cant write int i=1;int j=2; http://checkstyle.sourceforge.net/config_design.html#OneStatementPerLine -->
<module
name="SimplifyBooleanReturn"/> <!-- directly return boolean doe not check and return http://checkstyle.sourceforge.net/config_design.html#SimplifyBooleanReturn -->
<module
name="StringLiteralEquality"/> <!-- you cant write myString == "this" http://checkstyle.sourceforge.net/config_design.html#StringLiteralEquality -->
<module name="OneTopLevelClass"/> <!-- only one root class per file http://checkstyle.sourceforge.net/config_design.html#OneTopLevelClass -->
<module name="ThrowsCount"> <!-- max 5 throws definitions per method: http://checkstyle.sourceforge.net/config_design.html#ThrowsCount -->
<property name="max" value="5"/>
</module>
<module name="InterfaceIsType"/> <!-- interface must contain methods, should not be used for const only: http://checkstyle.sourceforge.net/config_design.html#InterfaceIsType -->
<module name="OuterTypeFilename"/> <!-- class Foo must be in Foo.java: http://checkstyle.sourceforge.net/config_misc.html#OuterTypeFilename -->

<module name="HideUtilityClassConstructor"/> <!-- utility class constructor must be private: http://checkstyle.sourceforge.net/config_design.html#HideUtilityClassConstructor -->
<module name="VisibilityModifier"> <!-- most members must be private http://checkstyle.sourceforge.net/config_design.html#VisibilityModifier -->
<property name="protectedAllowed" value="true"/>
<property name="packageAllowed" value="true"/>
<property name="allowPublicImmutableFields" value="true"/>
<property name="allowPublicFinalFields" value="true"/>
<property name="publicMemberPattern" value="^TAG$|^CREATOR$"/>
</module>

<!-- Coding -->
<module name="CovariantEquals"/> <!-- if you override equals with different type you must provide equals with same type: http://checkstyle.sourceforge.net/config_coding.html#CovariantEquals -->
<module name="DefaultComesLast"/> <!-- in switch case default must be the last elem: http://checkstyle.sourceforge.net/config_coding.html#DefaultComesLast -->
<module name="EmptyStatement"/> <!-- basically an empty semicolon: http://checkstyle.sourceforge.net/config_coding.html#EmptyStatement -->
<module name="EqualsHashCode"/> <!-- if you implement equals, you must implement hashcode and vice versa: http://checkstyle.sourceforge.net/config_coding.html#EqualsHashCode -->
<module name="NoFinalizer"/> <!-- Verifies there are no finalize() methods defined in a class: http://checkstyle.sourceforge.net/config_coding.html#NoFinalizer -->
<module name="FallThrough"/> <!-- switch fallthrough with statement not allowed http://checkstyle.sourceforge.net/config_coding.html#FallThrough -->
<module name="IllegalInstantiation"/> <!-- Must not use const of certain types (Activity, Fragment): http://checkstyle.sourceforge.net/config_coding.html#IllegalInstantiation -->

<!-- Size Limitiations -->
<module name="LineLength"><!-- max char length per line http://checkstyle.sourceforge.net/config_sizes.html#LineLength -->
<property name="max" value="300"/>
</module>
<module name="MethodLength"><!-- max line length for single method http://checkstyle.sourceforge.net/config_sizes.html#MethodLength -->
<property name="max" value="150"/>
</module>
<module name="AnonInnerLength"><!-- max line length for anon class http://checkstyle.sourceforge.net/config_sizes.html#AnonInnerLength -->
<property name="max" value="85"/>
</module>
<module name="ParameterNumber"><!-- max params for method http://checkstyle.sourceforge.net/config_sizes.html#ParameterNumber -->
<property name="max" value="10"/>
<property name="ignoreOverriddenMethods" value="true"/>
<property name="tokens" value="METHOD_DEF"/>
</module>

<!-- Naming Conventions -->
<!--<module name="ConstantName" /> for possible futer use-->

<!-- Whitespaces -->
<module name="EmptyLineSeparator"> <!-- Checks for correct empty line placements, omit VAR token: http://checkstyle.sourceforge.net/config_whitespace.html#EmptyLineSeparator -->
<property name="allowMultipleEmptyLines" value="false"/>
<property name="tokens" value="PACKAGE_DEF, IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF"/>
</module>
<module name="SingleSpaceSeparator"/> <!-- Checks if a token is surrounded by whitespace: http://checkstyle.sourceforge.net/config_whitespace.html#SingleSpaceSeparator -->
<module name="GenericWhitespace"/> <!-- Checks whitespaces with Java Generics <>: http://checkstyle.sourceforge.net/config_whitespace.html#GenericWhitespace -->
<module name="WhitespaceAround"> <!-- Checks if a token is surrounded by whitespace: http://checkstyle.sourceforge.net/config_whitespace.html#WhitespaceAround -->
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="allowEmptyTypes" value="true"/>
<property name="allowEmptyLambdas" value="true"/>
<property name="allowEmptyCatches" value="true"/>
</module>

<!-- Imports -->
<module name="RedundantImport"/> <!-- e.g. double import statements: http://checkstyle.sourceforge.net/config_imports.html#RedundantImport -->
<module name="UnusedImports"/> <!-- http://checkstyle.sourceforge.net/config_imports.html#UnusedImports -->
<module name="IllegalImport"/> <!-- checks if import sun.* is used http://checkstyle.sourceforge.net/config_imports.html#IllegalImport -->
</module>
</module>
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.4</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/at/favre/lib/crypto/HKDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package at.favre.lib.crypto;

import javax.crypto.Mac;
import java.nio.ByteBuffer;


/**
* A standards-compliant implementation of RFC 5869
* for HMAC-based Key Derivation Function.
Expand All @@ -44,6 +44,7 @@
* The HKDF Scheme</a>
* @see <a href="https://en.wikipedia.org/wiki/HKDF">Wikipedia: HKDF</a>
*/
@SuppressWarnings("WeakerAccess")
public final class HKDF {
/**
* Cache instances
Expand Down Expand Up @@ -276,4 +277,4 @@ byte[] execute(byte[] pseudoRandomKey, byte[] info, int outLengthBytes) {
return buffer.array();
}
}
}
}
1 change: 1 addition & 0 deletions src/main/java/at/favre/lib/crypto/HkdfMacFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package at.favre.lib.crypto;

import javax.crypto.Mac;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/at/favre/lib/crypto/HKDFBenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ public void benchmark() throws Exception {
System.out.println("Run (" + entry.getKey() + "): " + entry.getValue() + "ns");
}
}
}
}
6 changes: 3 additions & 3 deletions src/test/java/at/favre/lib/crypto/HKDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void quickStarTest() throws Exception {
@Test
public void simpleUseCase() throws Exception {
//if no dynamic salt is available, a static salt is better than null
byte[] staticSalt32Byte = new byte[]{(byte) 0xDA, (byte) 0xAC, 0x3E, 0x10, 0x55, (byte) 0xB5, (byte) 0xF1, 0x3E, 0x53, (byte) 0xE4, 0x70, (byte) 0xA8, 0x77, 0x79, (byte) 0x8E, 0x0A, (byte) 0x89, (byte) 0xAE, (byte) 0x96, 0x5F, 0x19, 0x5D, 0x53, 0x62, 0x58, (byte) 0x84, 0x2C, 0x09, (byte) 0xAD, 0x6E, 0x20, (byte) 0xD4};
byte[] staticSalt32Byte = new byte[]{(byte) 0xDA, (byte) 0xAC, 0x3E, 0x10, 0x55, (byte) 0xB5, (byte) 0xF1, 0x3E, 0x53, (byte) 0xE4, 0x70, (byte) 0xA8, 0x77, 0x79, (byte) 0x8E, 0x0A, (byte)
0x89, (byte) 0xAE, (byte) 0x96, 0x5F, 0x19, 0x5D, 0x53, 0x62, 0x58, (byte) 0x84, 0x2C, 0x09, (byte) 0xAD, 0x6E, 0x20, (byte) 0xD4};

//example input
String userInput = "this is a user input with bad entropy";
Expand Down Expand Up @@ -237,5 +238,4 @@ public void run() {
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS);
}

}
}
2 changes: 1 addition & 1 deletion src/test/java/at/favre/lib/crypto/HkdfMacFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ private void testHmacFactory(HkdfMacFactory macFactory, int refLength) {
byte[] hash = mac.doFinal();
assertEquals(refLength, hash.length);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/at/favre/lib/crypto/RFC5869TestCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ private void checkStep2(HkdfMacFactory macFactory, String prk, String info, int
Hex.decodeHex(info.toCharArray()), l);
assertArrayEquals(Hex.decodeHex(okm.toCharArray()), currentOkm);
}
}
}

0 comments on commit b654868

Please sign in to comment.