[Webfunds-commits] java/webfunds/token/random PrivateParams.java PublicParams.java RandomPair.java
Ian Grigg
iang@cypherpunks.ai
Sun, 1 Apr 2001 22:28:49 -0400 (AST)
iang 01/04/01 22:28:49
Added: webfunds/token/random PrivateParams.java PublicParams.java
RandomPair.java
Log:
Params classes for Random Tokens. Compiles, not tested, but might have
been based on working code in the past ;)
Revision Changes Path
1.1 java/webfunds/token/random/PrivateParams.java
Index: PrivateParams.java
===================================================================
/*
* $Id: PrivateParams.java,v 1.1 2001/04/02 02:28:48 iang Exp $
*
* Copyright (c) Systemics Inc 1995-2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
*/
package webfunds.token.random;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.InvalidKeyException;
import webfunds.sox.Crypto;
import webfunds.token.*;
/**
* Represents a Private (signing) key for a token
* {item, series, expiry, log} tuple.
*
* Refer to AbstractPublicParams for common methods.
*/
public class PrivateParams
extends AbstractPrivateParams
{
/**
* The version number for this structure:
* 0: current
*/
public static final int RAND_PRIV_VERSION = 0;
private PrivateKey key;
/**
* Create a Private Params for Random Tokens.
*/
public PrivateParams(PrivateKey key,
byte[] series, long expiry,
byte[] item,
byte log)
{
super(RAND_PRIV_VERSION, Factory.RANDOM_TOKEN,
series, expiry, item, log);
this.key = key;
}
/**
* Reconstruct the object from a byte array.
*
* @param buf the previously encoded object
* @excep TokenPacketException if the data is badly formatted
*/
public PrivateParams(byte[] buf)
throws TokenPacketException
{
super(buf);
}
/**
* Reconstruct the object from data in an input stream.
*
* @param is the input stream from which to read the data
* @excep TokenPacketException if the data is badly formatted
*/
public PrivateParams(InputStream is)
throws TokenPacketException
{
super(is);
}
/**
* Update this token object with the values from
* a token encoded as a byte array (such as previously
* returned from the encode() method of a token object).
*
* @param token the previosly encoded token
*/
public void decode(InputStream is)
throws IOException
{
DataInputStream dis = new DataInputStream(is);
super.decode(dis);
byte[] b = readByteArray(dis);
try {
key = Crypto.decodePrivateKey(b);
} catch(InvalidKeyException ikex) {
throw new IOException("InvalidKeyEx: " + ikex);
}
}
/**
* Encode a token as a byte array, suitable for
* sending to third parties for depositing.
* If the signature is not present, an unsigned
* token will be encoded.
*
* @return byte[] the token in encoded form
*/
public void encode(OutputStream os)
throws IOException
{
DataOutputStream dos = new DataOutputStream(os);
byte[] b = Crypto.encodePrivateKey(key);
writeByteArray(dos, b);
}
////// Self-Test //////////////////////////////////
public String toString()
{
String s = super.toString();
s += "\n" + key.toString() + "\n";
return s;
}
public boolean equals(java.lang.Object obj)
{
if (obj == null || !(obj instanceof PrivateParams))
return false;
PrivateParams other = (PrivateParams)obj;
if (!super.equals(other))
return false;
if (!Crypto.equals(key, other.key))
return false;
return true;
}
/* public abstract static PrivateParams example(); */
}
1.1 java/webfunds/token/random/PublicParams.java
Index: PublicParams.java
===================================================================
/*
* $Id: PublicParams.java,v 1.1 2001/04/02 02:28:48 iang Exp $
*
* Copyright (c) Systemics Inc 1995-2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
*/
package webfunds.token.random;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.InvalidKeyException;
import webfunds.sox.Crypto;
import webfunds.token.*;
/**
* This class represents a Public (verifying) key for a token.
*
*/
public class PublicParams
extends AbstractPublicParams
{
/**
* The version number for this structure:
* 0: current
*/
public static final byte RAND_PUB_VERSION = 0;
private PublicKey key;
/**
* Create a Public Params for Random Tokens.
*
* @param item is the description of which instrument relates
* @param series is the short descriptor of which set of tokens this is
* @param expiry date on which this series will expire
* @param log the coin size, log base 2 of quantity
*/
public PublicParams(PublicKey key,
byte[] series, long expiry,
byte[] item,
byte log)
{
super(RAND_PUB_VERSION, Factory.RANDOM_TOKEN,
series, expiry, item, log);
this.key = key;
}
/**
* Construct a token object from a byte array
* that was previously returned from the encode()
* method of a token object.
*
* @param token the previously encoded token
* @excep TokenPacketException The token data is badly formatted
*/
public PublicParams(byte[] buf)
throws TokenPacketException
{
super(buf);
}
/**
* Construct a token object from data in an input stream,
* where the data was previously returned from the encode()
* method of a token object.
*
* @param is the input stream from which to read the token data
* @excep TokenPacketException The token data is badly formatted
*/
public PublicParams(InputStream is)
throws TokenPacketException
{
super(is);
}
/**
* Update this token object with the values from
* a token encoded as a byte array (such as previously
* returned from the encode() method of a token object).
*
* @param token the previosly encoded token
*/
public void decode(InputStream is)
throws IOException
{
DataInputStream dis = new DataInputStream(is);
super.decode(dis);
byte[] b = readByteArray(dis);
try {
key = Crypto.decodePublicKey(b);
} catch(InvalidKeyException ikex) {
throw new IOException("InvalidKeyEx: " + ikex);
}
}
/**
* Encode a token as a byte array, suitable for
* sending to third parties for depositing.
* If the signature is not present, an unsigned
* token will be encoded.
*
* @return byte[] the token in encoded form
*/
public void encode(OutputStream os)
throws IOException
{
DataOutputStream dos = new DataOutputStream(os);
byte[] b = Crypto.encodePublicKey(key);
writeByteArray(dos, b);
}
////// Self-Test //////////////////////////////////
public String toString()
{
String s = super.toString();
s += "\n" + key.toString() + "\n";
return s;
}
public boolean equals(java.lang.Object obj)
{
if (obj == null || !(obj instanceof PublicParams))
return false;
PublicParams other = (PublicParams)obj;
if (!super.equals(other))
return false;
if (!Crypto.equals(key, other.key))
return false;
return true;
}
/* public abstract static PublicParams example(); */
}
1.1 java/webfunds/token/random/RandomPair.java
Index: RandomPair.java
===================================================================
/*
* $Id: RandomPair.java,v 1.1 2001/04/02 02:28:48 iang Exp $
*
* Copyright (c) Systemics Inc 1995-2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
*/
package webfunds.token.random;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.KeyException;
import java.security.SecureRandom;
import webfunds.sox.Crypto;
import webfunds.token.*;
/**
* Represents a Private (signing) key for a token
* {item, series, expiry, log} tuple.
*
* Refer to AbstractPublicParams for common methods.
*/
public class RandomPair
implements ParamsPair
{
private AbstractPrivateParams priv;
private AbstractPublicParams pub;
/**
* Create an uninitialised Pair.
* Call generate() with some params to make the contents.
*/
public RandomPair()
{
}
/**
* Create an uninitialised Random Pair.
*
* @param item is the description of which instrument relates
* @param series is the short descriptor of which set of tokens this is
* @param expiry date on which this series will expire
* @param log the coin size, log base 2 of quantity
*/
public void generate(SecureRandom sr,
byte[] item,
byte[] series, long expiry,
byte log)
throws TokenKeyException
{
KeyPair kp;
try {
//
// This Random method is for demo purposes, so doesn't
// need any great key strength.
//
kp = Crypto.generateKeys(768);
} catch (Throwable ex) {
//
// As the generateKeys uses a provider, we don't know for
// for sure that exceptions can't be thrown. Be dramatic
// catch it all, and bounce the account.
//
ex.printStackTrace();
throw new TokenKeyException(ex.getMessage());
}
PrivateKey secKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();
pub = new PublicParams(pubKey, series, expiry, item, log);
priv = new PrivateParams(secKey, series, expiry, item, log);
}
/**
* Get the private half of the pair, used for signing the token.
*/
public AbstractPrivateParams getPrivate() { return priv; }
/**
* Get the public half of the pair, used for verifying the token.
*/
public AbstractPublicParams getPublic() { return pub; }
}