From: Brett Porter Date: Sun, 1 Jan 2006 02:55:24 +0000 (+0000) Subject: add digester in a utility project X-Git-Tag: archiva-0.9-alpha-1~1018 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=02020af0a933e27dc6351e5a6a136a527c4be305;p=archiva.git add digester in a utility project PR: MRM-9 git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@360471 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-utils/pom.xml b/maven-repository-utils/pom.xml new file mode 100644 index 000000000..cd65d7719 --- /dev/null +++ b/maven-repository-utils/pom.xml @@ -0,0 +1,33 @@ + + + + + org.apache.maven.repository + maven-repository-manager + 1.0-SNAPSHOT + + 4.0.0 + maven-repository-utils + Maven Repository Utilities + + + org.codehaus.plexus + plexus-utils + + + diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java new file mode 100644 index 000000000..a8c1fb49b --- /dev/null +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java @@ -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 Brett Porter + * @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(); + } +} diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java new file mode 100644 index 000000000..ab097f39f --- /dev/null +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java @@ -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 Brett Porter + */ +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; +}