diff --git a/pom.xml b/pom.xml index 57d98bd0..57b656f1 100644 --- a/pom.xml +++ b/pom.xml @@ -1,21 +1,16 @@ 4.0.0 @@ -82,6 +77,8 @@ 3.0.4 1.59 3.0.24 + 1.3.2 + 3.7 false @@ -126,6 +123,16 @@ plexus-utils ${plexus-utils.version} + + org.apache.commons + commons-io + ${apache-commons-io.version} + + + org.apache.commons + commons-lang3 + ${apache-commons-lang.version} + @@ -150,6 +157,14 @@ org.bouncycastle bcprov-jdk15on + + org.apache.commons + commons-io + + + org.apache.commons + commons-lang3 + junit junit @@ -271,11 +286,11 @@ org.jacoco jacoco-maven-plugin - - - **/HelpMojo.class - - + + + **/HelpMojo.class + + setup-coverage-unit-tests diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CRC32FileDigester.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CRC32FileDigester.java index 82ab3a6b..05d1b3ab 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CRC32FileDigester.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CRC32FileDigester.java @@ -16,12 +16,16 @@ */ package net.nicoulaj.maven.plugins.checksum.digest; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.ArrayUtils; /** * Implementation of {@link FileDigester} for the CRC32 algorithm. @@ -31,8 +35,8 @@ * @since 1.0 */ public class CRC32FileDigester - extends AbstractFileDigester -{ + extends AbstractFileDigester { + /** * The identifier of the algorithm supported by this implementation. */ @@ -41,42 +45,40 @@ public class CRC32FileDigester /** * Build a new instance of {@link CRC32FileDigester}. */ - public CRC32FileDigester() - { - super( ALGORITHM ); + public CRC32FileDigester() { + super(ALGORITHM); } /** * {@inheritDoc} */ - public String calculate( File file ) - throws DigesterException - { + public String calculate(File file, String salt) + throws DigesterException { CheckedInputStream cis; - try - { - cis = new CheckedInputStream( new FileInputStream( file ), new CRC32() ); - } - catch ( FileNotFoundException e ) - { - throw new DigesterException( "Unable to read " + file.getPath() + ": " + e.getMessage() ); + try { + byte[] bytes = IOUtils.toByteArray(new FileInputStream(file)); + if(!(null == salt)){ + if(!salt.isEmpty()){ + bytes = ArrayUtils.addAll(bytes, salt.getBytes()); + } + } + InputStream bais = new ByteArrayInputStream(bytes); + cis = new CheckedInputStream(bais, new CRC32()); + } catch (IOException e) { + throw new DigesterException("Unable to read " + file.getPath() + ": " + e.getMessage()); } byte[] buf = new byte[STREAMING_BUFFER_SIZE]; - try - { - while ( cis.read( buf ) >= 0 ) - { + try { + while (cis.read(buf) >= 0) { continue; } - } - catch ( IOException e ) - { + } catch (IOException e) { throw new DigesterException( "Unable to calculate the " + getAlgorithm() + " hashcode for " + file.getPath() + ": " - + e.getMessage() ); + + e.getMessage()); } - return Long.toString( cis.getChecksum().getValue() ); + return Long.toString(cis.getChecksum().getValue()); } } diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CksumFileDigester.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CksumFileDigester.java index cea23d95..5b1a6110 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CksumFileDigester.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/CksumFileDigester.java @@ -45,8 +45,14 @@ public CksumFileDigester() } @Override - public String calculate(File file) throws DigesterException + public String calculate(File file, String salt) throws DigesterException { + if(!(null == salt)){ + if(!salt.isEmpty()){ + throw new DigesterException( "Adding a salt is currently not supported for algorithm Cksum!" ); + } + } + int value = 0; long length = 0L; BufferedInputStream fis; diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/FileDigester.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/FileDigester.java index 364cd167..31681155 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/FileDigester.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/FileDigester.java @@ -52,6 +52,6 @@ public interface FileDigester * @return the current checksum. * @throws DigesterException if there was a problem computing the hashcode. */ - String calculate( File file ) + String calculate( File file, String salt ) throws DigesterException; } diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/MessageDigestFileDigester.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/MessageDigestFileDigester.java index 67717d6c..5e563c92 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/MessageDigestFileDigester.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/digest/MessageDigestFileDigester.java @@ -55,7 +55,7 @@ protected MessageDigestFileDigester( String algorithm ) /** * {@inheritDoc} */ - public String calculate( File file ) + public String calculate( File file, String salt ) throws DigesterException { // Try to open the file. @@ -84,7 +84,12 @@ public String calculate( File file ) messageDigest.update( buffer, 0, size ); size = fis.read( buffer, 0, STREAMING_BUFFER_SIZE ); } - + // Add salt at the end of file + if(!(null == salt)){ + if(!salt.isEmpty()){ + messageDigest.update(salt.getBytes()); + } + } result = new String( Hex.encode( messageDigest.digest() ) ); } catch ( IOException e ) diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/AbstractExecution.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/AbstractExecution.java index 604961ef..109c5627 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/AbstractExecution.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/AbstractExecution.java @@ -67,6 +67,8 @@ public abstract class AbstractExecution */ protected boolean failIfNoTargets = true; + protected String salt; + /** * {@inheritDoc} */ @@ -248,6 +250,14 @@ public void setFailIfNoTargets(boolean failIfNoTargets) { this.failIfNoTargets = failIfNoTargets; } + public String getSalt() { + return salt; + } + + public void setSalt(String salt) { + this.salt = salt; + } + /** * {@inheritDoc} */ diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/Execution.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/Execution.java index 3da27d7e..fecc6c26 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/Execution.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/Execution.java @@ -178,6 +178,18 @@ public interface Execution */ void setFailIfNoTargets(boolean failIfNoTargets); + /** + * Gets the specified checksum salt + * @return the Salt set before + */ + String getSalt(); + + /** + * Sets a Salt to append to files during checksum creation + * @param salt + */ + void setSalt(String salt); + /** * Check that an execution can be run with the {@link #run()} method. * diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/FailOnErrorExecution.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/FailOnErrorExecution.java index c7a8111b..27848ae0 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/FailOnErrorExecution.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/FailOnErrorExecution.java @@ -69,7 +69,7 @@ public void run() { // Calculate the hash for the file/algo FileDigester digester = DigesterFactory.getInstance().getFileDigester( algorithm ); - String hash = digester.calculate( file.getFile() ); + String hash = digester.calculate( file.getFile(), getSalt() ); // Write it to each target defined for ( ExecutionTarget target : getTargets() ) diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/NeverFailExecution.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/NeverFailExecution.java index 4f8ebd0f..e1033889 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/NeverFailExecution.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/execution/NeverFailExecution.java @@ -99,7 +99,7 @@ public void run() { // Calculate the hash for the file/algo FileDigester digester = DigesterFactory.getInstance().getFileDigester( algorithm ); - String hash = digester.calculate( file.getFile() ); + String hash = digester.calculate( file.getFile(), getSalt() ); // Write it to each target defined for ( ExecutionTarget target : getTargets() ) diff --git a/src/main/java/net/nicoulaj/maven/plugins/checksum/mojo/AbstractChecksumMojo.java b/src/main/java/net/nicoulaj/maven/plugins/checksum/mojo/AbstractChecksumMojo.java index c22549e6..8e78a606 100644 --- a/src/main/java/net/nicoulaj/maven/plugins/checksum/mojo/AbstractChecksumMojo.java +++ b/src/main/java/net/nicoulaj/maven/plugins/checksum/mojo/AbstractChecksumMojo.java @@ -152,6 +152,12 @@ abstract class AbstractChecksumMojo @Parameter( defaultValue = "" ) protected String relativeSubPath = ""; + /** + * Salt to append at the end of files before checksums are created + */ + @Parameter(property = "salt") + protected String salt = ""; + /** * Constructor. * @@ -179,6 +185,7 @@ public void execute() execution.setFailIfNoFiles(failIfNoFiles); execution.setFailIfNoAlgorithms(failIfNoAlgorithms); execution.setFailIfNoTargets(failIfNoTargets); + execution.setSalt(salt); if ( !quiet ) { execution.addTarget( new MavenLogTarget( getLog() ) ); diff --git a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/Constants.java b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/Constants.java index 9139f8ba..593ab649 100644 --- a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/Constants.java +++ b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/Constants.java @@ -40,4 +40,9 @@ public class Constants * Path of the sample files hashcodes directory. */ public static final String SAMPLE_FILES_HASHCODES_PATH = RESOURCES_PATH + File.separator + "hashcodes"; + + /** + * Path of the sample files for salt checking + */ + public static final String SAMPLE_FILES_SALT_PATH = RESOURCES_PATH + File.separator + "salt"; } diff --git a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/CksumFileDigesterTest.java b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/CksumFileDigesterTest.java index 9bdac7c7..768910b2 100644 --- a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/CksumFileDigesterTest.java +++ b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/CksumFileDigesterTest.java @@ -34,7 +34,7 @@ public void testEmptyFileCksum() throws Exception { File empty_file = File.createTempFile("empty_file", null); CksumFileDigester cksumDigester=new CksumFileDigester(); - String cksumEmpty = cksumDigester.calculate(empty_file); + String cksumEmpty = cksumDigester.calculate(empty_file, ""); Assert.assertEquals("4294967295", cksumEmpty); diff --git a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/DigestersTest.java b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/DigestersTest.java index 933c0ffa..be82bd53 100644 --- a/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/DigestersTest.java +++ b/src/test/java/net/nicoulaj/maven/plugins/checksum/test/unit/digest/DigestersTest.java @@ -116,7 +116,7 @@ public void testFilenameExtensionDefined() * * @throws DigesterException if there was a problem while calculating the checksum. * @throws IOException if there was a problem reading the file containing the pre-calculated checksum. - * @see net.nicoulaj.maven.plugins.checksum.digest.FileDigester#calculate(java.io.File) + * @see net.nicoulaj.maven.plugins.checksum.digest.FileDigester#calculate(java.io.File, String) */ @Test public void testCalculate() @@ -127,7 +127,7 @@ public void testCalculate() { String referenceFile = Constants.SAMPLE_FILES_HASHCODES_PATH + File.separator + testFile.getName() + digester.getFileExtension(); - String calculatedHash = digester.calculate( testFile ); + String calculatedHash = digester.calculate( testFile, "" ); if (GENERATE_REFS) FileUtils.fileWrite(referenceFile, calculatedHash); String correctHash = FileUtils.fileRead(referenceFile); @@ -141,13 +141,29 @@ public void testCalculate() * Check an exception is thrown when attempting to calculate the checksum of a file that does not exist. * * @throws DigesterException should always happen. - * @see net.nicoulaj.maven.plugins.checksum.digest.FileDigester#calculate(java.io.File) + * @see net.nicoulaj.maven.plugins.checksum.digest.FileDigester#calculate(java.io.File, String) */ @Test public void testCalculateExceptionThrownOnFileNotFound() throws DigesterException { exception.expect( DigesterException.class ); - digester.calculate( new File( "some/path/that/does/not/exist" ) ); + digester.calculate( new File( "some/path/that/does/not/exist" ), "" ); + } + + /** + * Tests the patched salt functionality + */ + @Test + public void testWithSalt() throws IOException, NoSuchAlgorithmException, DigesterException { + + File testFile = new File(Constants.SAMPLE_FILES_SALT_PATH + File.separator + "salt-target.txt"); + String referenceHash = FileUtils.fileRead(Constants.SAMPLE_FILES_SALT_PATH + File.separator + "salt-target.txt.sha256"); + DigesterFactory factory = DigesterFactory.getInstance(); + FileDigester sha256 = factory.getFileDigester("sha256"); + String calculatedHashWithSalt = sha256.calculate(testFile, "KITTYCAT"); + String calculatedHashWithWrongSalt = sha256.calculate(testFile, "KITTYCATS"); + Assert.assertEquals(calculatedHashWithSalt, referenceHash); + Assert.assertNotEquals(calculatedHashWithWrongSalt, referenceHash); } } diff --git a/src/test/resources/salt/salt-target.txt b/src/test/resources/salt/salt-target.txt new file mode 100644 index 00000000..923de80f --- /dev/null +++ b/src/test/resources/salt/salt-target.txt @@ -0,0 +1 @@ +This is sample file for calculation with the salt KITTYCAT. diff --git a/src/test/resources/salt/salt-target.txt.sha256 b/src/test/resources/salt/salt-target.txt.sha256 new file mode 100644 index 00000000..46d6ac59 --- /dev/null +++ b/src/test/resources/salt/salt-target.txt.sha256 @@ -0,0 +1 @@ +ae30210ed29a0192980efea77894ca8dc1c5a975f07a4844f869ede0463bf029 \ No newline at end of file