com.google.bitcoin.core
Class Sha256Hash

java.lang.Object
  extended by com.google.bitcoin.core.Sha256Hash
All Implemented Interfaces:
Serializable, Comparable

public class Sha256Hash
extends Object
implements Serializable, Comparable

A Sha256Hash just wraps a byte[] so that equals and hashcode work correctly, allowing it to be used as keys in a map. It also checks that the length is correct and provides a bit more type safety.

See Also:
Serialized Form

Field Summary
static Sha256Hash ZERO_HASH
           
 
Constructor Summary
Sha256Hash(byte[] rawHashBytes)
          Creates a Sha256Hash by wrapping the given byte array.
Sha256Hash(String hexString)
          Creates a Sha256Hash by decoding the given hex string.
 
Method Summary
 int compareTo(Object o)
           
static Sha256Hash create(byte[] contents)
          Calculates the (one-time) hash of contents and returns it as a new wrapped hash.
static Sha256Hash createDouble(byte[] contents)
          Calculates the hash of the hash of the contents.
 Sha256Hash duplicate()
           
 boolean equals(Object other)
          Returns true if the hashes are equal.
 byte[] getBytes()
           
 int hashCode()
          Hash code of the byte array as calculated by Object.hashCode().
static Sha256Hash hashFileContents(File f)
          Returns a hash of the given files contents.
 BigInteger toBigInteger()
          Returns the bytes interpreted as a positive integer.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO_HASH

public static final Sha256Hash ZERO_HASH
Constructor Detail

Sha256Hash

public Sha256Hash(byte[] rawHashBytes)
Creates a Sha256Hash by wrapping the given byte array. It must be 32 bytes long.


Sha256Hash

public Sha256Hash(String hexString)
Creates a Sha256Hash by decoding the given hex string. It must be 64 characters long.

Method Detail

create

public static Sha256Hash create(byte[] contents)
Calculates the (one-time) hash of contents and returns it as a new wrapped hash.


createDouble

public static Sha256Hash createDouble(byte[] contents)
Calculates the hash of the hash of the contents. This is a standard operation in Bitcoin.


hashFileContents

public static Sha256Hash hashFileContents(File f)
                                   throws IOException
Returns a hash of the given files contents. Reads the file fully into memory before hashing so only use with small files.

Throws:
IOException

equals

public boolean equals(Object other)
Returns true if the hashes are equal.

Overrides:
equals in class Object

hashCode

public int hashCode()
Hash code of the byte array as calculated by Object.hashCode(). Note the difference between a SHA256 secure bytes and the type of quick/dirty bytes used by the Java hashCode method which is designed for use in bytes tables.

Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

toBigInteger

public BigInteger toBigInteger()
Returns the bytes interpreted as a positive integer.


getBytes

public byte[] getBytes()

duplicate

public Sha256Hash duplicate()

compareTo

public int compareTo(Object o)
Specified by:
compareTo in interface Comparable


Copyright © 2014. All rights reserved.