Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 872b4ca

Browse files
author
joakime
committed
Moving plexus-digest out of sandbox into plexus-components.
0 parents  commit 872b4ca

15 files changed

+902
-0
lines changed

pom.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>org.codehaus.plexus</groupId>
5+
<artifactId>plexus-components</artifactId>
6+
<version>1.1.6</version>
7+
</parent>
8+
9+
<artifactId>plexus-digest</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.codehaus.plexus</groupId>
17+
<artifactId>plexus-maven-plugin</artifactId>
18+
<executions>
19+
<execution>
20+
<goals>
21+
<goal>descriptor</goal>
22+
</goals>
23+
</execution>
24+
</executions>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.codehaus.plexus.digest;
2+
3+
/*
4+
* Copyright 2001-2006 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import org.codehaus.plexus.util.IOUtil;
20+
import org.codehaus.plexus.util.StringUtils;
21+
22+
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.io.IOException;
25+
26+
/**
27+
* Create a digest for a file.
28+
*
29+
* @author <a href="mailto:[email protected]">Brett Porter</a>
30+
*/
31+
public abstract class AbstractDigester
32+
implements Digester
33+
{
34+
private final StreamingDigester streamingDigester;
35+
36+
protected AbstractDigester( StreamingDigester streamingDigester )
37+
{
38+
this.streamingDigester = streamingDigester;
39+
}
40+
41+
public String getAlgorithm()
42+
{
43+
return streamingDigester.getAlgorithm();
44+
}
45+
46+
public String calc( File file )
47+
throws DigesterException
48+
{
49+
FileInputStream fis = null;
50+
try
51+
{
52+
fis = new FileInputStream( file );
53+
streamingDigester.reset();
54+
streamingDigester.update( fis );
55+
return streamingDigester.calc();
56+
}
57+
catch ( IOException e )
58+
{
59+
throw new DigesterException( "Unable to calculate the " + streamingDigester.getAlgorithm() +
60+
" hashcode for " + file.getAbsolutePath() + ": " + e.getMessage(), e );
61+
}
62+
finally
63+
{
64+
IOUtil.close( fis );
65+
}
66+
}
67+
68+
public void verify( File file, String checksum )
69+
throws DigesterException
70+
{
71+
String trimmedChecksum =
72+
DigestUtils.cleanChecksum( checksum, streamingDigester.getAlgorithm(), file.getName() );
73+
74+
//Create checksum for jar file
75+
String sum = calc( file );
76+
if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
77+
{
78+
throw new DigesterException( "Checksum failed" );
79+
}
80+
}
81+
82+
public String toString()
83+
{
84+
return "[Digester:" + streamingDigester.getAlgorithm() + "]";
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.codehaus.plexus.digest;
2+
3+
/*
4+
* Copyright 2001-2006 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.security.MessageDigest;
22+
import java.security.NoSuchAlgorithmException;
23+
24+
/**
25+
* Gradually create a digest for a stream.
26+
*
27+
* @author <a href="mailto:[email protected]">Brett Porter</a>
28+
*/
29+
public abstract class AbstractStreamingDigester
30+
implements StreamingDigester
31+
{
32+
protected final MessageDigest md;
33+
34+
private static final int BUFFER_SIZE = 32768;
35+
36+
protected AbstractStreamingDigester( String algorithm )
37+
{
38+
try
39+
{
40+
md = MessageDigest.getInstance( algorithm );
41+
}
42+
catch ( NoSuchAlgorithmException e )
43+
{
44+
throw new IllegalStateException( "Unable to initialize digest algorithm " + algorithm + " : "
45+
+ e.getMessage() );
46+
}
47+
}
48+
49+
public String getAlgorithm()
50+
{
51+
return md.getAlgorithm();
52+
}
53+
54+
public String calc()
55+
throws DigesterException
56+
{
57+
return calc( this.md );
58+
}
59+
60+
public void reset()
61+
throws DigesterException
62+
{
63+
md.reset();
64+
}
65+
66+
public void update( InputStream is )
67+
throws DigesterException
68+
{
69+
update( is, md );
70+
}
71+
72+
protected static String calc( MessageDigest md )
73+
{
74+
return Hex.encode( md.digest() );
75+
}
76+
77+
protected static void update( InputStream is, MessageDigest digest )
78+
throws DigesterException
79+
{
80+
try
81+
{
82+
byte[] buffer = new byte[BUFFER_SIZE];
83+
int size = is.read( buffer, 0, BUFFER_SIZE );
84+
while ( size >= 0 )
85+
{
86+
digest.update( buffer, 0, size );
87+
size = is.read( buffer, 0, BUFFER_SIZE );
88+
}
89+
}
90+
catch ( IOException e )
91+
{
92+
throw new DigesterException( "Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e );
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.codehaus.plexus.digest;
2+
3+
/*
4+
* Copyright 2001-2006 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
/**
23+
* Parse files from checksum file formats.
24+
*
25+
* @author <a href="mailto:[email protected]">Brett Porter</a>
26+
*/
27+
public class DigestUtils
28+
{
29+
private DigestUtils()
30+
{
31+
// don't create this class
32+
}
33+
34+
public static String cleanChecksum( String checksum, String algorithm, String path )
35+
throws DigesterException
36+
{
37+
String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
38+
39+
// Free-BSD / openssl
40+
String regex = algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-fA-F0-9]+)";
41+
Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
42+
if ( m.matches() )
43+
{
44+
String filename = m.group( 1 );
45+
if ( !isValidChecksumPattern( filename, path ) )
46+
{
47+
throw new DigesterException( "Supplied checksum does not match checksum pattern" );
48+
}
49+
trimmedChecksum = m.group( 2 );
50+
}
51+
else
52+
{
53+
// GNU tools
54+
m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
55+
if ( m.matches() )
56+
{
57+
String filename = m.group( 2 );
58+
if ( !isValidChecksumPattern( filename, path ) )
59+
{
60+
throw new DigesterException( "Supplied checksum does not match checksum pattern" );
61+
}
62+
trimmedChecksum = m.group( 1 );
63+
}
64+
}
65+
return trimmedChecksum;
66+
}
67+
68+
private static boolean isValidChecksumPattern( String filename, String path )
69+
{
70+
return filename.endsWith( path ) || ( "-".equals( filename ) );
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.codehaus.plexus.digest;
2+
3+
/*
4+
* Copyright 2001-2006 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.io.File;
20+
21+
/**
22+
* Create a digest for a file.
23+
*
24+
* @author <a href="mailto:[email protected]">Brett Porter</a>
25+
*/
26+
public interface Digester
27+
{
28+
String ROLE = Digester.class.getName();
29+
30+
/**
31+
* Get the algorithm used for the checksum.
32+
*
33+
* @return the algorithm
34+
*/
35+
String getAlgorithm();
36+
37+
/**
38+
* Calculate a checksum for a file.
39+
*
40+
* @param file the file to calculate the checksum for
41+
* @return the current checksum.
42+
* @throws DigesterException if there was a problem computing the hashcode.
43+
*/
44+
String calc( File file )
45+
throws DigesterException;
46+
47+
/**
48+
* Verify that a checksum is correct.
49+
*
50+
* @param file the file to compute the checksum for
51+
* @param checksum the checksum to compare to
52+
* @throws DigesterException if there was a problem computing the hashcode.
53+
*/
54+
void verify( File file, String checksum )
55+
throws DigesterException;
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.codehaus.plexus.digest;
2+
3+
/*
4+
* Copyright 2001-2006 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @author Edwin Punzalan
21+
*/
22+
public class DigesterException
23+
extends Exception
24+
{
25+
public DigesterException()
26+
{
27+
super();
28+
}
29+
30+
public DigesterException( String message )
31+
{
32+
super( message );
33+
}
34+
35+
public DigesterException( String message, Throwable cause )
36+
{
37+
super( message, cause );
38+
}
39+
40+
public DigesterException( Throwable cause )
41+
{
42+
super( cause );
43+
}
44+
}

0 commit comments

Comments
 (0)