directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: rev 57126 - incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum
Date Wed, 10 Nov 2004 03:41:26 GMT
Author: erodriguez
Date: Tue Nov  9 19:41:26 2004
New Revision: 57126

Modified:
   incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/ChecksumEngine.java
   incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Crc32Checksum.java
   incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd4Checksum.java
   incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd5Checksum.java
   incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Sha1Checksum.java
Log:
Refactoring.  Added a template method for digest selection which allowed the consolidation
of all checksum calculation to the ChecksumEngine base class.

Modified: incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/ChecksumEngine.java
==============================================================================
--- incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/ChecksumEngine.java
(original)
+++ incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/ChecksumEngine.java
Tue Nov  9 19:41:26 2004
@@ -16,19 +16,30 @@
  */
 package org.apache.kerberos.crypto.checksum;
 
-import org.apache.kerberos.crypto.encryption.*;
+import org.apache.kerberos.crypto.encryption.CipherType;
+import org.bouncycastle.crypto.Digest;
 
-public interface ChecksumEngine {
-	
-	public ChecksumType checksumType();
-	public CipherType keyType();
-	public int checksumSize();
-	public int keySize();
-	public int confounderSize();
-	public boolean isSafe();
-	public byte[] calculateChecksum(byte[] data);
-	public byte[] calculateKeyedChecksum(byte[] data, byte[] key);
-	public boolean verifyKeyedChecksum(byte[] data, byte[] key, byte[] checksum);
+public abstract class ChecksumEngine
+{
+    public abstract Digest getDigest();
+	public abstract ChecksumType checksumType();
+	public abstract CipherType keyType();
+	public abstract int checksumSize();
+	public abstract int keySize();
+	public abstract int confounderSize();
+	public abstract boolean isSafe();
+	public abstract byte[] calculateKeyedChecksum( byte[] data, byte[] key );
+	public abstract boolean verifyKeyedChecksum( byte[] data, byte[] key, byte[] checksum );
 
+    public byte[] calculateChecksum( byte[] data )
+    {
+        Digest digester = getDigest();
+
+		digester.reset();
+		digester.update( data, 0, data.length );
+		byte[] returnValue = new byte[ digester.getDigestSize() ];
+		digester.doFinal( returnValue, 0 );
+		return returnValue;
+	}
 }
 

Modified: incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Crc32Checksum.java
==============================================================================
--- incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Crc32Checksum.java
(original)
+++ incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Crc32Checksum.java
Tue Nov  9 19:41:26 2004
@@ -17,12 +17,16 @@
 package org.apache.kerberos.crypto.checksum;
 
 import org.apache.kerberos.crypto.encryption.CipherType;
+import org.bouncycastle.crypto.Digest;
 
 import java.util.zip.CRC32;
 
-public class Crc32Checksum implements ChecksumEngine
+public class Crc32Checksum extends ChecksumEngine
 {
-	private final CRC32 crc32 = new CRC32();
+    public Digest getDigest()
+    {
+        return new CRC32Digest();
+    }
 
 	public ChecksumType checksumType()
     {
@@ -54,13 +58,6 @@
 		return false;
 	}
 
-	public synchronized byte[] calculateChecksum( byte[] data )
-    {
-		crc32.reset();
-		crc32.update( data );
-		return int2octet( (int)crc32.getValue() );
-	}
-
 	public byte[] calculateKeyedChecksum( byte[] data, byte[] key )
     {
 		return null;
@@ -71,14 +68,51 @@
 		return false;
 	}
 
-    private byte[] int2octet( int value )
+    private class CRC32Digest implements Digest
     {
-		byte[] bytes = new byte[4];
-		int i, shift;
+        private CRC32 crc32 = new CRC32();
 
-		for (i = 0, shift = 24; i < 4; i++, shift -= 8)
-			bytes[i] = (byte) (0xFF & (value >> shift));
-		return bytes;
-	}
+        public String getAlgorithmName()
+        {
+            return "CRC-32";
+        }
+
+        public int getDigestSize()
+        {
+            return 4;
+        }
+
+        public void reset()
+        {
+            crc32.reset();
+        }
+
+        public void update( byte in )
+        {
+            crc32.update( in );
+        }
+
+        public void update( byte[] in, int inOff, int len )
+        {
+            crc32.update( in, inOff, len );
+        }
+
+        public int doFinal(byte[] out, int outOff)
+        {
+            out = int2octet( (int)crc32.getValue() );
+
+            return 0;
+        }
+
+        private byte[] int2octet( int value )
+        {
+            byte[] bytes = new byte[4];
+            int i, shift;
+
+            for (i = 0, shift = 24; i < 4; i++, shift -= 8)
+                bytes[i] = (byte) (0xFF & (value >> shift));
+            return bytes;
+        }
+    }
 }
 

