webfunds.util
Class Armoury

java.lang.Object
  |
  +--webfunds.util.Armoury

public abstract class Armoury
extends java.lang.Object

This class provides methods for encoding and decoding data in MIME base64 format. Also included are many convenience methods for encoding and decoding various types of object.

All of the methods either take a byte array, a String, or an Object, and return a byte array or String (depending upon the method) containing the base 64 encoded data. The return value represents a sequence of characters, and depending upon the method, can be either a byte array or String, since these characters are from a subset of both the Unicode and ASCII character repertoires.

See Base64Coder for details on how Base64 works.


Constructor Summary
Armoury()
           
 
Method Summary
static void canonicalTest(byte[] buf)
           
static java.lang.String createSignedData(byte[] data, byte[] binarySignature)
          Create the signed data
static byte[] decode(java.lang.String msg)
          Decodes a byte array containing base64-encoded ASCII.
static byte[] decodeByteArray(java.lang.String type, java.lang.String buf)
          Decode some Base 64 encoded data, first of all searching for the appropriate "BEGIN" line, and reading up until the "END" line.
static java.security.cert.Certificate decodeCert(java.lang.String buf)
          Decode a Certificate object from ascii-armoured base64 encoding
static int decodeChecksum(java.lang.String s)
           
static byte[] decodeData(byte[] data)
          This method returns the data part of a signed message removing the -(space) and changing line endings to local format.
static java.security.PrivateKey decodePrivateKey(java.lang.String buf)
          Decode an PrivateKey object from ascii-armoured base64 encoding
static byte[] decodeSig(java.lang.String buf)
          Decode a Signature from ascii-armoured base64 encoding
static java.lang.String encode(java.lang.String type, byte[] data)
          Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines, and default platform newline and 51 chars per line.
static java.lang.String encode(java.lang.String type, byte[] data, int maxline)
          Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines.
static java.lang.String encode(java.lang.String type, byte[] data, int maxline, java.lang.String headers, java.lang.String newline)
          Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines.
static java.lang.String encodeCert(java.security.cert.Certificate cert)
          Encode a Certificate object, using ascii-armoured base64 encoding
static java.lang.String encodeChecksum(byte[] buf)
          deprecated?
static java.lang.String encodePrivateKey(java.security.PrivateKey key)
          Encode a PrivateKey object using ascii-armoured base64 encoding
static java.lang.String encodePublicKey(java.security.PublicKey key)
          Encode a PublicKey object using ascii-armoured base64 encoding
static java.lang.String encodeSig(byte[] sig)
          Encode a Signature (stored in a byte array) using ascii-armoured base64 encoding
static java.security.cert.Certificate getCertificate(byte[] data)
           
static byte[] getChecksum(byte[] buf)
           
static java.lang.String getErrors()
          Obviously not thread-relevant!
static boolean isPresent(java.lang.String type, java.lang.String buf)
          Determine if this header can be found
static void main(java.lang.String[] args)
           
