You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@depot.apache.org by mm...@apache.org on 2004/02/20 22:59:31 UTC
svn commit: rev 6804 - incubator/depot/trunk/ruper/src/java/core/org/apache/ruper/util/security
Author: mmay
Date: Fri Feb 20 14:59:30 2004
New Revision: 6804
Added:
incubator/depot/trunk/ruper/src/java/core/org/apache/ruper/util/security/ChkSum.java
Log:
Added to allow easy ChkSum generation and validation
Added: incubator/depot/trunk/ruper/src/java/core/org/apache/ruper/util/security/ChkSum.java
==============================================================================
--- (empty file)
+++ incubator/depot/trunk/ruper/src/java/core/org/apache/ruper/util/security/ChkSum.java Fri Feb 20 14:59:30 2004
@@ -0,0 +1,267 @@
+/*
+ * Copyright 2004 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.
+ */
+package org.apache.ruper.util.security;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+
+import org.apache.ruper.RuperException;
+
+import org.apache.version.log.Logger;
+
+
+/**
+ * TODO mm Create Comment
+ *
+ * @author mmay
+ */
+public class ChkSum {
+ private String digestAlgorithm = null;
+ private String digestProvider = null;
+ private MessageDigest messageDigest = null;
+ private File hashCodeFile = null;
+ private File sourceFile = null;
+ private String genHashCode = null;
+ private String readHashCode = null;
+ private int buffersize = HashCodeManager.DEFAULT_BUFFERSIZE;
+
+ /**
+ * Constructs a new ChkSum Object
+ * Note, that the provider can be null
+ */
+ public ChkSum(String algorithm, String provider) {
+ this.digestAlgorithm = algorithm;
+ this.digestProvider = provider;
+ }
+
+ /**
+ * Creates a new ChkSum object.
+ * Convenience Constructor
+ *
+ * @param algorithm maybe MD5 or SHA or ???
+ */
+ public ChkSum(String algorithm) {
+ this(algorithm, null);
+ }
+
+ /**
+ * Creates a new ChkSum object with the Default Algorithm
+ * Convenience Constructor
+ *
+ */
+ public ChkSum() {
+ this(HashCodeManager.DEFAULT_ALGORITHM, null);
+ }
+
+ /**
+ * TODO Add JavaDoc
+ *
+ * @return TODO
+ *
+ * @throws NoSuchAlgorithmException TODO
+ * @throws NoSuchProviderException TODO
+ */
+ public MessageDigest getDigest() throws NoSuchAlgorithmException, NoSuchProviderException {
+ if (this.messageDigest == null) {
+ this.messageDigest = HashCodeManager.createDigester(this.digestAlgorithm, this.digestProvider);
+ }
+
+ return this.messageDigest;
+ }
+
+ /**
+ * TODO Add JavaDoc
+ *
+ * @return TODO
+ */
+ private boolean validate() throws RuperException {
+ boolean returnValue = false;
+
+ Logger.getLogger().debug("Validating HashCode");
+
+ if ((this.sourceFile == null) || !this.sourceFile.exists()) {
+ throw new RuperException("Cannot find " + sourceFile);
+ }
+
+ if ((this.hashCodeFile == null) || !this.hashCodeFile.exists()) {
+ throw new RuperException("Cannot find " + hashCodeFile);
+ }
+
+ try {
+ byte[] checksum = HashCodeManager.getDigest(this.sourceFile, this.messageDigest, this.buffersize);
+ byte[] readChecksum = HashCodeManager.readHashFromFile(this.hashCodeFile);
+
+ returnValue = HashCodeManager.compareHashes(checksum, readChecksum);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return returnValue;
+ }
+
+ /**
+ *
+ */
+ public void writeHashCodeToFile(File destination, byte[] digest) {
+ Logger.getLogger().debug("Opening destination File");
+
+ try {
+ HashCodeManager.writeHashToFile(destination, digest);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Generates a Checksum from a File
+ *
+ * @param source
+ *
+ * @return
+ */
+ private byte[] generateChecksum(File source) throws Exception {
+ return HashCodeManager.getDigest(source, this.messageDigest, this.buffersize);
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public String getDigestAlgorithm() {
+ return digestAlgorithm;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pString
+ */
+ public void setDigestAlgorithm(String pString) {
+ digestAlgorithm = pString;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public String getDigestProvider() {
+ return digestProvider;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pString
+ */
+ public void setDigestProvider(String pString) {
+ digestProvider = pString;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public String getGenHashCode() {
+ return genHashCode;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pString
+ */
+ public void setGenHashCode(String pString) {
+ genHashCode = pString;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public File getHashCodeFile() {
+ return hashCodeFile;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pFile
+ */
+ public void setHashCodeFile(File pFile) {
+ hashCodeFile = pFile;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public MessageDigest getMessageDigest() {
+ return messageDigest;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pDigest
+ */
+ public void setMessageDigest(MessageDigest pDigest) {
+ messageDigest = pDigest;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public String getReadHashCode() {
+ return readHashCode;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pString
+ */
+ public void setReadHashCode(String pString) {
+ readHashCode = pString;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @return
+ */
+ public File getSourceFile() {
+ return sourceFile;
+ }
+
+ /**
+ * TODO mm Create Comment
+ *
+ * @param pFile
+ */
+ public void setSourceFile(File pFile) {
+ sourceFile = pFile;
+ }
+}