From 80b26d0a23e4e429f9bdf431d26473a8c0102364 Mon Sep 17 00:00:00 2001 From: Bryan Silverthorn Date: Tue, 17 Dec 2013 22:12:58 -0800 Subject: [PATCH 1/3] Fix handling of unsigned arithmetic. --- .../com/github/emboss/siphash/SipHash.java | 20 +++++++++---------- .../github/emboss/siphash/UnsignedInt64.java | 16 +++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/github/emboss/siphash/SipHash.java b/src/main/java/com/github/emboss/siphash/SipHash.java index c0f48c3..39b190b 100644 --- a/src/main/java/com/github/emboss/siphash/SipHash.java +++ b/src/main/java/com/github/emboss/siphash/SipHash.java @@ -14,32 +14,32 @@ public static long digest(SipKey key, byte[] data) { m = UnsignedInt64.binToIntOffset(data, i * 8); s.processBlock(m); } - + m = lastBlock(data, iter); s.processBlock(m); s.finish(); return s.digest(); } - + private static long lastBlock(byte[] data, int iter) { - long last = ((long) data.length) << 56; + long last = (((long) data.length) & 0xff) << 56; int off = iter * 8; switch (data.length % 8) { case 7: - last |= ((long) data[off + 6]) << 48; + last |= (((long) data[off + 6]) & 0xff) << 48; case 6: - last |= ((long) data[off + 5]) << 40; + last |= (((long) data[off + 5]) & 0xff) << 40; case 5: - last |= ((long) data[off + 4]) << 32; + last |= (((long) data[off + 4]) & 0xff) << 32; case 4: - last |= ((long) data[off + 3]) << 24; + last |= (((long) data[off + 3]) & 0xff) << 24; case 3: - last |= ((long) data[off + 2]) << 16; + last |= (((long) data[off + 2]) & 0xff) << 16; case 2: - last |= ((long) data[off + 1]) << 8; + last |= (((long) data[off + 1]) & 0xff) << 8; case 1: - last |= (long) data[off]; + last |= ((long) data[off]) & 0xff; break; case 0: break; diff --git a/src/main/java/com/github/emboss/siphash/UnsignedInt64.java b/src/main/java/com/github/emboss/siphash/UnsignedInt64.java index afc296f..4d3269d 100644 --- a/src/main/java/com/github/emboss/siphash/UnsignedInt64.java +++ b/src/main/java/com/github/emboss/siphash/UnsignedInt64.java @@ -12,14 +12,14 @@ public static long binToInt(byte[] b) { } public static long binToIntOffset(byte[] b, int off) { - return ((long) b[off ]) | - ((long) b[off + 1]) << 8 | - ((long) b[off + 2]) << 16 | - ((long) b[off + 3]) << 24 | - ((long) b[off + 4]) << 32 | - ((long) b[off + 5]) << 40 | - ((long) b[off + 6]) << 48 | - ((long) b[off + 7]) << 56; + return (((long) b[off ]) & 0xff) | + (((long) b[off + 1]) & 0xff) << 8 | + (((long) b[off + 2]) & 0xff) << 16 | + (((long) b[off + 3]) & 0xff) << 24 | + (((long) b[off + 4]) & 0xff) << 32 | + (((long) b[off + 5]) & 0xff) << 40 | + (((long) b[off + 6]) & 0xff) << 48 | + (((long) b[off + 7]) & 0xff) << 56; } public static void intToBin(long l, byte[] b) { From bd4a7234fec0cf50e56f724124d25deadadf8ab6 Mon Sep 17 00:00:00 2001 From: Bryan Silverthorn Date: Sun, 31 Aug 2014 10:07:12 -0700 Subject: [PATCH 2/3] Revert an unnecessary change to lastBlock(). --- src/main/java/com/github/emboss/siphash/SipHash.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/emboss/siphash/SipHash.java b/src/main/java/com/github/emboss/siphash/SipHash.java index 39b190b..daa75c2 100644 --- a/src/main/java/com/github/emboss/siphash/SipHash.java +++ b/src/main/java/com/github/emboss/siphash/SipHash.java @@ -22,7 +22,7 @@ public static long digest(SipKey key, byte[] data) { } private static long lastBlock(byte[] data, int iter) { - long last = (((long) data.length) & 0xff) << 56; + long last = ((long) data.length) << 56; int off = iter * 8; switch (data.length % 8) { From f5b5fa7b760e3b4582bce5094667f23e0c7d7de2 Mon Sep 17 00:00:00 2001 From: Bryan Silverthorn Date: Sun, 31 Aug 2014 10:08:47 -0700 Subject: [PATCH 3/3] Revert removal of trailing whitespace. ... eliminate noise from the PR diff. --- src/main/java/com/github/emboss/siphash/SipHash.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/emboss/siphash/SipHash.java b/src/main/java/com/github/emboss/siphash/SipHash.java index daa75c2..228f27c 100644 --- a/src/main/java/com/github/emboss/siphash/SipHash.java +++ b/src/main/java/com/github/emboss/siphash/SipHash.java @@ -14,13 +14,13 @@ public static long digest(SipKey key, byte[] data) { m = UnsignedInt64.binToIntOffset(data, i * 8); s.processBlock(m); } - + m = lastBlock(data, iter); s.processBlock(m); s.finish(); return s.digest(); } - + private static long lastBlock(byte[] data, int iter) { long last = ((long) data.length) << 56; int off = iter * 8;