-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac.py
More file actions
38 lines (26 loc) · 1.04 KB
/
mac.py
File metadata and controls
38 lines (26 loc) · 1.04 KB
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
38
import convert
import hash
import xor
def keyPrefixMac(message, key, hashFunction):
return hashFunction(key+message)
def verifyKeyPrefix(message, key, mac, hashFunction):
return mac == keyPrefixMac(message, key, hashFunction)
def sha1KeyPrefix(message, key):
return keyPrefixMac(message, key, hash.sha1)
def verifySha1KeyPrefix(message, key, mac):
return verifyKeyPrefix(message, key, mac, hash.sha1)
def md4KeyPrefix(message, key):
return keyPrefixMac(message, key, hash.md4)
def verifyMd4KeyPrefix(message, key, mac):
return verifyKeyPrefix(message, key, mac, hash.md4)
def hMAC(message, key, hashFunction, blockSize):
if len(key) > blockSize:
key = hashFunction(key)
key = key.ljust(blockSize, chr(0))
oKeyPad = xor.xorByteStrings(chr(0x5c)*blockSize, key)
iKeyPad = xor.xorByteStrings(chr(0x36)*blockSize, key)
return hashFunction(oKeyPad+ convert.intToByteString(hashFunction(iKeyPad+message)))
def hMAC_SHA1(message, key):
return hMAC(message, key, hash.sha1, 64)
def HMAC_SHA256(message, key):
return hMAC(message, key, hash.sha256, 64)