类 Sha256Hash
- java.lang.Object
-
- network.nerve.core.crypto.Sha256Hash
-
- 所有已实现的接口:
Serializable,Comparable<Sha256Hash>
public class Sha256Hash extends Object implements Serializable, Comparable<Sha256Hash>
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.- 作者:
- 另请参阅:
- 序列化表格
-
-
字段概要
字段 修饰符和类型 字段 说明 static intLENGTHbytesstatic Sha256HashZERO_HASH
-
构造器概要
构造器 构造器 说明 Sha256Hash(byte[] rawHashBytes)已过时。Sha256Hash(String hexString)已过时。
-
方法概要
所有方法 静态方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 intcompareTo(Sha256Hash other)static Sha256Hashcreate(byte[] contents)已过时。static Sha256HashcreateDouble(byte[] contents)已过时。booleanequals(Object o)byte[]getBytes()Returns the internal byte array, without defensively copying.byte[]getReversedBytes()Returns a reversed copy of the internal byte array.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.inthashCode()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[] 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)static MessageDigestnewDigest()Returns a new SHA-256 MessageDigest instance.static Sha256Hashof(byte[] contents)Creates a new instance containing the calculated (one-time) hash of the given bytes.static Sha256Hashof(File file)Creates a new instance containing the calculated (one-time) hash of the given file's contents.BigIntegertoBigInteger()Returns the bytes interpreted as a positive integer.StringtoString()static Sha256HashtwiceOf(byte[] contents)Creates a new instance containing the hash of the calculated hash of the given bytes.static Sha256Hashwrap(byte[] rawHashBytes)Creates a new instance that wraps the given hash value.static Sha256Hashwrap(String hexString)Creates a new instance that wraps the given hash value (represented as a hex string).static Sha256HashwrapReversed(byte[] rawHashBytes)Creates a new instance that wraps the given hash value, but with byte order reversed.
-
-
-
字段详细资料
-
LENGTH
public static final int LENGTH
bytes- 另请参阅:
- 常量字段值
-
ZERO_HASH
public static final Sha256Hash ZERO_HASH
-
-
构造器详细资料
-
Sha256Hash
@Deprecated public Sha256Hash(byte[] rawHashBytes)
已过时。
-
Sha256Hash
@Deprecated public Sha256Hash(String hexString)
已过时。
-
-
方法详细资料
-
wrap
public static Sha256Hash wrap(byte[] rawHashBytes)
Creates a new instance that wraps the given hash value.- 参数:
rawHashBytes- the raw hash bytes to wrap- 返回:
- a new instance
- 抛出:
IllegalArgumentException- if the given array length is not exactly 32
-
wrap
public static Sha256Hash wrap(String hexString)
Creates a new instance that wraps the given hash value (represented as a hex string).- 参数:
hexString- a hash value represented as a hex string- 返回:
- a new instance
- 抛出:
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.- 参数:
rawHashBytes- the raw hash bytes to wrap- 返回:
- a new instance
- 抛出:
IllegalArgumentException- if the given array length is not exactly 32
-
create
@Deprecated public static Sha256Hash create(byte[] contents)
已过时。
-
of
public static Sha256Hash of(byte[] contents)
Creates a new instance containing the calculated (one-time) hash of the given bytes.- 参数:
contents- the bytes on which the hash value is calculated- 返回:
- a new instance containing the calculated (one-time) hash
-
createDouble
@Deprecated public static Sha256Hash createDouble(byte[] contents)
已过时。
-
twiceOf
public static Sha256Hash twiceOf(byte[] contents)
Creates a new instance containing the hash of the calculated hash of the given bytes.- 参数:
contents- the bytes on which the hash value is calculated- 返回:
- a new instance containing the calculated (two-time) hash
-
of
public static Sha256Hash of(File file) throws 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.
- 参数:
file- the file on which the hash value is calculated- 返回:
- a new instance containing the calculated (one-time) hash
- 抛出:
IOException- if an error occurs while reading the file
-
newDigest
public static 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.
- 返回:
- a new SHA-256 MessageDigest instance
-
hash
public static byte[] hash(byte[] input)
Calculates the SHA-256 hash of the given bytes.- 参数:
input- the bytes to hash- 返回:
- 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.- 参数:
input- the array containing the bytes to hashoffset- the offset within the array of the bytes to hashlength- the number of bytes to hash- 返回:
- 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.- 参数:
input- the bytes to hash- 返回:
- the double-hash (in big-endian order)
-
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.- 参数:
input- the array containing the bytes to hashoffset- the offset within the array of the bytes to hashlength- the number of bytes to hash- 返回:
- the double-hash (in big-endian order)
-
hashTwice
public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2)
-
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).
-
toBigInteger
public BigInteger toBigInteger()
Returns the bytes interpreted as a positive integer.- 返回:
- BigInteger
-
getBytes
public byte[] getBytes()
Returns the internal byte array, without defensively copying. Therefore do NOT modify the returned array.- 返回:
- byte[]
-
getReversedBytes
public byte[] getReversedBytes()
Returns a reversed copy of the internal byte array.- 返回:
- byte[]
-
compareTo
public int compareTo(Sha256Hash other)
- 指定者:
compareTo在接口中Comparable<Sha256Hash>
-
-