Modified: incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd4Checksum.java
==============================================================================
--- incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd4Checksum.java
(original)
+++ incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd4Checksum.java
Tue Nov  9 19:41:26 2004
@@ -16,51 +16,54 @@
  */
 package org.apache.kerberos.crypto.checksum;
 
-import org.apache.kerberos.crypto.encryption.*;
-import org.bouncycastle.crypto.*;
-import org.bouncycastle.crypto.digests.*;
+import org.apache.kerberos.crypto.encryption.CipherType;
+import org.bouncycastle.crypto.Digest;
+import org.bouncycastle.crypto.digests.MD4Digest;
+
+public class RsaMd4Checksum extends ChecksumEngine
+{
+    public Digest getDigest()
+    {
+        return new MD4Digest();
+    }
 
-public class RsaMd4Checksum implements ChecksumEngine {
-
-	private static final Digest digester = new MD4Digest();
-
-	public ChecksumType checksumType() {
+	public ChecksumType checksumType()
+    {
 		return ChecksumType.RSA_MD4;
 	}
 
-	public CipherType keyType() {
+	public CipherType keyType()
+    {
 		return CipherType.NULL;
 	}
 	
-	public int checksumSize() {
+	public int checksumSize()
+    {
 		return 16;
 	}
 
-	public int keySize() {
+	public int keySize()
+    {
 		return 0;
 	}
 
-	public int confounderSize() {
+	public int confounderSize()
+    {
 		return 0;
 	}
 
-	public boolean isSafe() {
+	public boolean isSafe()
+    {
 		return false;
 	}
 
-	public synchronized byte[] calculateChecksum(byte[] data) {
-		digester.reset();
-		digester.update(data, 0, data.length);
-		byte[] returnValue = new byte[digester.getDigestSize()];
-		digester.doFinal(returnValue, 0);
-		return returnValue;
-	}
-
-	public byte[] calculateKeyedChecksum(byte[] data, byte[] key) {
+	public byte[] calculateKeyedChecksum(byte[] data, byte[] key)
+    {
 		return null;
 	}
 
-	public boolean verifyKeyedChecksum(byte[] data, byte[] key, byte[] checksum) {
+	public boolean verifyKeyedChecksum(byte[] data, byte[] key, byte[] checksum)
+    {
 		return false;
 	}
 }

Modified: incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd5Checksum.java
==============================================================================
--- incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd5Checksum.java
(original)
+++ incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/RsaMd5Checksum.java
Tue Nov  9 19:41:26 2004
@@ -16,51 +16,54 @@
  */
 package org.apache.kerberos.crypto.checksum;
 
-import org.apache.kerberos.crypto.encryption.*;
-import org.bouncycastle.crypto.*;
-import org.bouncycastle.crypto.digests.*;
+import org.apache.kerberos.crypto.encryption.CipherType;
+import org.bouncycastle.crypto.Digest;
+import org.bouncycastle.crypto.digests.MD5Digest;
+
+public class RsaMd5Checksum extends ChecksumEngine
+{
+    public Digest getDigest()
+    {
+        return new MD5Digest();
+    }
 
-public class RsaMd5Checksum implements ChecksumEngine {
-
-	private static final Digest digester = new MD5Digest();
-
-	public ChecksumType checksumType() {
+	public ChecksumType checksumType()
+    {
 		return ChecksumType.RSA_MD5;
 	}
 
-	public CipherType keyType() {
+	public CipherType keyType()
+    {
 		return CipherType.NULL;
 	}
 
-	public int checksumSize() {
+	public int checksumSize()
+    {
 		return 16;
 	}
 
-	public int keySize() {
+	public int keySize()
+    {
 		return 0;
 	}
 
-	public int confounderSize() {
+	public int confounderSize()
+    {
 		return 0;
 	}
 
-	public boolean isSafe() {
+	public boolean isSafe()
+    {
 		return false;
 	}
 
-	public synchronized byte[] calculateChecksum(byte[] data) {
-		digester.reset();
-		digester.update(data, 0, data.length);
-		byte[] returnValue = new byte[digester.getDigestSize()];
-		digester.doFinal(returnValue, 0);
-		return returnValue;
-	}
-
-	public byte[] calculateKeyedChecksum(byte[] data, byte[] key) {
+	public byte[] calculateKeyedChecksum( byte[] data, byte[] key )
+    {
 		return null;
 	}
 
-	public boolean verifyKeyedChecksum(byte[] data, byte[] key, byte[] checksum) {
+	public boolean verifyKeyedChecksum( byte[] data, byte[] key, byte[] checksum )
+    {
 		return false;
 	}
 }

Modified: incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Sha1Checksum.java
==============================================================================
--- incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Sha1Checksum.java
(original)
+++ incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/checksum/Sha1Checksum.java
Tue Nov  9 19:41:26 2004
@@ -16,51 +16,54 @@
  */
 package org.apache.kerberos.crypto.checksum;
 
-import org.apache.kerberos.crypto.encryption.*;
-import org.bouncycastle.crypto.*;
-import org.bouncycastle.crypto.digests.*;
+import org.apache.kerberos.crypto.encryption.CipherType;
+import org.bouncycastle.crypto.Digest;
+import org.bouncycastle.crypto.digests.SHA1Digest;
+
+public class Sha1Checksum extends ChecksumEngine
+{
+    public Digest getDigest()
+    {
+        return new SHA1Digest();
+    }
 
-public class Sha1Checksum implements ChecksumEngine {
-
-	private static final Digest digester = new SHA1Digest();
-
-	public ChecksumType checksumType() {
+	public ChecksumType checksumType()
+    {
 		return ChecksumType.SHA1;
 	}
 
-	public CipherType keyType() {
+	public CipherType keyType()
+    {
 		return CipherType.NULL;
 	}
 	
-	public int checksumSize() {
+	public int checksumSize()
+    {
 		return 20;
 	}
 
-	public int keySize() {
+	public int keySize()
+    {
 		return 0;
 	}
 
-	public int confounderSize() {
+	public int confounderSize()
+    {
 		return 0;
 	}
 
-	public boolean isSafe() {
+	public boolean isSafe()
+    {
 		return false;
 	}
 
-	public synchronized byte[] calculateChecksum(byte[] data) {
-		digester.reset();
-		digester.update(data, 0, data.length);
-		byte[] returnValue = new byte[digester.getDigestSize()];
-		digester.doFinal(returnValue, 0);
-		return returnValue;
-	}
-
-	public byte[] calculateKeyedChecksum(byte[] data, byte[] key) {
+	public byte[] calculateKeyedChecksum( byte[] data, byte[] key )
+    {
 		return null;
 	}
 
-	public boolean verifyKeyedChecksum(byte[] data, byte[] key, byte[] checksum) {
+	public boolean verifyKeyedChecksum( byte[] data, byte[] key, byte[] checksum )
+    {
 		return false;
 	}
 }

Mime
View raw message