· 8 years ago · Jan 29, 2018, 06:56 PM
1hmac_blocksize=16
2def strToNum(inp):
3 """Takes a sequence of bytes and makes a number"""
4 out=0
5 for i in inp:
6 out=out<<8
7 out^=ord(i)
8 return out
9def numToStr(inp):
10 """Take a number and make a sequence of bytes in a string"""
11 out=""
12 while inp!=0:
13 out=chr(inp & 255)+out
14 inp=inp>>8
15 return out
16def cueh_hash_1(inp):
17 """ CUEH Hash Function v 1.0
18 Returns 16 bit hash of any string input or stringable input
19 """
20 inp=str(inp) #Make sure we have a string
21 if len(inp)%2!=0: inp+=" " #Pad it if we need to
22 val=0 #Our accumulator
23 for pos in range(0,len(inp),2): #Now in twos...
24 i=inp[pos]
25 j=inp[pos+1]
26 # print "\tEncoding",i,j
27 val^=ord(i) #XOR first char onto lowest 8 bits
28 val^=(ord(j)<<8) #and second char onto highest 8 bits
29 # print "\t\t",val
30 # print "\t",val
31 return val
32
33def cueh_hmac_1(key, message):
34 """Outputs a hash-based digest of the message and secret key combo"""
35 key=str(key)
36 message=str(message)
37 if len(key)>hmac_blocksize/8:
38 key=numToStr(cueh_hash_1(key)) #Keys are shortened to blocksize
39 while len(key)<hmac_blocksize/8:
40 key+=" " #Keys are padded with spaces if they're too short
41 # print "0x%x"%cueh_hash_1(key+message)
42 return cueh_hash_1(key+message)
43
44if __name__=="__main__":
45 print "-"*20
46 #Examples of flipping between numbers and strings of bytes
47 #Just makes it easier to have "password" style keys
48 print "%x"%strToNum("ABC")
49 print numToStr((65<<16) + (66<<8) + 67)
50
51 #Now to see it in practice
52
53 #secretKey="cCAA" #This is known by both parties
54 #authedMessage="This is a test of the emergency broadcast system."
55
56 #out=cueh_hmac_1(secretKey,authedMessage)
57
58 #Now we have the special verification code that can be used to
59 #prove we were the aithor of the message. Anyone else who knows
60 #the secret can do the same and compare the values
61 #print "%d|%s"%(out, authedMessage)
62
63 s = "AAA"
64 r = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
65 i = 0
66
67 for x2 in range(52):
68 for x3 in range(52):
69 for x4 in range(52):
70 secretKey = r[x2] + r[x3] + r[x4]
71 authedMessage="power up gigamatrix server"
72 out=cueh_hmac_1(secretKey,authedMessage)
73 if(out == 25193):
74 authedMessage="install toaster updates"
75 out=cueh_hmac_1(secretKey,authedMessage)
76 if(out == 21084):
77 authedMessage="realign singularity polishing buffers"
78 out=cueh_hmac_1(secretKey,authedMessage)
79 if(out == 25136):
80 authedMessage="enhance undulation"
81 out=cueh_hmac_1(secretKey,authedMessage)
82 if(out == 14382):
83 authedMessage="detatch porpoise"
84 out=cueh_hmac_1(secretKey,authedMessage)
85 if(out == 23900):
86 print "This is secret key: " + secretKey