From e26fff7c5aee64574917e9a46c6f08a1ba55f280 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 17 Jun 2013 00:30:43 +0200 Subject: m + fix for python version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- c/sha3sum.c | 20 ++++++++-------- pure-java/sha3sum.java | 20 ++++++++-------- python3/sha3sum.py | 63 +++++++++++++++++++++++++++----------------------- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/c/sha3sum.c b/c/sha3sum.c index d3657d7..8872f6d 100644 --- a/c/sha3sum.c +++ b/c/sha3sum.c @@ -294,7 +294,7 @@ int main(int argc, char** argv) free(*linger); free(linger); free(files); - return 2; + return 0; } else { @@ -505,6 +505,15 @@ int main(int argc, char** argv) } + fprintf(stderr, "Bitrate: %li\n", r); + fprintf(stderr, "Capacity: %li\n", c); + fprintf(stderr, "Word size: %li\n", w); + fprintf(stderr, "State size: %li\n", s); + fprintf(stderr, "Output Size: %li\n", o); + fprintf(stderr, "Iterations: %li\n", i); + fprintf(stderr, "Squeezes: %li\n", j); + + if (r > s) { ERR("the bitrate must not be higher than the state size."); @@ -522,15 +531,6 @@ int main(int argc, char** argv) } - fprintf(stderr, "Bitrate: %li", r); - fprintf(stderr, "Capacity: %li", c); - fprintf(stderr, "Word size: %li", w); - fprintf(stderr, "State size: %li", s); - fprintf(stderr, "Output Size: %li", o); - fprintf(stderr, "Iterations: %li", i); - fprintf(stderr, "Squeezes: %li", j); - - if (fptr == 0) files[fptr++] = null; if (i < 1) diff --git a/pure-java/sha3sum.java b/pure-java/sha3sum.java index 3b4cec9..783e41b 100644 --- a/pure-java/sha3sum.java +++ b/pure-java/sha3sum.java @@ -142,7 +142,7 @@ public class sha3sum System.out.println("You should have received a copy of the GNU General Public License"); System.out.println("along with this program. If not, see ."); System.out.println(""); - System.exit(2); + System.exit(0); } else { @@ -292,6 +292,15 @@ public class sha3sum } + System.err.println("Bitrate: " + r); + System.err.println("Capacity: " + c); + System.err.println("Word size: " + w); + System.err.println("State size: " + s); + System.err.println("Output size: " + o); + System.err.println("Iterations: " + i); + System.err.println("Squeezes: " + j); + + if (r > s) { System.err.println(cmd + ": the bitrate must not be higher than the state size."); System.exit(6); @@ -306,15 +315,6 @@ public class sha3sum } - System.err.println("Bitrate: " + r); - System.err.println("Capacity: " + c); - System.err.println("Word size: " + w); - System.err.println("State size: " + s); - System.err.println("Output size: " + o); - System.err.println("Iterations: " + i); - System.err.println("Squeezes: " + j); - - if (fptr == 0) files[fptr++] = null; if (i < 1) diff --git a/python3/sha3sum.py b/python3/sha3sum.py index 32b09d1..ebb4c98 100755 --- a/python3/sha3sum.py +++ b/python3/sha3sum.py @@ -467,7 +467,9 @@ class SHA3: @param withReturn:bool Whether to return the hash instead of just do a quick squeeze phrase and return `None` @return :bytes? The hash sum, or `None` if `withReturn` is `False` ''' - if (msglen is not None) and isinstance(msglen, bool): + if (msg is not None) and isinstance(msg, bool): + (msg, withReturn) = (withReturn, msg) + elif (msglen is not None) and isinstance(msglen, bool): (msglen, withReturn) = (withReturn, msglen) if msg is None: msg = bytes([]) @@ -593,7 +595,7 @@ class SHA3: ''' for i in range(times): SHA3.keccakF(SHA3.S) # Last squeeze did not do a ending squeeze - int olen = SHA3.n + olen = SHA3.n while olen > SHA3.r: olen -= SHA3.r SHA3.keccakF(SHA3.S) @@ -725,7 +727,7 @@ along with this program. If not, see . ''' % (_r, _c, _w, _o, _s, _i, _j)).encode('utf-8')) sys.stderr.buffer.flush() - exit(2) + exit(0) else: if linger[1] is None: linger[1] = arg @@ -813,24 +815,27 @@ along with this program. If not, see . if C is not None: c = C if (c <= 0) or ((c & 7) != 0): - printerr(cmd + ': the capacity must be a positive multiple of 8.') - sys.exit(6); + printerr(cmd + ': the capacity must be a positive multiple of 8.') + sys.exit(6) if R is not None: r = R if (r <= 0) or ((r & 7) != 0): printerr(cmd + ': the bitrate must be a positive multiple of 8.') - sys.exit(6); + sys.exit(6) if O is not None: o = O if o <= 0: printerr(cmd + ': the output size must be positive.') - sys.exit(6); + sys.exit(6) if (R is None) and (C is None) and (O is None): ## s? - r = -((c = (o = ((((s = S is None ? _s : s) << 5) / 100 + 7) >> 3) << 3) << 1) - s); + s = _s if S is None else s + o = (((s << 5) // 100 + 7) >> 3) << 3 + c = o << 1 + r = s - c o = 8 if o < 8 else o elif (R is None) and (C is None): ## !o s? r = _r @@ -838,35 +843,35 @@ along with this program. If not, see . s = (r + c) if S is None else s elif R is None: ## !c o? s? s = _s if S is None else s - r = s - c; - o = (8 if c == 8 else (c << 1)) if O is None else o; + r = s - c + o = (8 if c == 8 else (c << 1)) if O is None else o elif C is None: ## !r o? s? s = _s if S is None else s - c = s - r; - o = (8 if c == 8 else (c << 1)) if O is None else o; + c = s - r + o = (8 if c == 8 else (c << 1)) if O is None else o else: ## !r !c o? s? s = (r + c) if S is None else s o = (8 if c == 8 else (c << 1)) if O is None else o + printerr('Bitrate: %d' % r) + printerr('Capacity: %d' % c) + printerr('Word size: %d' % w) + printerr('State size: %d' % s) + printerr('Output size: %d' % o) + printerr('Iterations: %d' % i) + printerr('Squeezes: %d' % j) + + if r > s: - printerr(cmd + ': the bitrate must not be higher than the state size.'); - sys.exit(6); + printerr(cmd + ': the bitrate must not be higher than the state size.') + sys.exit(6) if c > s: - printerr(cmd + ': the capacity must not be higher than the state size.'); - sys.exit(6); + printerr(cmd + ': the capacity must not be higher than the state size.') + sys.exit(6) if r + c != s: - printerr(cmd + ': the sum of the bitrate and the capacity must equal the state size.'); - sys.exit(6); - - - printerr('Bitrate: ' % r) - printerr('Capacity: ' % c) - printerr('Word size: ' % w) - printerr('State size: ' % s) - printerr('Output size: ' % o) - printerr('Iterations: ' % i) - printerr('Squeezes: ' % j) + printerr(cmd + ': the sum of the bitrate and the capacity must equal the state size.') + sys.exit(6) if len(files) == 0: @@ -908,7 +913,7 @@ along with this program. If not, see . a = ((a & 15) + (0 if a <= '9' else 9)) << 4 b = (b & 15) + (0 if b <= '9' else 0) chunk[_] = a | b - SHA3.update(chunk, n) + SHA3.update(bytes(chunk), n) bs = SHA3.digest(j == 1) if j > 2: SHA3.fastSqueeze(j - 2) @@ -919,7 +924,7 @@ along with this program. If not, see . else: bs = stdin if multi == 0: - for _ in range(n - 1): + for _ in range(i - 1): SHA3.initialise(r, c, o) bs = SHA3.digest(bs, j == 1) if j > 2: -- cgit v1.2.3-70-g09d2