Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added optional appended salt for checksum calculation #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

checksum-maven-plugin - http://checksum-maven-plugin.nicoulaj.net
Copyright © 2010-2017 checksum-maven-plugin contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -82,6 +77,8 @@
<maven-min-supported.version>3.0.4</maven-min-supported.version>
<bouncycastle.version>1.59</bouncycastle.version>
<plexus-utils.version>3.0.24</plexus-utils.version>
<apache-commons-io.version>1.3.2</apache-commons-io.version>
<apache-commons-lang.version>3.7</apache-commons-lang.version>

<!-- Build settings -->
<debug>false</debug>
Expand Down Expand Up @@ -126,6 +123,16 @@
<artifactId>plexus-utils</artifactId>
<version>${plexus-utils.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${apache-commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache-commons-lang.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -150,6 +157,14 @@
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -271,11 +286,11 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/HelpMojo.class</exclude>
</excludes>
</configuration>
<configuration>
<excludes>
<exclude>**/HelpMojo.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>setup-coverage-unit-tests</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,8 +35,8 @@
* @since 1.0
*/
public class CRC32FileDigester
extends AbstractFileDigester
{
extends AbstractFileDigester {

/**
* The identifier of the algorithm supported by this implementation.
*/
Expand All @@ -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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will load the whole file in memory, also there are several unneeded array copies, I think this can be done without much overhead but just chaining the file input stream with a salt ByteArrayInputStream

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public abstract class AbstractExecution
*/
protected boolean failIfNoTargets = true;

protected String salt;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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() ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions src/test/resources/salt/salt-target.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is sample file for calculation with the salt KITTYCAT.
1 change: 1 addition & 0 deletions src/test/resources/salt/salt-target.txt.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ae30210ed29a0192980efea77894ca8dc1c5a975f07a4844f869ede0463bf029