com.nettgryppa.security
Class HashCash

java.lang.Object
  extended by com.nettgryppa.security.HashCash
All Implemented Interfaces:
java.lang.Comparable<HashCash>

public class HashCash
extends java.lang.Object
implements java.lang.Comparable<HashCash>

Class for generation and parsing of HashCash
Copyright 2006 Gregory Rubin grrubin@gmail.com
Permission is given to use, modify, and or distribute this code so long as this message remains attached
Please see the spec at: http://www.hashcash.org/

Version:
1.1
Author:
grrubin@gmail.com

Field Summary
static int DefaultVersion
           
 
Constructor Summary
HashCash(java.lang.String cash)
          Parses and validates a HashCash.
 
Method Summary
 int compareTo(HashCash other)
          Compares the value of two HashCashes
 boolean equals(java.lang.Object obj)
          Two objects are considered equal if they are both of type HashCash and have an identical string representation
static long estimateTime(int value)
          Estimates how many milliseconds it would take to mint a cash of the specified value.
static int estimateValue(int secs)
          Estimates what value (e.g. how many bits of collision) are required for the specified length of time.
 java.util.Calendar getDate()
          The minting date
 java.util.Map<java.lang.String,java.util.List<java.lang.String>> getExtensions()
          Extra data encoded in the HashCash
 java.lang.String getResource()
          The primary resource being protected
 int getValue()
          The value of the HashCash (e.g. how many leading zero bits it has)
 int getVersion()
          Which version of HashCash is used here
static HashCash mintCash(java.lang.String resource, java.util.Calendar date, int value)
          Mints a version 1 HashCash
static HashCash mintCash(java.lang.String resource, java.util.Calendar date, int value, int version)
          Mints a HashCash
static HashCash mintCash(java.lang.String resource, int value)
          Mints a version 1 HashCash using now as the date
static HashCash mintCash(java.lang.String resource, int value, int version)
          Mints a HashCash using now as the date
static HashCash mintCash(java.lang.String resource, java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions, java.util.Calendar date, int value)
          Mints a version 1 HashCash
static HashCash mintCash(java.lang.String resource, java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions, java.util.Calendar date, int value, int version)
          Mints a HashCash
static HashCash mintCash(java.lang.String resource, java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions, int value)
          Mints a version 1 HashCash using now as the date
static HashCash mintCash(java.lang.String resource, java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions, int value, int version)
          Mints a HashCash using now as the date
 java.lang.String toString()
          Returns the canonical string representation of the HashCash
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DefaultVersion

public static final int DefaultVersion
See Also:
Constant Field Values
Constructor Detail

HashCash

public HashCash(java.lang.String cash)
         throws java.security.NoSuchAlgorithmException
Parses and validates a HashCash.

Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest
Method Detail

mintCash

public static HashCash mintCash(java.lang.String resource,
                                int value)
                         throws java.security.NoSuchAlgorithmException
Mints a version 1 HashCash using now as the date

Parameters:
resource - the string to be encoded in the HashCash
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                int value,
                                int version)
                         throws java.security.NoSuchAlgorithmException
Mints a HashCash using now as the date

Parameters:
resource - the string to be encoded in the HashCash
version - Which version to mint. Only valid values are 0 and 1
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Calendar date,
                                int value)
                         throws java.security.NoSuchAlgorithmException
Mints a version 1 HashCash

Parameters:
resource - the string to be encoded in the HashCash
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Calendar date,
                                int value,
                                int version)
                         throws java.security.NoSuchAlgorithmException
Mints a HashCash

Parameters:
resource - the string to be encoded in the HashCash
version - Which version to mint. Only valid values are 0 and 1
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions,
                                int value)
                         throws java.security.NoSuchAlgorithmException
Mints a version 1 HashCash using now as the date

Parameters:
resource - the string to be encoded in the HashCash
extensions - Extra data to be encoded in the HashCash
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions,
                                int value,
                                int version)
                         throws java.security.NoSuchAlgorithmException
Mints a HashCash using now as the date

Parameters:
resource - the string to be encoded in the HashCash
extensions - Extra data to be encoded in the HashCash
version - Which version to mint. Only valid values are 0 and 1
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions,
                                java.util.Calendar date,
                                int value)
                         throws java.security.NoSuchAlgorithmException
Mints a version 1 HashCash

Parameters:
resource - the string to be encoded in the HashCash
extensions - Extra data to be encoded in the HashCash
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

mintCash

public static HashCash mintCash(java.lang.String resource,
                                java.util.Map<java.lang.String,java.util.List<java.lang.String>> extensions,
                                java.util.Calendar date,
                                int value,
                                int version)
                         throws java.security.NoSuchAlgorithmException
Mints a HashCash

Parameters:
resource - the string to be encoded in the HashCash
extensions - Extra data to be encoded in the HashCash
version - Which version to mint. Only valid values are 0 and 1
Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

equals

public boolean equals(java.lang.Object obj)
Two objects are considered equal if they are both of type HashCash and have an identical string representation

Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Returns the canonical string representation of the HashCash

Overrides:
toString in class java.lang.Object

getExtensions

public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getExtensions()
Extra data encoded in the HashCash


getResource

public java.lang.String getResource()
The primary resource being protected


getDate

public java.util.Calendar getDate()
The minting date


getValue

public int getValue()
The value of the HashCash (e.g. how many leading zero bits it has)


getVersion

public int getVersion()
Which version of HashCash is used here


estimateTime

public static long estimateTime(int value)
                         throws java.security.NoSuchAlgorithmException
Estimates how many milliseconds it would take to mint a cash of the specified value.

Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

estimateValue

public static int estimateValue(int secs)
                         throws java.security.NoSuchAlgorithmException
Estimates what value (e.g. how many bits of collision) are required for the specified length of time.

Throws:
java.security.NoSuchAlgorithmException - If SHA1 is not a supported Message Digest

compareTo

public int compareTo(HashCash other)
Compares the value of two HashCashes

Specified by:
compareTo in interface java.lang.Comparable<HashCash>
Parameters:
other -
See Also:
Comparable.compareTo(Object)