gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] storm/org/nongnu/storm BlockId.java BlockId.test


From: Benja Fallenstein
Subject: [Gzz-commits] storm/org/nongnu/storm BlockId.java BlockId.test
Date: Mon, 07 Apr 2003 15:23:56 -0400

CVSROOT:        /cvsroot/storm
Module name:    storm
Changes by:     Benja Fallenstein <address@hidden>      03/04/07 15:23:56

Modified files:
        org/nongnu/storm: BlockId.java BlockId.test 

Log message:
        Ok, BlockId works

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/BlockId.java.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/BlockId.test.diff?tr1=1.3&tr2=1.4&r1=text&r2=text

Patches:
Index: storm/org/nongnu/storm/BlockId.java
diff -u storm/org/nongnu/storm/BlockId.java:1.6 
storm/org/nongnu/storm/BlockId.java:1.7
--- storm/org/nongnu/storm/BlockId.java:1.6     Mon Apr  7 15:05:23 2003
+++ storm/org/nongnu/storm/BlockId.java Mon Apr  7 15:23:56 2003
@@ -83,7 +83,7 @@
 
     /** Check that the given data bytes match this id.
      */
-    public void check(byte[] data) throws WrongIdException, Exception {
+    public void check(byte[] data) throws WrongIdException {
        if(!equals(getIdForData(contentType, data)))
            throw new WrongIdException("ID doesn't match");
     }
@@ -101,7 +101,28 @@
      */
     public InputStream getCheckedInputStream(InputStream in)
                                                 throws IOException {
-       return null;
+        final MessageDigest dig_tt = makeTigerTreeDigest();
+        final MessageDigest dig_sha1 = makeSHA1Digest();
+
+       in = new DigestInputStream(in, dig_tt);
+       in = new DigestInputStream(in, dig_sha1);
+
+       return new FilterInputStream(in) {
+               public void close() throws IOException {
+                   super.close();
+                   byte[] dig; 
+
+                   dig = dig_sha1.digest();
+                   for(int i=0; i<dig.length; i++)
+                       if(dig[i] != sha1[i])
+                           throw new WrongIdException("SHA-1 hash doesn't 
match");
+
+                   dig = dig_tt.digest();
+                   for(int i=0; i<dig.length; i++)
+                       if(dig[i] != tigertree[i])
+                           throw new WrongIdException("TigerTree hash doesn't 
match");
+               }
+           };
     }
 
     public boolean equals(Object o) {
@@ -115,14 +136,46 @@
      *  The byte array must contain the bytes in a block.
      */
     public static BlockId getIdForData(String contentType, 
-                                      byte[] bytes) throws Exception {
-        MessageDigest dig_tt = new TreeTiger();
-        MessageDigest dig_sha1 = MessageDigest.getInstance("SHA");
+                                      byte[] bytes) {
+        MessageDigest dig_tt = makeTigerTreeDigest();
+        MessageDigest dig_sha1 = makeSHA1Digest();
 
        dig_tt.update(bytes);
        dig_sha1.update(bytes);
 
        return new BlockId(contentType, dig_sha1.digest(),
                           dig_tt.digest());
+    }
+
+
+    /** Create a new SHA-1 message digest; throw an error
+     *  if this algorithm isn't available. 
+     */
+    static MessageDigest makeSHA1Digest() {
+       try {
+           return MessageDigest.getInstance("SHA");
+       } catch(NoSuchAlgorithmException e) {
+           throw new Error("Fatal error: The SHA-1 algorithm "+
+                           "is not supported by this version "+
+                           "of the Java libraries. "+
+                           "Storm cannot operate without "+
+                           "an SHA-1 implementation.");
+       }
+    }
+
+    /** Create a new TigerTree message digest; throw an error
+     *  if this algorithm isn't available. 
+     */
+    static MessageDigest makeTigerTreeDigest() {
+       try {
+           return new TreeTiger();
+       } catch(NoSuchAlgorithmException e) {
+           throw new Error("Fatal error: There was a problem " +
+                           "initializing the TigerTree " +
+                           "message digest (maybe the Cryptix " +
+                           "JCE is missing). " +
+                           "Storm cannot operate without "+
+                           "a TigerTree implementation.");
+       }
     }
 }
Index: storm/org/nongnu/storm/BlockId.test
diff -u storm/org/nongnu/storm/BlockId.test:1.3 
storm/org/nongnu/storm/BlockId.test:1.4
--- storm/org/nongnu/storm/BlockId.test:1.3     Mon Apr  7 15:05:23 2003
+++ storm/org/nongnu/storm/BlockId.test Mon Apr  7 15:23:56 2003
@@ -20,6 +20,7 @@
 # 
 # 
 
+import java
 from org.nongnu.storm import BlockId
 from jarray import array
 
@@ -72,6 +73,16 @@
     except BlockId.WrongIdException: return 0
     else: return 1
 
+def check2(id, data):
+    s = java.io.ByteArrayInputStream(data)
+    s2 = id.getCheckedInputStream(s)
+    while s2.read() >= 0: pass
+
+    try: s2.close()
+    except BlockId.WrongIdException: return 0
+    else: return 1
+
+
 def testCheck():
     id_1 = BlockId(uri_1)
     id_2 = BlockId(uri_2)
@@ -82,3 +93,9 @@
     
     assert check(id_3, data_3)
     assert (not check(id_1, data_3)) and (not check(id_2, data_3))
+
+    assert check2(id_2, data_2)
+    assert (not check2(id_1, data_2)) and (not check2(id_3, data_2))
+    
+    assert check2(id_3, data_3)
+    assert (not check2(id_1, data_3)) and (not check2(id_2, data_3))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]