/[storm]/storm/org/nongnu/storm/BlockId.java
ViewVC logotype

Diff of /storm/org/nongnu/storm/BlockId.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by benja, Mon Apr 7 19:05:23 2003 UTC revision 1.7 by benja, Mon Apr 7 19:23:56 2003 UTC
# Line 83  public final class BlockId { Line 83  public final class BlockId {
83    
84      /** Check that the given data bytes match this id.      /** Check that the given data bytes match this id.
85       */       */
86      public void check(byte[] data) throws WrongIdException, Exception {      public void check(byte[] data) throws WrongIdException {
87          if(!equals(getIdForData(contentType, data)))          if(!equals(getIdForData(contentType, data)))
88              throw new WrongIdException("ID doesn't match");              throw new WrongIdException("ID doesn't match");
89      }      }
# Line 101  public final class BlockId { Line 101  public final class BlockId {
101       */       */
102      public InputStream getCheckedInputStream(InputStream in)      public InputStream getCheckedInputStream(InputStream in)
103                                                  throws IOException {                                                  throws IOException {
104          return null;          final MessageDigest dig_tt = makeTigerTreeDigest();
105            final MessageDigest dig_sha1 = makeSHA1Digest();
106    
107            in = new DigestInputStream(in, dig_tt);
108            in = new DigestInputStream(in, dig_sha1);
109    
110            return new FilterInputStream(in) {
111                    public void close() throws IOException {
112                        super.close();
113                        byte[] dig;
114    
115                        dig = dig_sha1.digest();
116                        for(int i=0; i<dig.length; i++)
117                            if(dig[i] != sha1[i])
118                                throw new WrongIdException("SHA-1 hash doesn't match");
119    
120                        dig = dig_tt.digest();
121                        for(int i=0; i<dig.length; i++)
122                            if(dig[i] != tigertree[i])
123                                throw new WrongIdException("TigerTree hash doesn't match");
124                    }
125                };
126      }      }
127    
128      public boolean equals(Object o) {      public boolean equals(Object o) {
# Line 115  public final class BlockId { Line 136  public final class BlockId {
136       *  The byte array must contain the bytes in a block.       *  The byte array must contain the bytes in a block.
137       */       */
138      public static BlockId getIdForData(String contentType,      public static BlockId getIdForData(String contentType,
139                                         byte[] bytes) throws Exception {                                         byte[] bytes) {
140          MessageDigest dig_tt = new TreeTiger();          MessageDigest dig_tt = makeTigerTreeDigest();
141          MessageDigest dig_sha1 = MessageDigest.getInstance("SHA");          MessageDigest dig_sha1 = makeSHA1Digest();
142    
143          dig_tt.update(bytes);          dig_tt.update(bytes);
144          dig_sha1.update(bytes);          dig_sha1.update(bytes);
# Line 125  public final class BlockId { Line 146  public final class BlockId {
146          return new BlockId(contentType, dig_sha1.digest(),          return new BlockId(contentType, dig_sha1.digest(),
147                             dig_tt.digest());                             dig_tt.digest());
148      }      }
149    
150    
151        /** Create a new SHA-1 message digest; throw an error
152         *  if this algorithm isn't available.
153         */
154        static MessageDigest makeSHA1Digest() {
155            try {
156                return MessageDigest.getInstance("SHA");
157            } catch(NoSuchAlgorithmException e) {
158                throw new Error("Fatal error: The SHA-1 algorithm "+
159                                "is not supported by this version "+
160                                "of the Java libraries. "+
161                                "Storm cannot operate without "+
162                                "an SHA-1 implementation.");
163            }
164        }
165    
166        /** Create a new TigerTree message digest; throw an error
167         *  if this algorithm isn't available.
168         */
169        static MessageDigest makeTigerTreeDigest() {
170            try {
171                return new TreeTiger();
172            } catch(NoSuchAlgorithmException e) {
173                throw new Error("Fatal error: There was a problem " +
174                                "initializing the TigerTree " +
175                                "message digest (maybe the Cryptix " +
176                                "JCE is missing). " +
177                                "Storm cannot operate without "+
178                                "a TigerTree implementation.");
179            }
180        }
181  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26