static byte[] prepareDataToSign(byte[] data)
          This method prepares data to be signed (i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Armoury

public Armoury()
Method Detail

getErrors

public static java.lang.String getErrors()
Obviously not thread-relevant!

decode

public static byte[] decode(java.lang.String msg)
                     throws java.io.IOException
Decodes a byte array containing base64-encoded ASCII. Characters with ASCII code points <= (THIS ARE WHITESPACE NEWLINES) INCLUDES AND 32 IGNORED.
Parameters:
data - the data to be encoded.
Returns:
the decoded data.
Throws:
java.io.IOException - if data contains invalid characters, or is incorrectly padded.
InvalidChecksumException - if the checksum fails.

getChecksum

public static byte[] getChecksum(byte[] buf)

encodeChecksum

public static java.lang.String encodeChecksum(byte[] buf)
deprecated?

decodeChecksum

public static int decodeChecksum(java.lang.String s)

isPresent

public static boolean isPresent(java.lang.String type,
                                java.lang.String buf)
                         throws java.io.IOException
Determine if this header can be found
Parameters:
header - the string following the "BEGIN" on the ascii armour.
buf - a String containing the Base 64 encoded data
Returns:
true if the "BEGIN" header can be found
Throws:
java.io.IOException - if buf contains invalid characters, or is incorrectly padded.

decodeByteArray

public static byte[] decodeByteArray(java.lang.String type,
                                     java.lang.String buf)
                              throws java.io.IOException
Decode some Base 64 encoded data, first of all searching for the appropriate "BEGIN" line, and reading up until the "END" line.
Parameters:
header - the string following the "BEGIN" and "END" on the ascii armouring.
buf - a String containing the Base 64 encoded data
Returns:
the decoded ascii-armoured data, as a byte array
Throws:
java.io.IOException - if buf contains invalid characters, or is incorrectly padded.

encode

public static java.lang.String encode(java.lang.String type,
                                      byte[] data)
Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines, and default platform newline and 51 chars per line.
Parameters:
data - a byte array containing the data to be encoded
Returns:
the encoded base64 ascii-armoured data, as a String

encode

public static java.lang.String encode(java.lang.String type,
                                      byte[] data,
                                      int maxline)
Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines.
Parameters:
header - the string following the "BEGIN" and "END" on the ascii armour.
data - a byte array containing the data to be encoded
Returns:
the encoded base64 ascii-armoured data, as a String

encode

public static java.lang.String encode(java.lang.String type,
                                      byte[] data,
                                      int maxline,
                                      java.lang.String headers,
                                      java.lang.String newline)
Encode data (as a byte array) into ascii-armoured Base 64 encoded data, with "BEGIN" and "END" lines.
Parameters:
header - the string following the "BEGIN" and "END" on the ascii armour.
data - a byte array containing the data to be encoded
Returns:
the encoded base64 ascii-armoured data, as a String

encodeCert

public static java.lang.String encodeCert(java.security.cert.Certificate cert)
Encode a Certificate object, using ascii-armoured base64 encoding
Parameters:
cert - the certificate object to be encoded
Returns:
the ascii-armoured certificate

getCertificate

public static java.security.cert.Certificate getCertificate(byte[] data)
                                                     throws java.io.IOException

decodeCert

public static java.security.cert.Certificate decodeCert(java.lang.String buf)
                                                 throws SOXKeyException
Decode a Certificate object from ascii-armoured base64 encoding
Parameters:
buf - a String containing the ascii-armoured certificate
Returns:
the Certificate object

encodePublicKey

public static java.lang.String encodePublicKey(java.security.PublicKey key)
Encode a PublicKey object using ascii-armoured base64 encoding

Parameters:
key - the PublicKey object to be encoded
Returns:
the base64 encoded PublicKey object
See Also:
PublicKey

encodePrivateKey

public static java.lang.String encodePrivateKey(java.security.PrivateKey key)
Encode a PrivateKey object using ascii-armoured base64 encoding
Parameters:
key - the PrivateKey object to be encoded
Returns:
the base64 encoded PrivateKey object
See Also:
PrivateKey

decodePrivateKey

public static java.security.PrivateKey decodePrivateKey(java.lang.String buf)
                                                 throws java.security.InvalidKeyException
Decode an PrivateKey object from ascii-armoured base64 encoding
Parameters:
buf - a String containing the base64 encoded PrivateKey
Returns:
the PrivateKey object
See Also:
PrivateKey

encodeSig

public static java.lang.String encodeSig(byte[] sig)
Encode a Signature (stored in a byte array) using ascii-armoured base64 encoding
Parameters:
keyPair - the KeyPair object to be encoded
Returns:
the base64 encoded KeyPair object

decodeSig

public static byte[] decodeSig(java.lang.String buf)
                        throws java.io.IOException
Decode a Signature from ascii-armoured base64 encoding
Parameters:
buf - a String containing the base64 encoded signature
Returns:
the signature (stored in a byte array)

createSignedData

public static java.lang.String createSignedData(byte[] data,
                                                byte[] binarySignature)
                                         throws java.io.IOException,
                                                java.io.FileNotFoundException,
                                                java.security.KeyException
Create the signed data
Parameters:
data - the original data (not the preparedToSign data)
binary - Signature the signature of the signedData
Returns:
the Signed Data

decodeData

public static byte[] decodeData(byte[] data)
                         throws java.io.IOException
This method returns the data part of a signed message removing the -(space) and changing line endings to local format. Used by Ricardian Contracts.

prepareDataToSign

public static byte[] prepareDataToSign(byte[] data)
                                throws java.io.IOException
This method prepares data to be signed (i.e. ensures lines end with \r\n). It should always be called before signing data
Parameters:
data - the original data
Returns:
trimed and \r\n ended data

canonicalTest

public static void canonicalTest(byte[] buf)
                          throws java.lang.Exception

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception