[Webfunds-commits] java/webfunds/util Hex.java Support.java Armoury.java

Ian Grigg iang@cypherpunks.ai
Sun, 1 Apr 2001 22:22:44 -0400 (AST)


iang        01/04/01 22:22:44

  Modified:    webfunds/util Armoury.java
  Added:       webfunds/util Hex.java Support.java
  Log:
  more stuff migrated to webfunds.util standard package name

Revision  Changes    Path
1.33      +4 -4      java/webfunds/util/Armoury.java

Index: Armoury.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/util/Armoury.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -r1.32 -r1.33
--- Armoury.java	2001/03/18 23:20:57	1.32
+++ Armoury.java	2001/04/02 02:22:43	1.33
@@ -1,4 +1,4 @@
-/* $Id: Armoury.java,v 1.32 2001/03/18 23:20:57 iang Exp $
+/* $Id: Armoury.java,v 1.33 2001/04/02 02:22:43 iang Exp $
  *
  * Copyright (c) 1995-2001 Systemics Inc. on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -45,7 +45,7 @@
  * <p>
  * See Base64Coder for details on how Base64 works.
  *
- * @version $Revision: 1.32 $
+ * @version $Revision: 1.33 $
  */
 public abstract class Armoury
 {
@@ -54,7 +54,7 @@
      *  (not to be used in calculation of sigs, etc).
      *  Note that if the armoured payment is so to be pasted into a
      *  cut&paste buffer, Java has a bug in it that assumes Unix
-     *  newlines, so use the webfunds.utils.ClipboardHelper to
+     *  newlines, so use the webfunds.util(s).ClipboardHelper to
      *  filter the payment into the correct format.
      */
     static final String newline = System.getProperty("line.separator");
@@ -701,7 +701,7 @@
     {
         byte[] prepared = prepareDataToSign(buf);
         byte[] ddd = Crypto.digest(prepared);
-        System.err.println("digest  : " + webfunds.utils.Hex.data2hex(ddd));
+        System.err.println("digest  : " + webfunds.util.Hex.data2hex(ddd));
     }
 
     public static void main(String[] args)



1.1                  java/webfunds/util/Hex.java

Index: Hex.java
===================================================================
/* $Id: Hex.java,v 1.1 2001/04/02 02:22:43 iang Exp $
 *
 * Copyright 1995-1999 by Systemics, Ltd on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */

package webfunds.util;

/**
 *  Common Hex and digest routines.
 *
 *  Was webfunds.utils;
 */
public class Hex
{
    private Hex() {}

    /**
     * isHexAscii checks whether a String is made of Hex digits.
     */
    public static boolean isHexAscii(String s)
    {
        if (null == s)   // not logically correct, but is the intention
            return false ;

        if (s.length() == 0)
            return false ;

        /*
         *  For Hex, both upper and lower are ok.
         */
        for (int i = 0; i < s.length(); i++)
        {
            if (toDataNibble(s.charAt(i)) < 0)
                return false;
        }
        return true ;
    }

    public static String data2hex(byte[] data)
    {
        if (data == null)
            return NULL;

        int len = data.length;
        StringBuffer buf = new StringBuffer(len*2);
        for (int pos = 0; pos < len; pos++)
            buf.append(toHexChar((data[pos]>>>4)&0x0F)).append(toHexChar(data[pos]&0x0F));
        return buf.toString();
    }

    public static byte[] hex2data(String str)
    {
        if (str == null)
            return new byte[0] ;

        int len = str.length();    // probably should check length
        char hex[] = str.toCharArray();
        byte[] buf = new byte[len/2];

        for (int pos = 0; pos < len / 2; pos++)
            buf[pos] = (byte)( ((toDataNibble(hex[2*pos]) << 4) & 0xF0)
                            | ( toDataNibble(hex[2*pos + 1])   & 0x0F) );

        return buf;
    }

    public static char toHexChar(int i)
    {
        if ((0 <= i) && (i <= 9 ))
            return (char)('0' + i);
        else
            return (char)('a' + (i-10));
    }

    public static byte toDataNibble(char c)
    {
        if (('0' <= c) && (c <= '9' ))
            return (byte)((byte)c - (byte)'0');
        else if (('a' <= c) && (c <= 'f' ))
            return (byte)((byte)c - (byte)'a' + 10);
        else if (('A' <= c) && (c <= 'F' ))
            return (byte)((byte)c - (byte)'A' + 10);
        else
            return -1;
    }

    static final String NULL = "<null>";

    /**
     * @return a byte array converted to hex, and truncated beyond some
     *         reasonable length, long enough to avoid debugging collisions.
     */
    public static String quick(byte[] b)
    {
        if (b == null)
            return NULL;
        if (b.length < 8)
            return data2hex(b);

        byte[] bbb = new byte[8];
        for (int j = 0; j < bbb.length; j++)
            bbb[j] = b[j];

        return data2hex(bbb) + "...";
    }



    /**
     *  Useful for printing descriptions, which can be binary.
     *  @return a string that is printable, either the original, or a hex
     *          conversion of the original if funny chars found.
     */
    public static String printable(byte[] data)
    {
        if (data == null)
            return "<undef>";

        int len = data.length;
        for (int pos = 0; pos < len; pos++)
        {
            if (0x20 <= data[pos] && data[pos] <= 0xEF)
                continue ;
            if (data[pos] == '\r' || data[pos] == '\n' || data[pos] == '\t')
                continue ;
            return data2hex(data);
        }
        return new String(data);
    }





        ////////////////////////////////
       //                            //
      //     T E S T    C O D E     //
     //                            //
    ////////////////////////////////



    public static void main(String args[])
        throws Exception
    {
        args = null;
        String s = "c001d00d";
        byte b[] = { (byte)0xC0, (byte)0x01, (byte)0xD0, (byte)0x0D };

        String test = data2hex(b).toLowerCase();
        if (!test.equals(s))
            throw new Exception("data2hex " + s + " failed: " + test);

        byte ttt[] = hex2data(s);
        // ttt[2] = (byte)0xFA;  // test
        for (int i = 0; i < b.length; i++)
        {
            if (ttt[i] != b[i])
                throw new Exception("hex2data " + s + " failed: "
                                + data2hex(ttt) + " (byte " + i + ")");
        }


        System.err.println("Tests Passed.");
        System.exit(0);
    }


}




1.1                  java/webfunds/util/Support.java

Index: Support.java
===================================================================
/*
 * $Id: Support.java,v 1.1 2001/04/02 02:22:43 iang Exp $
 *
 * Copyright (c) 2000-2001 Systemics Inc on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.util;

/**
 *  General static support for Stuff.
 */
public class Support
{



    /**
     *  @return true if the byte arrays are equal, false if not
     */
    public static boolean equals(byte[] b1, byte[] b2)
    {
        if (b1 == null && b2 == null)
            return true ;
        if (b1 == null || b2 == null)
            return false ;
        if (b1.length != b2.length)
            return false ;

        // check if the data components are equivalent 
        String s1 = new String(b1);  
        String s2 = new String(b2);

        return s1.equals(s2) ;
    }
}