Release v4.13441 - midsummer
[RRG-proxmark3.git] / client / pyscripts / parity.py
blob6be9d979d96e0e562f6bfacf207e8b6a841681a3
1 # This code is contributed by
2 # Shubham Singh(SHUBHAMSINGH10)
3 # 2020, modified (@iceman1001)
5 import sys
7 # Python3 program to illustrate Compute the
8 # parity of a number using XOR
9 # Generating the look-up table while pre-processing
10 def P2(n, table):
11 table.extend([n, n ^ 1, n ^ 1, n])
12 def P4(n, table):
13 return (P2(n, table), P2(n ^ 1, table),
14 P2(n ^ 1, table), P2(n, table))
15 def P6(n, table):
16 return (P4(n, table), P4(n ^ 1, table),
17 P4(n ^ 1, table), P4(n, table))
18 def LOOK_UP(table):
19 return (P6(0, table), P6(1, table),
20 P6(1, table), P6(0, table))
22 # LOOK_UP is the macro expansion to generate the table
23 table = [0] * 256
24 LOOK_UP(table)
26 # Function to find the parity
27 def Parity(num) :
28 # Number is considered to be of 32 bits
29 max = 16
31 # Dividing the number o 8-bit
32 # chunks while performing X-OR
33 while (max >= 8):
34 num = num ^ (num >> max)
35 max = max // 2
37 # Masking the number with 0xff (11111111)
38 # to produce valid 8-bit result
39 return table[num & 0xff]
41 def main():
42 if(len(sys.argv) < 2):
43 print("""
44 \t{0} - Calculate parity of a given number
46 Usage: {0} <2,10,16> <number>
48 \t Specify type as in 2 Bin, 10 Decimal, 16 Hex, and number in that particular format
49 \t number can only be 32bit long.
51 Example:
53 \t{0} 10 1234
55 Should produce the output:
57 \tOdd parity\n""".format(sys.argv[0]))
58 return 0
61 numtype= int(sys.argv[1], 10)
62 print("numtype: {0}".format(numtype))
63 input= int(sys.argv[2], numtype)
64 print("num: {0} 0x{0:X}".format(input))
66 #num = "001111100010100011101010111101011110"
67 # Result is 1 for odd parity
68 # 0 for even parity
69 # result = Parity( int(input, numtype) )
70 result = Parity(input)
71 print("Odd parity") if result else print("Even parity")
74 if __name__ == "__main__":
75 main()