Skip to content

Commit 9977995

Browse files
committed
null returns null for ImmutableSha256Hash.
1 parent 5f122a4 commit 9977995

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/main/java/com/softwareverde/security/hash/ripemd160/ImmutableRipemd160Hash.java

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static ImmutableRipemd160Hash copyOf(final byte[] bytes) {
2929

3030
protected ImmutableRipemd160Hash(final byte[] bytes) {
3131
super(new byte[BYTE_COUNT]);
32+
33+
if (bytes.length != BYTE_COUNT) {
34+
throw new RuntimeException("Invalid byte count: " + bytes.length);
35+
}
36+
3237
ByteUtil.setBytes(_bytes, bytes);
3338
}
3439

src/main/java/com/softwareverde/security/hash/ripemd160/MutableRipemd160Hash.java

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static MutableRipemd160Hash copyOf(final byte[] bytes) {
3939

4040
protected MutableRipemd160Hash(final byte[] bytes) {
4141
super(bytes);
42+
43+
if (bytes.length != BYTE_COUNT) {
44+
throw new RuntimeException("Invalid byte count: " + bytes.length);
45+
}
4246
}
4347

4448
public MutableRipemd160Hash() {

src/main/java/com/softwareverde/security/hash/sha256/ImmutableSha256Hash.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ public static ImmutableSha256Hash fromHexString(final String hexString) {
1818
}
1919

2020
public static ImmutableSha256Hash copyOf(final byte[] bytes) {
21-
if (bytes.length != BYTE_COUNT) { return null; }
21+
if (bytes == null) { return null; }
22+
if (bytes.length != BYTE_COUNT) {
23+
Logger.warn("NOTICE: Unable to wrap bytes as hash. Invalid byte count: "+ bytes.length);
24+
return null;
25+
}
2226
return new ImmutableSha256Hash(bytes);
2327
}
2428

2529
protected ImmutableSha256Hash(final byte[] bytes) {
2630
super(new byte[BYTE_COUNT]);
31+
32+
if (bytes.length != BYTE_COUNT) {
33+
throw new RuntimeException("Invalid byte count: " + bytes.length);
34+
}
35+
2736
ByteUtil.setBytes(_bytes, bytes);
2837
}
2938

src/main/java/com/softwareverde/security/hash/sha256/MutableSha256Hash.java

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static MutableSha256Hash copyOf(final byte[] bytes) {
3535

3636
protected MutableSha256Hash(final byte[] bytes) {
3737
super(bytes);
38+
39+
if (bytes.length != BYTE_COUNT) {
40+
throw new RuntimeException("Invalid byte count: " + bytes.length);
41+
}
3842
}
3943

4044
public MutableSha256Hash() {

0 commit comments

Comments
 (0)