[Webfunds-commits] java/webfunds/sox Id.java

Ian Grigg iang@cypherpunks.ai
Tue, 27 Feb 2001 10:06:24 -0400 (AST)


iang        01/02/27 10:06:24

  Modified:    webfunds/sox Id.java
  Log:
  Added OpenPGP leading byte support code, which is used by the
  receipt and maybe by other code (especially, SOX1).

Revision  Changes    Path
1.9       +59 -5     java/webfunds/sox/Id.java

Index: Id.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/Id.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Id.java	2001/02/27 01:28:28	1.8
+++ Id.java	2001/02/27 14:06:23	1.9
@@ -1,5 +1,5 @@
 /*
- * $Id: Id.java,v 1.8 2001/02/27 01:28:28 iang Exp $
+ * $Id: Id.java,v 1.9 2001/02/27 14:06:23 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -14,7 +14,7 @@
  * an object by means of a hash or similar.
  * This is a pseudo abstract class for other identifier classes.
  *
- * @version $Revision: 1.8 $
+ * @version $Revision: 1.9 $
  */
 public /*abstract*/ class Id extends Encodable
     // should be abstract but this makes the test harder
@@ -34,7 +34,6 @@
         return id;
     }
 
-
     /**
      * Set the underlying byte[].
      * (This call was setId)
@@ -47,6 +46,60 @@
 
 
 
+    /**
+     *  SOX2 format for these hashes is the OpenPGP format with leading byte.
+     *  Normally, hashes are stored just as a byte array.  For some parts
+     *  (notably, the receipt) they are stored as OpenPGP format, with
+     *  these leading bytes prepended.
+     *
+     *  For completeness, the OpenPGP spec also defines these:
+     *          3      "RIP",
+     *          4      "2SHA",
+     *          5      "MD2",
+     *          6      "TIG",
+     *          7      "HAV",
+     *  Ref:  OpenPGP Message Formats RFC 2440 Section 9.4
+     *
+     *  These should probably be in a different file, but are only
+     *  used by Ids, so here for now.
+     */  
+    protected static final int MD_MD5  = 1,   // was used for SOX1
+                               MD_SHA1 = 2,   // standard a/c for SOX2
+                               MD_NAH  = 110; // Not-A-Hash internal float a/c
+
+    protected static final int LEN_MD5  = 16,
+                               LEN_SHA1 = 20,
+                               LEN_NAH  = 18; // deliberately not any other
+
+    private int openPGPId      = -1;
+    public void setOpenPGPId(int i)    { openPGPId = i; }
+    
+    /**
+     *  Assumes length of the hash array will determine.
+     *  @return the OpenPGP hash id for this hash, else 0 for bearer,
+     *  else -1 for unknown hash array?
+     */
+    public int getOpenPGPId()
+    {
+        if (openPGPId >= 0)        // user of this should save it
+           return openPGPId;
+
+        if (this instanceof AccountId && ((AccountId)this).isBearer())
+            return MD_SHA1;        // how they are done at the moment
+
+        if (id == null || id.length == 0)
+            return 0;
+        if (id.length == LEN_SHA1)
+            return MD_SHA1;
+        if (id.length == LEN_MD5)
+            return MD_MD5;
+        if (id.length == LEN_NAH)
+            return MD_NAH;
+
+        return -1;
+    }
+
+
 ////// Construction ///////////////////////////////////
 
     protected Id()
@@ -164,11 +217,12 @@
     }
 
     /**
-     * Make and return an example Id for testing (other classes).
+     *  Make and return an example Id for testing (other classes).
+     *  This hash can be standard length, or empty.
      */
     protected static Id ex()
     {
-        return ex(20) ;                // SHA1 sized
+        return ex(LEN_SHA1) ;                // SHA1 sized
     }
 
     protected static Id ex(int len)