1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import hashlib |
---|
4 | import sha256 |
---|
5 | from binascii import a2b_hex, b2a_hex |
---|
6 | |
---|
7 | class Comp4P(object): |
---|
8 | def __init__(self, h0 = None, h1 = None, text = ""): |
---|
9 | if h0 is None: |
---|
10 | self.h0 = sha256.SHA256 |
---|
11 | elif callable(h0): |
---|
12 | self.h0 = h0 |
---|
13 | else: |
---|
14 | self.h0 = lambda d='':h0.new(d) |
---|
15 | |
---|
16 | if h1 is None: |
---|
17 | self.h1 = sha256.SHA256 |
---|
18 | elif callable(h1): |
---|
19 | self.h1 = h1 |
---|
20 | else: |
---|
21 | self.h1 = lambda d='':h1.new(d) |
---|
22 | |
---|
23 | blen1 = len(self.h0().digest()) |
---|
24 | blen2 = len(self.h1().digest()) |
---|
25 | #need to preocess the different length here |
---|
26 | if blen1 > blen2 : |
---|
27 | self.blen = blen2 |
---|
28 | elif blen1 < blen2: |
---|
29 | self.blen = blen1 |
---|
30 | else: |
---|
31 | self.blen = blen1 |
---|
32 | |
---|
33 | # self.ltemp = text |
---|
34 | # self.rtemp = text |
---|
35 | |
---|
36 | self.hlins = self.h0() |
---|
37 | self.hrins = self.h1() |
---|
38 | |
---|
39 | if text != "" : |
---|
40 | self.hlins.update(text) |
---|
41 | self.hrins.update(text) |
---|
42 | |
---|
43 | def setText(self, text): |
---|
44 | # self.ltemp = text |
---|
45 | # self.rtemp = text |
---|
46 | self.hlins.update(text) |
---|
47 | self.hrins.update(text) |
---|
48 | |
---|
49 | def update(self, m): |
---|
50 | self.hlins.update(m) |
---|
51 | self.hrins.update(m) |
---|
52 | |
---|
53 | def digest(self): |
---|
54 | self.left = self.hlins.digest() |
---|
55 | self.right = self.hrins.digest() |
---|
56 | |
---|
57 | self.feistelround(self.f1) |
---|
58 | # print 'in comp', b2a_hex(self.left + self.right),'\n' |
---|
59 | self.feistelround(self.f2) |
---|
60 | # print 'in comp', b2a_hex(self.left + self.right),'\n' |
---|
61 | self.feistelround(self.f3) |
---|
62 | # print 'in comp', b2a_hex(self.left + self.right),'\n' |
---|
63 | |
---|
64 | self.result = self.right + self.left |
---|
65 | return self.result |
---|
66 | |
---|
67 | def hexdigest(self): |
---|
68 | return b2a_hex(self.digest()) |
---|
69 | |
---|
70 | def feistelround(self, f): |
---|
71 | l = self.left |
---|
72 | self.left = self.right |
---|
73 | xor1 = f(self.right) |
---|
74 | self.right = "".join(chr(ord(xor1[i]) ^ ord(l[i])) for i in xrange(self.blen)) |
---|
75 | |
---|
76 | def f1(self, m): |
---|
77 | return m |
---|
78 | |
---|
79 | def f2(self, m): |
---|
80 | msg = chr(0x02) + m |
---|
81 | a1 = self.hash0(msg) |
---|
82 | a2 = self.hash1(msg) |
---|
83 | return "".join(chr(ord(a1[i]) ^ ord(a2[i])) for i in xrange(self.blen)) |
---|
84 | |
---|
85 | def f3(self, m): |
---|
86 | msg = chr(0x03) + m |
---|
87 | a1 = self.hash0(msg) |
---|
88 | a2 = self.hash1(msg) |
---|
89 | return "".join(chr(ord(a1[i]) ^ ord(a2[i])) for i in xrange(self.blen)) |
---|
90 | |
---|
91 | def hash0(self, m): |
---|
92 | # self.h0().update(m) |
---|
93 | # return self.h0.digest() |
---|
94 | return self.h0(m).digest() |
---|
95 | |
---|
96 | def hash1(self, m): |
---|
97 | # self.h1().update(m) |
---|
98 | # return self.h1.digest() |
---|
99 | return self.h1(m).digest() |
---|
100 | |
---|
101 | def new( h0 = None, h1 = None): |
---|
102 | return Comp4P(h0, h1) |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|