|
1 |
| -/* |
| 1 | +/* |
2 | 2 | * Bitcoin cryptography library
|
3 | 3 | * Copyright (c) Project Nayuki
|
4 |
| - * |
| 4 | + * |
5 | 5 | * https://www.nayuki.io/page/bitcoin-cryptography-library
|
6 | 6 | * https://github.com/nayuki/Bitcoin-Cryptography-Library
|
7 | 7 | */
|
|
17 | 17 | * <p>Note that by Bitcoin convention, SHA-256 hash strings are serialized in byte-reversed order.
|
18 | 18 | * For example, these three lines all represent the same hash value:</p>
|
19 | 19 | * <ul>
|
20 |
| - * <li>Bigint: 0x0102030405060708091011121314151617181920212223242526272829303132.</li> |
21 |
| - * <li>Byte array: {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32}.</li> |
22 |
| - * <li>Hex string: "3231302928272625242322212019181716151413121110090807060504030201".</li> |
| 20 | + * <li>Bigint: 0x0102030405060708091011121314151617181920212223242526272829303132.</li> |
| 21 | + * <li>Byte array: {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32}.</li> |
| 22 | + * <li>Hex string: "3231302928272625242322212019181716151413121110090807060504030201".</li> |
23 | 23 | * <ul>
|
| 24 | + * |
24 | 25 | * @see Sha256
|
25 | 26 | */
|
26 | 27 | public final class Sha256Hash implements Comparable<Sha256Hash> {
|
27 |
| - |
28 |
| - /*---- Constants ----*/ |
29 |
| - |
30 |
| - /** The number of bytes in each SHA-256 hash value. */ |
31 |
| - public static final int HASH_LENGTH = 32; |
32 |
| - |
33 |
| - |
34 |
| - |
35 |
| - /*---- Fields ----*/ |
36 |
| - |
37 |
| - private final byte[] hash; |
38 |
| - |
39 |
| - |
40 |
| - |
41 |
| - /*---- Constructors ----*/ |
42 |
| - |
43 |
| - /** |
44 |
| - * Constructs a SHA-256 hash object from the specified array of bytes. |
45 |
| - * The array must be 32 bytes long. All 2<sup>256</sup> possible values are valid. |
46 |
| - * Constant-time with respect to the specified value. |
47 |
| - * @throws IllegalArgumentException if the array is not of length 32 |
48 |
| - * @throws NullPointerException if the array is {@code null} |
49 |
| - */ |
50 |
| - public Sha256Hash(byte[] b) { |
51 |
| - Objects.requireNonNull(b); |
52 |
| - if (b.length != HASH_LENGTH) |
53 |
| - throw new IllegalArgumentException(); |
54 |
| - hash = b.clone(); |
55 |
| - } |
56 |
| - |
57 |
| - |
58 |
| - /** |
59 |
| - * Constructs a SHA-256 hash object from the specified hexadecimal string. |
60 |
| - * The string must be 64 characters long and entirely made up of hexadecimal digits. |
61 |
| - * All 2<sup>256</sup> possible values are valid. Not constant-time. |
62 |
| - * @throws IllegalArgumentException if the string is not of length 64 or entirely hexadecimal digits |
63 |
| - * @throws NullPointerException if the string is {@code null} |
64 |
| - */ |
65 |
| - public Sha256Hash(String s) { |
66 |
| - Objects.requireNonNull(s); |
67 |
| - if (s.length() != HASH_LENGTH * 2 || !s.matches("[0-9a-fA-F]*")) |
68 |
| - throw new IllegalArgumentException("Invalid hash string"); |
69 |
| - hash = new byte[HASH_LENGTH]; |
70 |
| - for (int i = 0; i < hash.length; i++) |
71 |
| - hash[hash.length - 1 - i] = (byte)Integer.parseInt(s.substring(i * 2, (i + 1) * 2), 16); |
72 |
| - } |
73 |
| - |
74 |
| - |
75 |
| - |
76 |
| - /*---- Methods ----*/ |
77 |
| - |
78 |
| - /** |
79 |
| - * Returns a new 32-byte array representing this hash value. Constant-time with respect to this hash value. |
80 |
| - * @return a byte array representing this hash |
81 |
| - */ |
82 |
| - public byte[] toBytes() { |
83 |
| - return hash.clone(); |
84 |
| - } |
85 |
| - |
86 |
| - |
87 |
| - /** |
88 |
| - * Tests whether this hash is equal to the specified object. Returns {@code true} if and only if the |
89 |
| - * other object is a {@code Sha256Hash} object with the same byte array values. Not constant-time. |
90 |
| - * @param obj the object to test equality with |
91 |
| - * @return whether the other object is a {@code Sha256Hash} with the same hash value |
92 |
| - */ |
93 |
| - public boolean equals(Object obj) { |
94 |
| - if (obj == this) |
95 |
| - return true; |
96 |
| - else if (!(obj instanceof Sha256Hash)) |
97 |
| - return false; |
98 |
| - else |
99 |
| - return Arrays.equals(hash, ((Sha256Hash)obj).hash); |
100 |
| - } |
101 |
| - |
102 |
| - |
103 |
| - /** |
104 |
| - * Returns the hash code of this object. Constant-time with respect to this hash value. |
105 |
| - * @return the hash code of this object |
106 |
| - */ |
107 |
| - public int hashCode() { |
108 |
| - return (hash[0] & 0xFF) | (hash[1] & 0xFF) << 8 | (hash[2] & 0xFF) << 16 | hash[3] << 24; |
109 |
| - } |
110 |
| - |
111 |
| - |
112 |
| - /** |
113 |
| - * Compares whether this hash is less than, equal to, or greater than the specified hash object. Not constant-time. |
114 |
| - * <p>The comparison is performed in byte-reversed order, which means the string representations are normally ordered. |
115 |
| - * This behavior corresponds to the comparison used in the proof-of-work check for block headers.</p> |
116 |
| - * @return a negative number if {@code this < other}, zero if {@code this == other}, or a positive number if {@code this > other} |
117 |
| - * @throws NullPointerException if the other object is {@code null} |
118 |
| - */ |
119 |
| - public int compareTo(Sha256Hash other) { |
120 |
| - Objects.requireNonNull(other); |
121 |
| - for (int i = hash.length - 1; i >= 0; i--) { |
122 |
| - int temp = (hash[i] & 0xFF) - (other.hash[i] & 0xFF); |
123 |
| - if (temp != 0) |
124 |
| - return temp; |
125 |
| - } |
126 |
| - return 0; |
127 |
| - } |
128 |
| - |
129 |
| - |
130 |
| - /** |
131 |
| - * Returns the hexadecimal string representation of this hash, in lowercase, 64 digits long. |
132 |
| - * Remember that the string is byte-reversed with respect to the byte array. Not constant-time. |
133 |
| - * @return a 64-digit hexadecimal string of this hash |
134 |
| - */ |
135 |
| - public String toString() { |
136 |
| - StringBuilder sb = new StringBuilder(); |
137 |
| - for (int i = hash.length - 1; i >= 0; i--) |
138 |
| - sb.append(String.format("%02x", hash[i])); |
139 |
| - return sb.toString(); |
140 |
| - } |
141 |
| - |
| 28 | + |
| 29 | + /*---- Constants ----*/ |
| 30 | + |
| 31 | + /** |
| 32 | + * The number of bytes in each SHA-256 hash value. |
| 33 | + */ |
| 34 | + public static final int HASH_LENGTH = 32; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + /*---- Fields ----*/ |
| 39 | + |
| 40 | + private final byte[] hash; |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + /*---- Constructors ----*/ |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs a SHA-256 hash object from the specified array of bytes. |
| 48 | + * The array must be 32 bytes long. All 2<sup>256</sup> possible values are valid. |
| 49 | + * Constant-time with respect to the specified value. |
| 50 | + * |
| 51 | + * @throws IllegalArgumentException if the array is not of length 32 |
| 52 | + * @throws NullPointerException if the array is {@code null} |
| 53 | + */ |
| 54 | + public Sha256Hash(byte[] b) { |
| 55 | + Objects.requireNonNull(b); |
| 56 | + if (b.length != HASH_LENGTH) |
| 57 | + throw new IllegalArgumentException(); |
| 58 | + hash = b.clone(); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Constructs a SHA-256 hash object from the specified hexadecimal string. |
| 64 | + * The string must be 64 characters long and entirely made up of hexadecimal digits. |
| 65 | + * All 2<sup>256</sup> possible values are valid. Not constant-time. |
| 66 | + * |
| 67 | + * @throws IllegalArgumentException if the string is not of length 64 or entirely hexadecimal digits |
| 68 | + * @throws NullPointerException if the string is {@code null} |
| 69 | + */ |
| 70 | + public Sha256Hash(String s) { |
| 71 | + Objects.requireNonNull(s); |
| 72 | + if (s.length() != HASH_LENGTH * 2 || !s.matches("[0-9a-fA-F]*")) |
| 73 | + throw new IllegalArgumentException("Invalid hash string"); |
| 74 | + hash = new byte[HASH_LENGTH]; |
| 75 | + for (int i = 0; i < hash.length; i++) |
| 76 | + hash[hash.length - 1 - i] = (byte) Integer.parseInt(s.substring(i * 2, (i + 1) * 2), 16); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + /*---- Methods ----*/ |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns a new 32-byte array representing this hash value. Constant-time with respect to this hash value. |
| 85 | + * |
| 86 | + * @return a byte array representing this hash |
| 87 | + */ |
| 88 | + public byte[] toBytes() { |
| 89 | + return hash.clone(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests whether this hash is equal to the specified object. Returns {@code true} if and only if the |
| 95 | + * other object is a {@code Sha256Hash} object with the same byte array values. Not constant-time. |
| 96 | + * |
| 97 | + * @param obj the object to test equality with |
| 98 | + * @return whether the other object is a {@code Sha256Hash} with the same hash value |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public boolean equals(Object obj) { |
| 102 | + if (obj == this) |
| 103 | + return true; |
| 104 | + else if (!(obj instanceof Sha256Hash)) |
| 105 | + return false; |
| 106 | + else |
| 107 | + return Arrays.equals(hash, ((Sha256Hash) obj).hash); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the hash code of this object. Constant-time with respect to this hash value. |
| 113 | + * |
| 114 | + * @return the hash code of this object |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public int hashCode() { |
| 118 | + return (hash[0] & 0xFF) | (hash[1] & 0xFF) << 8 | (hash[2] & 0xFF) << 16 | hash[3] << 24; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /** |
| 123 | + * Compares whether this hash is less than, equal to, or greater than the specified hash object. Not constant-time. |
| 124 | + * <p>The comparison is performed in byte-reversed order, which means the string representations are normally ordered. |
| 125 | + * This behavior corresponds to the comparison used in the proof-of-work check for block headers.</p> |
| 126 | + * |
| 127 | + * @return a negative number if {@code this < other}, zero if {@code this == other}, or a positive number if {@code this > other} |
| 128 | + * @throws NullPointerException if the other object is {@code null} |
| 129 | + */ |
| 130 | + @Override |
| 131 | + public int compareTo(Sha256Hash other) { |
| 132 | + Objects.requireNonNull(other); |
| 133 | + for (int i = hash.length - 1; i >= 0; i--) { |
| 134 | + int temp = (hash[i] & 0xFF) - (other.hash[i] & 0xFF); |
| 135 | + if (temp != 0) |
| 136 | + return temp; |
| 137 | + } |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns the hexadecimal string representation of this hash, in lowercase, 64 digits long. |
| 144 | + * Remember that the string is byte-reversed with respect to the byte array. Not constant-time. |
| 145 | + * |
| 146 | + * @return a 64-digit hexadecimal string of this hash |
| 147 | + */ |
| 148 | + @Override |
| 149 | + public String toString() { |
| 150 | + StringBuilder sb = new StringBuilder(); |
| 151 | + for (int i = hash.length - 1; i >= 0; i--) |
| 152 | + sb.append(String.format("%02x", hash[i])); |
| 153 | + return sb.toString(); |
| 154 | + } |
| 155 | + |
142 | 156 | }
|
0 commit comments