PR: MRM-9 git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@360471 13f79535-47bb-0310-9956-ffa450edef68tags/archiva-0.9-alpha-1
@@ -0,0 +1,33 @@ | |||
<!-- | |||
~ Copyright 2005-2006 The Apache Software Foundation. | |||
~ | |||
~ 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"> | |||
<parent> | |||
<groupId>org.apache.maven.repository</groupId> | |||
<artifactId>maven-repository-manager</artifactId> | |||
<version>1.0-SNAPSHOT</version> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>maven-repository-utils</artifactId> | |||
<name>Maven Repository Utilities</name> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.codehaus.plexus</groupId> | |||
<artifactId>plexus-utils</artifactId> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -0,0 +1,107 @@ | |||
package org.apache.maven.repository.digest; | |||
import org.codehaus.plexus.util.IOUtil; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.security.MessageDigest; | |||
import java.security.NoSuchAlgorithmException; | |||
/* | |||
* Copyright 2005-2006 The Apache Software Foundation. | |||
* | |||
* 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. | |||
*/ | |||
/** | |||
* Create a digest for a file. | |||
* | |||
* @author <a href="mailto:brett@apache.org">Brett Porter</a> | |||
* @plexus.component role="org.apache.maven.repository.digest.Digester" | |||
*/ | |||
public class DefaultDigester | |||
implements Digester | |||
{ | |||
private static final int CHECKSUM_BUFFER_SIZE = 16384; | |||
private static final int BYTE_MASK = 0xFF; | |||
public String createChecksum( File file, String algorithm ) | |||
throws IOException, NoSuchAlgorithmException | |||
{ | |||
MessageDigest digest = MessageDigest.getInstance( algorithm ); | |||
InputStream fis = new FileInputStream( file ); | |||
try | |||
{ | |||
byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE]; | |||
int numRead; | |||
do | |||
{ | |||
numRead = fis.read( buffer ); | |||
if ( numRead > 0 ) | |||
{ | |||
digest.update( buffer, 0, numRead ); | |||
} | |||
} | |||
while ( numRead != -1 ); | |||
} | |||
finally | |||
{ | |||
IOUtil.close( fis ); | |||
} | |||
return byteArrayToHexStr( digest.digest() ); | |||
} | |||
public boolean verifyChecksum( File file, String checksum, String algorithm ) | |||
throws NoSuchAlgorithmException, IOException | |||
{ | |||
//Create checksum for jar file | |||
String sum = createChecksum( file, algorithm ); | |||
return checksum.toUpperCase().equals( sum.toUpperCase() ); | |||
} | |||
/** | |||
* Convert an incoming array of bytes into a string that represents each of | |||
* the bytes as two hex characters. | |||
* | |||
* @param data | |||
*/ | |||
private static String byteArrayToHexStr( byte[] data ) | |||
{ | |||
String output = ""; | |||
for ( int cnt = 0; cnt < data.length; cnt++ ) | |||
{ | |||
//Deposit a byte into the 8 lsb of an int. | |||
int tempInt = data[cnt] & BYTE_MASK; | |||
//Get hex representation of the int as a string. | |||
String tempStr = Integer.toHexString( tempInt ); | |||
//Append a leading 0 if necessary so that each hex string will contain 2 characters. | |||
if ( tempStr.length() == 1 ) | |||
{ | |||
tempStr = "0" + tempStr; | |||
} | |||
//Concatenate the two characters to the output string. | |||
output = output + tempStr; | |||
} | |||
return output.toUpperCase(); | |||
} | |||
} |
@@ -0,0 +1,42 @@ | |||
package org.apache.maven.repository.digest; | |||
/* | |||
* Copyright 2005-2006 The Apache Software Foundation. | |||
* | |||
* 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. | |||
*/ | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.FileNotFoundException; | |||
import java.security.NoSuchAlgorithmException; | |||
/** | |||
* Create a digest for a file. | |||
* | |||
* @author <a href="mailto:brett@apache.org">Brett Porter</a> | |||
*/ | |||
public interface Digester | |||
{ | |||
String ROLE = Digester.class.getName(); | |||
String SHA1 = "SHA-1"; | |||
String MD5 = "MD5"; | |||
String createChecksum( File file, String algorithm ) | |||
throws FileNotFoundException, IOException, NoSuchAlgorithmException; | |||
boolean verifyChecksum( File file, String checksum, String algorithm ) | |||
throws NoSuchAlgorithmException, IOException; | |||
} |