Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Implement digester for SHA-256 #2

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 23 additions & 15 deletions src/main/java/org/codehaus/plexus/digest/ChecksumFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
public class ChecksumFile
{
/**
* @plexus.requirement role-hint="sha256"
*/
private Digester digestSha256;

/**
* @plexus.requirement role-hint="sha1"
*/
Expand Down Expand Up @@ -82,31 +87,34 @@ public boolean isValidChecksum( File checksumFile ) throws DigesterException, Fi
}

String path = checksumFile.getAbsolutePath();
Digester digester = null;
final Digester digester = findDigesterByFileSuffix(path);

File referenceFile = new File( path.substring( 0, path.length() - digester.getFilenameExtension().length() ) );

String rawChecksum = FileUtils.fileRead( checksumFile, "UTF-8" );
String expectedChecksum = DigestUtils.cleanChecksum( rawChecksum, digester, referenceFile.getName() );

String actualChecksum = digester.calc( referenceFile );

return StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum );
}

private Digester findDigesterByFileSuffix(String path) throws DigesterException {
if ( path.endsWith( digestMd5.getFilenameExtension() ) )
{
digester = digestMd5;
return digestMd5;
}
else if ( path.endsWith( digestSha1.getFilenameExtension() ) )
{
digester = digestSha1;
return digestSha1;
}
// TODO: Add more digester implementations here.

if ( digester == null )
else if ( path.endsWith( digestSha256.getFilenameExtension() ) )
{
throw new DigesterException( "Unable to determine digester type from filename " + path );
return digestSha256;
}
// TODO: Add more digester implementations here.

File referenceFile = new File( path.substring( 0, path.length() - digester.getFilenameExtension().length() ) );

String rawChecksum = FileUtils.fileRead( checksumFile, "UTF-8" );
String expectedChecksum = DigestUtils.cleanChecksum( rawChecksum, digester, referenceFile.getName() );

String actualChecksum = digester.calc( referenceFile );

return StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum );
throw new DigesterException( "Unable to determine digester type from filename " + path );
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/codehaus/plexus/digest/Sha256Digester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.codehaus.plexus.digest;

/*
* Copyright 2001-2006 The Codehaus.
*
* 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.
*/

/**
* Digester that does SHA256 Message Digesting Only.
*
* @plexus.component role="org.codehaus.plexus.digest.Digester" role-hint="sha256"
*/
public class Sha256Digester
extends AbstractDigester
{
public String getFilenameExtension()
{
return ".sha256";
}

public Sha256Digester()
{
super( new StreamingSha256Digester() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.codehaus.plexus.digest;

/*
* Copyright 2001-2006 The Codehaus.
*
* 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.
*/

/**
* An SHA-256 implementation of the streaming digester.
*
* @plexus.component role="org.codehaus.plexus.digest.StreamingDigester" role-hint="sha256"
*/
public class StreamingSha256Digester
extends AbstractStreamingDigester
{
public StreamingSha256Digester()
{
super( "SHA-256" );
}
}
1 change: 1 addition & 0 deletions src/test/examples/openssl.jar.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SHA256(openssl.jar)= c8708900fd00167e6912c5e99c6bddaf5526a7f0c9ce78ce6a0cc08c2713d32a
1 change: 1 addition & 0 deletions src/test/examples/plain.jar.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c8708900fd00167e6912c5e99c6bddaf5526a7f0c9ce78ce6a0cc08c2713d32a
1 change: 1 addition & 0 deletions src/test/examples/redback-authz-open.jar.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fb342200c89233cc37ba8e164189301fdbcf1b74c7ab54c662c4261cf615d743 redback-authz-open.jar
1 change: 1 addition & 0 deletions src/test/examples/single-space.jar.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c8708900fd00167e6912c5e99c6bddaf5526a7f0c9ce78ce6a0cc08c2713d32a single-space.jar
1 change: 1 addition & 0 deletions src/test/examples/space-asterisk.jar.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c8708900fd00167e6912c5e99c6bddaf5526a7f0c9ce78ce6a0cc08c2713d32a *space-asterisk.jar
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,23 @@ public void testIsValidChecksum()

assertTrue( checksum.isValidChecksum( new File( exampleDir, "redback-authz-open.jar.md5" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "redback-authz-open.jar.sha1" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "redback-authz-open.jar.sha256" ) ) );

assertTrue( checksum.isValidChecksum( new File( exampleDir, "plain.jar.md5" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "plain.jar.sha1" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "plain.jar.sha256" ) ) );

assertTrue( checksum.isValidChecksum( new File( exampleDir, "single-space.jar.md5" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "single-space.jar.sha1" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "single-space.jar.sha256" ) ) );

assertTrue( checksum.isValidChecksum( new File( exampleDir, "space-asterisk.jar.md5" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "space-asterisk.jar.sha1" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "space-asterisk.jar.sha256" ) ) );

assertTrue( checksum.isValidChecksum( new File( exampleDir, "openssl.jar.md5" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "openssl.jar.sha1" ) ) );
assertTrue( checksum.isValidChecksum( new File( exampleDir, "openssl.jar.sha256" ) ) );
}

public void testCreateChecksum()
Expand Down
Loading