Skip to content

Commit

Permalink
Digest.toLong()
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Nov 27, 2023
1 parent 4a10f0e commit 8569ef5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ public Digeste toDigeste() {
return builder.build();
}

public long toLong() {
return algorithm.toLong(hash);
}

@Override
public String toString() {
String hexString = Hex.hex(getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,20 @@ public Digest random(Random random) {
return new Digest(digestCode(), hash);
}

public long toLong(long[] hash) {
assert hash != null;
long l = 0;
for (var h : hash) {
l ^= h;
}
return l;
}

public UUID toUUID(long[] hash) {
assert hash != null;
return switch (hash.length) {
case 4 -> new UUID(hash[0] ^ hash[2], hash[1] ^ hash[3]);
case 8 -> toUUID(new long[] { hash[0] ^ hash[2], hash[1] ^ hash[3], hash[4] ^ hash[6], hash[5] ^ hash[7] });
case 4 -> new UUID(hash[0] ^ hash[1], hash[2] ^ hash[3]);
case 8 -> toUUID(new long[] { hash[0] ^ hash[1], hash[2] ^ hash[3], hash[4] ^ hash[5], hash[6] ^ hash[7] });
default -> throw new IllegalArgumentException("invalid hash array size: " + hash.length);
};
}
Expand Down

0 comments on commit 8569ef5

Please sign in to comment.