This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e38a37
commit 2f49c4f
Showing
18 changed files
with
274 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.codehaus.plexus</groupId> | ||
<artifactId>plexus-components</artifactId> | ||
<version>14.2</version> | ||
<relativePath/> | ||
<relativePath /> | ||
</parent> | ||
|
||
<artifactId>plexus-digest</artifactId> | ||
|
@@ -16,8 +17,8 @@ | |
<scm> | ||
<connection>scm:git:[email protected]:codehaus-plexus/plexus-digest.git</connection> | ||
<developerConnection>scm:git:[email protected]:codehaus-plexus/plexus-digest.git</developerConnection> | ||
<url>http://github.com/codehaus-plexus/plexus-digest</url> | ||
<tag>HEAD</tag> | ||
<url>http://github.com/codehaus-plexus/plexus-digest</url> | ||
</scm> | ||
<issueManagement> | ||
<system>github</system> | ||
|
@@ -65,15 +66,17 @@ | |
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-scm-publish-plugin</artifactId> | ||
<configuration> | ||
<content>${project.reporting.outputDirectory}</content><!-- mono-module doesn't require site:stage --> | ||
<content>${project.reporting.outputDirectory}</content> | ||
<!-- mono-module doesn't require site:stage --> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>scm-publish</id> | ||
<phase>site-deploy</phase><!-- deploy site with maven-scm-publish-plugin --> | ||
<!-- deploy site with maven-scm-publish-plugin --> | ||
<goals> | ||
<goal>publish-scm</goal> | ||
</goals> | ||
<phase>site-deploy</phase> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,30 +16,27 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import org.codehaus.plexus.util.IOUtil; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
import org.codehaus.plexus.util.IOUtil; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
/** | ||
* Create a digest for a file. | ||
* | ||
* @author <a href="mailto:[email protected]">Brett Porter</a> | ||
*/ | ||
public abstract class AbstractDigester | ||
implements Digester | ||
{ | ||
public abstract class AbstractDigester implements Digester { | ||
private final StreamingDigester streamingDigester; | ||
|
||
/** | ||
* <p>Constructor for AbstractDigester.</p> | ||
* | ||
* @param streamingDigester a {@link org.codehaus.plexus.digest.StreamingDigester} object. | ||
*/ | ||
protected AbstractDigester( StreamingDigester streamingDigester ) | ||
{ | ||
protected AbstractDigester(StreamingDigester streamingDigester) { | ||
this.streamingDigester = streamingDigester; | ||
} | ||
|
||
|
@@ -48,46 +45,36 @@ protected AbstractDigester( StreamingDigester streamingDigester ) | |
* | ||
* @return a {@link java.lang.String} object. | ||
*/ | ||
public String getAlgorithm() | ||
{ | ||
public String getAlgorithm() { | ||
return streamingDigester.getAlgorithm(); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public String calc( File file ) | ||
throws DigesterException | ||
{ | ||
public String calc(File file) throws DigesterException { | ||
FileInputStream fis = null; | ||
try | ||
{ | ||
fis = new FileInputStream( file ); | ||
try { | ||
fis = new FileInputStream(file); | ||
streamingDigester.reset(); | ||
streamingDigester.update( fis ); | ||
streamingDigester.update(fis); | ||
return streamingDigester.calc(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new DigesterException( "Unable to calculate the " + streamingDigester.getAlgorithm() + | ||
" hashcode for " + file.getAbsolutePath() + ": " + e.getMessage(), e ); | ||
} | ||
finally | ||
{ | ||
IOUtil.close( fis ); | ||
} catch (IOException e) { | ||
throw new DigesterException( | ||
"Unable to calculate the " + streamingDigester.getAlgorithm() + " hashcode for " | ||
+ file.getAbsolutePath() + ": " + e.getMessage(), | ||
e); | ||
} finally { | ||
IOUtil.close(fis); | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public void verify( File file, String checksum ) | ||
throws DigesterException | ||
{ | ||
String trimmedChecksum = | ||
DigestUtils.cleanChecksum( checksum, streamingDigester.getAlgorithm(), file.getName() ); | ||
public void verify(File file, String checksum) throws DigesterException { | ||
String trimmedChecksum = DigestUtils.cleanChecksum(checksum, streamingDigester.getAlgorithm(), file.getName()); | ||
|
||
//Create checksum for file | ||
String sum = calc( file ); | ||
if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) ) | ||
{ | ||
throw new DigesterException( "Checksum failed (expected=" + trimmedChecksum + ", actual=" + sum + ")" ); | ||
// Create checksum for file | ||
String sum = calc(file); | ||
if (!StringUtils.equalsIgnoreCase(trimmedChecksum, sum)) { | ||
throw new DigesterException("Checksum failed (expected=" + trimmedChecksum + ", actual=" + sum + ")"); | ||
} | ||
} | ||
|
||
|
@@ -96,8 +83,7 @@ public void verify( File file, String checksum ) | |
* | ||
* @return a {@link java.lang.String} object. | ||
*/ | ||
public String toString() | ||
{ | ||
public String toString() { | ||
return "[Digester:" + streamingDigester.getAlgorithm() + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,7 @@ | |
* | ||
* @author <a href="mailto:[email protected]">Brett Porter</a> | ||
*/ | ||
public abstract class AbstractStreamingDigester | ||
implements StreamingDigester | ||
{ | ||
public abstract class AbstractStreamingDigester implements StreamingDigester { | ||
protected final MessageDigest md; | ||
|
||
private static final int BUFFER_SIZE = 32768; | ||
|
@@ -38,16 +36,12 @@ public abstract class AbstractStreamingDigester | |
* | ||
* @param algorithm a {@link java.lang.String} object. | ||
*/ | ||
protected AbstractStreamingDigester( String algorithm ) | ||
{ | ||
try | ||
{ | ||
md = MessageDigest.getInstance( algorithm ); | ||
} | ||
catch ( NoSuchAlgorithmException e ) | ||
{ | ||
throw new IllegalStateException( "Unable to initialize digest algorithm " + algorithm + " : " | ||
+ e.getMessage() ); | ||
protected AbstractStreamingDigester(String algorithm) { | ||
try { | ||
md = MessageDigest.getInstance(algorithm); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException( | ||
"Unable to initialize digest algorithm " + algorithm + " : " + e.getMessage()); | ||
} | ||
} | ||
|
||
|
@@ -56,8 +50,7 @@ protected AbstractStreamingDigester( String algorithm ) | |
* | ||
* @return a {@link java.lang.String} object. | ||
*/ | ||
public String getAlgorithm() | ||
{ | ||
public String getAlgorithm() { | ||
return md.getAlgorithm(); | ||
} | ||
|
||
|
@@ -67,28 +60,22 @@ public String getAlgorithm() | |
* @return a {@link java.lang.String} object. | ||
* @throws org.codehaus.plexus.digest.DigesterException if any. | ||
*/ | ||
public String calc() | ||
throws DigesterException | ||
{ | ||
return calc( this.md ); | ||
public String calc() throws DigesterException { | ||
return calc(this.md); | ||
} | ||
|
||
/** | ||
* <p>reset.</p> | ||
* | ||
* @throws org.codehaus.plexus.digest.DigesterException if any. | ||
*/ | ||
public void reset() | ||
throws DigesterException | ||
{ | ||
public void reset() throws DigesterException { | ||
md.reset(); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public void update( InputStream is ) | ||
throws DigesterException | ||
{ | ||
update( is, md ); | ||
public void update(InputStream is) throws DigesterException { | ||
update(is, md); | ||
} | ||
|
||
/** | ||
|
@@ -97,9 +84,8 @@ public void update( InputStream is ) | |
* @param md a {@link java.security.MessageDigest} object. | ||
* @return a {@link java.lang.String} object. | ||
*/ | ||
protected static String calc( MessageDigest md ) | ||
{ | ||
return Hex.encode( md.digest() ); | ||
protected static String calc(MessageDigest md) { | ||
return Hex.encode(md.digest()); | ||
} | ||
|
||
/** | ||
|
@@ -109,22 +95,16 @@ protected static String calc( MessageDigest md ) | |
* @param digest a {@link java.security.MessageDigest} object. | ||
* @throws org.codehaus.plexus.digest.DigesterException if any. | ||
*/ | ||
protected static void update( InputStream is, MessageDigest digest ) | ||
throws DigesterException | ||
{ | ||
try | ||
{ | ||
protected static void update(InputStream is, MessageDigest digest) throws DigesterException { | ||
try { | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int size = is.read( buffer, 0, BUFFER_SIZE ); | ||
while ( size >= 0 ) | ||
{ | ||
digest.update( buffer, 0, size ); | ||
size = is.read( buffer, 0, BUFFER_SIZE ); | ||
int size = is.read(buffer, 0, BUFFER_SIZE); | ||
while (size >= 0) { | ||
digest.update(buffer, 0, size); | ||
size = is.read(buffer, 0, BUFFER_SIZE); | ||
} | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new DigesterException( "Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e ); | ||
} catch (IOException e) { | ||
throw new DigesterException("Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,33 +16,33 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import org.codehaus.plexus.util.FileUtils; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
import org.codehaus.plexus.util.FileUtils; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
/** | ||
* ChecksumFile | ||
* | ||
* @author <a href="mailto:[email protected]">Joakim Erdfelt</a> | ||
*/ | ||
@Named | ||
public class ChecksumFile | ||
{ | ||
public class ChecksumFile { | ||
@Inject | ||
@Named ( "sha256" ) | ||
@Named("sha256") | ||
private Digester digestSha256; | ||
|
||
@Inject | ||
@Named ( "sha1" ) | ||
@Named("sha1") | ||
private Digester digestSha1; | ||
|
||
@Inject | ||
@Named ( "md5" ) | ||
@Named("md5") | ||
private Digester digestMd5; | ||
|
||
/** | ||
|
@@ -67,47 +67,40 @@ public class ChecksumFile | |
* @throws java.io.FileNotFoundException if the checksumFile itself or the file it refers to is not found. | ||
* @throws java.io.IOException if the reading of the checksumFile or the file it refers to fails. | ||
*/ | ||
public boolean isValidChecksum( File checksumFile ) throws DigesterException, FileNotFoundException, IOException | ||
{ | ||
if ( !checksumFile.exists() ) | ||
{ | ||
throw new FileNotFoundException( "Unable to find checksum file " + checksumFile.getAbsolutePath() ); | ||
public boolean isValidChecksum(File checksumFile) throws DigesterException, FileNotFoundException, IOException { | ||
if (!checksumFile.exists()) { | ||
throw new FileNotFoundException("Unable to find checksum file " + checksumFile.getAbsolutePath()); | ||
} | ||
|
||
if ( !checksumFile.isFile() ) | ||
{ | ||
throw new IOException( "Unable to load checksum from non-file " + checksumFile.getAbsolutePath() ); | ||
if (!checksumFile.isFile()) { | ||
throw new IOException("Unable to load checksum from non-file " + checksumFile.getAbsolutePath()); | ||
} | ||
|
||
String path = checksumFile.getAbsolutePath(); | ||
Digester digester = findDigesterByFileSuffix(path); | ||
|
||
File referenceFile = new File( path.substring( 0, path.length() - digester.getFilenameExtension().length() ) ); | ||
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 rawChecksum = FileUtils.fileRead(checksumFile, "UTF-8"); | ||
String expectedChecksum = DigestUtils.cleanChecksum(rawChecksum, digester, referenceFile.getName()); | ||
|
||
String actualChecksum = digester.calc( referenceFile ); | ||
String actualChecksum = digester.calc(referenceFile); | ||
|
||
return StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum ); | ||
return StringUtils.equalsIgnoreCase(expectedChecksum, actualChecksum); | ||
} | ||
|
||
private Digester findDigesterByFileSuffix(String path) throws DigesterException { | ||
if ( path.endsWith( digestMd5.getFilenameExtension() ) ) | ||
{ | ||
if (path.endsWith(digestMd5.getFilenameExtension())) { | ||
return digestMd5; | ||
} | ||
else if ( path.endsWith( digestSha1.getFilenameExtension() ) ) | ||
{ | ||
} else if (path.endsWith(digestSha1.getFilenameExtension())) { | ||
return digestSha1; | ||
} | ||
else if ( path.endsWith( digestSha256.getFilenameExtension() ) ) | ||
{ | ||
} else if (path.endsWith(digestSha256.getFilenameExtension())) { | ||
return digestSha256; | ||
} | ||
// TODO: Add more digester implementations here. | ||
|
||
throw new DigesterException( "Unable to determine digester type from filename " + path ); | ||
throw new DigesterException("Unable to determine digester type from filename " + path); | ||
} | ||
|
||
/** | ||
|
@@ -119,11 +112,10 @@ else if ( path.endsWith( digestSha256.getFilenameExtension() ) ) | |
* @throws org.codehaus.plexus.digest.DigesterException if there was a problem calculating the checksum of the referenceFile. | ||
* @throws java.io.IOException if there was a problem either reading the referenceFile, or writing the checksum file. | ||
*/ | ||
public File createChecksum( File referenceFile, Digester digester ) throws DigesterException, IOException | ||
{ | ||
File checksumFile = new File( referenceFile.getAbsolutePath() + digester.getFilenameExtension() ); | ||
String checksum = digester.calc( referenceFile ); | ||
FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum + " " + referenceFile.getName() ); | ||
public File createChecksum(File referenceFile, Digester digester) throws DigesterException, IOException { | ||
File checksumFile = new File(referenceFile.getAbsolutePath() + digester.getFilenameExtension()); | ||
String checksum = digester.calc(referenceFile); | ||
FileUtils.fileWrite(checksumFile.getAbsolutePath(), "UTF-8", checksum + " " + referenceFile.getName()); | ||
return checksumFile; | ||
} | ||
} |
Oops, something went wrong.