1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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))
|