|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectme.edwards.des.util.HashUtil
public class HashUtil
Utility class for generating and validating hashes. Also provides utility
methods for manipulating and formatting hashes. Handles generation of the
Proof of Work for Blocks
.
SHA-256 is the hashing method used by this class, and all hashes (except for
Merkle Root hashes) are squared, also known as SHA-256^2, or
SHA-256(SHA-256(DATA)). This is meant to provide added security against a
pre-image attack.
Created on: Oct 16, 2015 at 5:30:31 PM
Field Summary | |
---|---|
private static double |
hashEst
|
private static java.util.logging.Logger |
logger
|
Constructor Summary | |
---|---|
HashUtil()
|
Method Summary | |
---|---|
static int |
estimateTime(double difficulty)
Returns the estimated time (in seconds) to generate a proof of work. |
static java.lang.String |
generateBlockHash(byte[] bytes,
int proof)
Generates a Block hash from a byte array representing a
Block Header. |
static java.lang.String |
generateHash(byte[] bytes)
Generates a hash from given data as an array of bytes. |
static java.lang.String |
generateLeadingZeros(java.lang.String hash)
Formats a hash string with the correct number of leading zeros (64 digits by default). |
static java.lang.String |
generateLeadingZeros(java.lang.String hash,
int digits)
Formats a hash string with the correct number of leading zeros. |
static java.lang.String |
generateMerkleRoot(java.lang.String root1,
java.lang.String root2)
Generates a Merkle Root hash based on two other Merkle Roots. |
static int |
generateProof(byte[] bytes,
int target)
Generates the Proof of Work for a given Block using the
Block Header. |
private static boolean |
validateHash(java.math.BigInteger value,
java.math.BigInteger target)
Validates a hash against the specified target. |
static boolean |
validateProof(byte[] bytes,
int proof,
int target)
Validates a generated Proof of Work for any given Block . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final java.util.logging.Logger logger
private static double hashEst
Constructor Detail |
---|
public HashUtil()
Method Detail |
---|
public static java.lang.String generateLeadingZeros(java.lang.String hash)
hash
- String to format
public static java.lang.String generateLeadingZeros(java.lang.String hash, int digits)
hash
- String to formatdigits
- Number of digits to match (including leading zeros)
public static java.lang.String generateBlockHash(byte[] bytes, int proof)
Block
hash from a byte array representing a
Block Header.
bytes
- Byte array representing a Block Headerproof
- Integer Proof of Work for the specified Block
public static java.lang.String generateHash(byte[] bytes)
bytes
- Byte Array from which to generate hash
public static boolean validateProof(byte[] bytes, int proof, int target)
Block
.
bytes
- The Block in binary formatproof
- Integer Proof of Work for the specified Blocktarget
- Target
in shorthand form. The
hash of the Block Header must be less than the target to be
valid (increasing the difficulty to create a proof with the
power and speed of the network).
public static int generateProof(byte[] bytes, int target) throws java.lang.InterruptedException
Block
using the
Block Header. This algorithm is based on methods used in Adam Back's Hashcash, an anti-spam
solution for email. The generation of the Proof of Work requires the most
time and resources during the mining process. It is based on the
difficulty of finding the pre-image of a hash. Theoretically, any hash
String of 64 characters can be created, but it is difficult to compute
the data that will result in the desired hash.
bytes
- The Block Header in binary formattarget
- The hash must be less than the target to be valid (increasing
the difficulty to create a proof with the power and speed of
the network).
java.lang.InterruptedException
- Thrown if the Thread is interrupted by Node.stopBlockGeneration().private static boolean validateHash(java.math.BigInteger value, java.math.BigInteger target)
value
- The hash which must be validatedtarget
- The hash must be less than the target to be valid (increasing
the difficulty to create a proof with the power and speed of
the network).
public static java.lang.String generateMerkleRoot(java.lang.String root1, java.lang.String root2)
root1
- First Merkle Rootroot2
- Second Merkle Root
public static int estimateTime(double difficulty)
difficulty
- Difficulty of block
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |