Package org.bitcoinj.base
Class Sha256Hash
- java.lang.Object
-
- org.bitcoinj.base.Sha256Hash
-
- All Implemented Interfaces:
java.lang.Comparable<Sha256Hash>
public class Sha256Hash extends java.lang.Object implements java.lang.Comparable<Sha256Hash>
ASha256Hash
wraps abyte[]
so thatequals(java.lang.Object)
andhashCode()
work correctly, allowing it to be used as a key in a map. It also checks that thelength
is correct (equal toLENGTH
) and provides a bit more type safety.Given that
Sha256Hash
instances can be created usingwrapReversed(byte[])
ortwiceOf(byte[])
or by wrapping raw bytes, there is no guarantee that if twoSha256Hash
instances are found equal (viaequals(Object)
) that their preimages would be the same (even in the absence of a hash collision.)
-
-
Field Summary
Fields Modifier and Type Field Description static int
LENGTH
static Sha256Hash
ZERO_HASH
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
compareTo(Sha256Hash other)
boolean
equals(java.lang.Object o)
byte[]
getBytes()
Returns the internal byte array, without defensively copying.byte[]
getReversedBytes()
Deprecated.useserialize()
static byte[]
hash(byte[] input)
Calculates the SHA-256 hash of the given bytes.static byte[]
hash(byte[] input, int offset, int length)
Calculates the SHA-256 hash of the given byte range.int
hashCode()
Returns the last four bytes of the wrapped hash.static byte[]
hashTwice(byte[] input)
Calculates the SHA-256 hash of the given bytes, and then hashes the resulting hash again.static byte[]
hashTwice(byte[] input1, byte[] input2)
Calculates the hash of hash on the given chunks of bytes.static byte[]
hashTwice(byte[] input, int offset, int length)
Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again.static byte[]
hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2)
Calculates the hash of hash on the given byte ranges.static java.security.MessageDigest
newDigest()
Returns a new SHA-256 MessageDigest instance.static Sha256Hash
of(byte[] contents)
Creates a new instance containing the calculated (one-time) hash of the given bytes.static Sha256Hash
of(java.io.File file)
Creates a new instance containing the calculated (one-time) hash of the given file's contents.static Sha256Hash
read(java.nio.ByteBuffer buf)
Create a new instance by reading from the given buffer.byte[]
serialize()
Allocates a byte array and writes the hash into it, in reversed order.java.math.BigInteger
toBigInteger()
Returns the bytes interpreted as a positive integer.java.lang.String
toString()
static Sha256Hash
twiceOf(byte[] contents)
Creates a new instance containing the hash of the calculated hash of the given bytes.static Sha256Hash
twiceOf(byte[] content1, byte[] content2)
Creates a new instance containing the hash of the calculated hash of the given bytes.static Sha256Hash
wrap(byte[] rawHashBytes)
Creates a new instance that wraps the given hash value.static Sha256Hash
wrap(java.lang.String hexString)
Creates a new instance that wraps the given hash value (represented as a hex string).static Sha256Hash
wrapReversed(byte[] rawHashBytes)
Creates a new instance that wraps the given hash value, but with byte order reversed.java.nio.ByteBuffer
write(java.nio.ByteBuffer buf)
Write hash into the given buffer.
-
-
-
Field Detail
-
LENGTH
public static final int LENGTH
- See Also:
- Constant Field Values
-
ZERO_HASH
public static final Sha256Hash ZERO_HASH
-
-
Method Detail
-
wrap
public static Sha256Hash wrap(byte[] rawHashBytes)
Creates a new instance that wraps the given hash value.- Parameters:
rawHashBytes
- the raw hash bytes to wrap- Returns:
- a new instance
- Throws:
java.lang.IllegalArgumentException
- if the given array length is not exactly 32
-
wrap
public static Sha256Hash wrap(java.lang.String hexString)
Creates a new instance that wraps the given hash value (represented as a hex string).- Parameters:
hexString
- a hash value represented as a hex string- Returns:
- a new instance
- Throws:
java.lang.IllegalArgumentException
- if the given string is not a valid hex string, or if it does not represent exactly 32 bytes
-
wrapReversed
public static Sha256Hash wrapReversed(byte[] rawHashBytes)
Creates a new instance that wraps the given hash value, but with byte order reversed.- Parameters:
rawHashBytes
- the raw hash bytes to wrap- Returns:
- a new instance
- Throws:
java.lang.IllegalArgumentException
- if the given array length is not exactly 32
-
read
public static Sha256Hash read(java.nio.ByteBuffer buf) throws java.nio.BufferUnderflowException
Create a new instance by reading from the given buffer.- Parameters:
buf
- buffer to read from- Returns:
- a new instance
- Throws:
java.nio.BufferUnderflowException
- if the read hash extends beyond the remaining bytes of the buffer
-
of
public static Sha256Hash of(byte[] contents)
Creates a new instance containing the calculated (one-time) hash of the given bytes.- Parameters:
contents
- the bytes on which the hash value is calculated- Returns:
- a new instance containing the calculated (one-time) hash
-
twiceOf
public static Sha256Hash twiceOf(byte[] contents)
Creates a new instance containing the hash of the calculated hash of the given bytes.- Parameters:
contents
- the bytes on which the hash value is calculated- Returns:
- a new instance containing the calculated (two-time) hash
-
twiceOf
public static Sha256Hash twiceOf(byte[] content1, byte[] content2)
Creates a new instance containing the hash of the calculated hash of the given bytes.- Parameters:
content1
- first bytes on which the hash value is calculatedcontent2
- second bytes on which the hash value is calculated- Returns:
- a new instance containing the calculated (two-time) hash
-
of
public static Sha256Hash of(java.io.File file) throws java.io.IOException
Creates a new instance containing the calculated (one-time) hash of the given file's contents. The file contents are read fully into memory, so this method should only be used with small files.- Parameters:
file
- the file on which the hash value is calculated- Returns:
- a new instance containing the calculated (one-time) hash
- Throws:
java.io.IOException
- if an error occurs while reading the file
-
newDigest
public static java.security.MessageDigest newDigest()
Returns a new SHA-256 MessageDigest instance. This is a convenience method which wraps the checked exception that can never occur with a RuntimeException.- Returns:
- a new SHA-256 MessageDigest instance
-
hash
public static byte[] hash(byte[] input)
Calculates the SHA-256 hash of the given bytes.- Parameters:
input
- the bytes to hash- Returns:
- the hash (in big-endian order)
-
hash
public static byte[] hash(byte[] input, int offset, int length)
Calculates the SHA-256 hash of the given byte range.- Parameters:
input
- the array containing the bytes to hashoffset
- the offset within the array of the bytes to hashlength
- the number of bytes to hash- Returns:
- the hash (in big-endian order)
-
hashTwice
public static byte[] hashTwice(byte[] input)
Calculates the SHA-256 hash of the given bytes, and then hashes the resulting hash again.- Parameters:
input
- the bytes to hash- Returns:
- the double-hash (in big-endian order)
-
hashTwice
public static byte[] hashTwice(byte[] input1, byte[] input2)
Calculates the hash of hash on the given chunks of bytes. This is equivalent to concatenating the two chunks and then passing the result tohashTwice(byte[])
.
-
hashTwice
public static byte[] hashTwice(byte[] input, int offset, int length)
Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again.- Parameters:
input
- the array containing the bytes to hashoffset
- the offset within the array of the bytes to hashlength
- the number of bytes to hash- Returns:
- the double-hash (in big-endian order)
-
hashTwice
public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2)
Calculates the hash of hash on the given byte ranges. This is equivalent to concatenating the two ranges and then passing the result tohashTwice(byte[])
.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
Returns the last four bytes of the wrapped hash. This should be unique enough to be a suitable hash code even for blocks, where the goal is to try and get the first bytes to be zeros (i.e. the value as a big integer lower than the target value).- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toBigInteger
public java.math.BigInteger toBigInteger()
Returns the bytes interpreted as a positive integer.
-
getBytes
public byte[] getBytes()
Returns the internal byte array, without defensively copying. Therefore do NOT modify the returned array.
-
serialize
public byte[] serialize()
Allocates a byte array and writes the hash into it, in reversed order.- Returns:
- byte array containing the hash
-
getReversedBytes
@Deprecated public byte[] getReversedBytes()
Deprecated.useserialize()
-
write
public java.nio.ByteBuffer write(java.nio.ByteBuffer buf) throws java.nio.BufferOverflowException
Write hash into the given buffer.- Parameters:
buf
- buffer to write into- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException
- if the hash doesn't fit the remaining buffer
-
compareTo
public int compareTo(Sha256Hash other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<Sha256Hash>
-
-