-
Notifications
You must be signed in to change notification settings - Fork 4
/
HMAC.py
executable file
·36 lines (29 loc) · 1.11 KB
/
HMAC.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import hmac
import hashlib
from Peach.transformer import Transformer
class Hmac(Transformer):
"""HMAC as described in RFC 2104."""
_key = None
_digestmod = None
_asHex = None
def __init__(self, key, digestmod=hashlib.md5, asHex=0):
"""
Key is a generator for HMAC key, digestmod is hash to use (md5 or sha)
:param key: HMAC key
:type key: Generator
:param digestmod: which digest to use
:type digestmod: MD5 or SHA hashlib object
:param asHex: 1 is hex, 0 is binary
:type asHex: int
"""
Transformer.__init__(self)
self._key = key
self._digestmod = digestmod
self._asHex = asHex
def realEncode(self, data):
if self._asHex == 0:
return hmac.new(self._key.getValue(), data, self._digestmod).digest()
return hmac.new(self._key.getValue(), data, self._digestmod).hexdigest()