From 8569ef57ce56c1eb3d1ceaf8eaca53651d27110a Mon Sep 17 00:00:00 2001 From: Hellblazer Date: Sun, 26 Nov 2023 17:47:20 -0800 Subject: [PATCH] Digest.toLong() --- .../com/salesforce/apollo/cryptography/Digest.java | 4 ++++ .../apollo/cryptography/DigestAlgorithm.java | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cryptography/src/main/java/com/salesforce/apollo/cryptography/Digest.java b/cryptography/src/main/java/com/salesforce/apollo/cryptography/Digest.java index dad1c34b2..3bc5c7a37 100644 --- a/cryptography/src/main/java/com/salesforce/apollo/cryptography/Digest.java +++ b/cryptography/src/main/java/com/salesforce/apollo/cryptography/Digest.java @@ -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()); diff --git a/cryptography/src/main/java/com/salesforce/apollo/cryptography/DigestAlgorithm.java b/cryptography/src/main/java/com/salesforce/apollo/cryptography/DigestAlgorithm.java index bfece121a..e06fa97d7 100644 --- a/cryptography/src/main/java/com/salesforce/apollo/cryptography/DigestAlgorithm.java +++ b/cryptography/src/main/java/com/salesforce/apollo/cryptography/DigestAlgorithm.java @@ -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); }; }