diff options
author | Camil Staps | 2016-02-12 14:50:35 +0100 |
---|---|---|
committer | Camil Staps | 2016-02-12 14:50:35 +0100 |
commit | a9af9b24b7fbfb31110123b125fb7fc916cff24f (patch) | |
tree | e8a49c9bedbcf43a912dfd37348da79123bf814a /Assignment 3/nthkey.py |
Put everything on git
Diffstat (limited to 'Assignment 3/nthkey.py')
-rwxr-xr-x | Assignment 3/nthkey.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Assignment 3/nthkey.py b/Assignment 3/nthkey.py new file mode 100755 index 0000000..40cf0cf --- /dev/null +++ b/Assignment 3/nthkey.py @@ -0,0 +1,37 @@ +#!/usr/bin/python + +# Copyright (c) 2015 Camil Staps <info@camilstaps.nl> + +import binascii + +# From http://stackoverflow.com/a/843846/1544337 +# 1 for odd parity, 0 for even parity +# Was used only in a previous version of nthByte +# def parity(b): +# c = 0 +# while b != 0: +# c += 1 +# b &= b - 1 +# return c % 2 + +# Find the nth possibility for a byte with odd parity +oddBytes = [1,2,4,7,8,11,13,14,16,19,21,22,25,26,28,31,32,35,37,38,41,42,44,47,49,50,52,55,56,59,61,62,64,67,69,70,73,74,76,79,81,82,84,87,88,91,93,94,97,98,100,103,104,107,109,110,112,115,117,118,121,122,124,127,128,131,133,134,137,138,140,143,145,146,148,151,152,155,157,158,161,162,164,167,168,171,173,174,176,179,181,182,185,186,188,191,193,194,196,199,200,203,205,206,208,211,213,214,217,218,220,223,224,227,229,230,233,234,236,239,241,242,244,247,248,251,253,254] +def nthByte(n): + return oddBytes[n] + # c = -1 # This is the old version. The new version uses a faster lookup table. + # b = 0 + # while c != n: + # b += 1 + # if parity(b) == 1: + # c += 1 + # return b + +# Find the nth key in which all bytes have odd parity +def nthKey(n): + key = '' + for b in range(7,-1,-1): + key += chr(nthByte((n >> b*7) & 0x7f)) + return key + +# Example +print binascii.b2a_hex(nthKey(765637))
\ No newline at end of file |