_JSON_INDENT = 2
-_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
+_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE)
def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
fn = _DumpJson
txt = _RE_EOLSP.sub("", fn(data))
- if not txt.endswith('\n'):
- txt += '\n'
+ if not txt.endswith("\n"):
+ txt += "\n"
return txt
"""
txt = DumpJson(data, indent=False)
if salt is None:
- salt = ''
+ salt = ""
signed_dict = {
- 'msg': txt,
- 'salt': salt,
- }
+ "msg": txt,
+ "salt": salt,
+ }
+
if key_selector:
signed_dict["key_selector"] = key_selector
- message = salt + key_selector + txt
else:
- message = salt + txt
- signed_dict["hmac"] = utils.Sha1Hmac(key, message)
+ key_selector = ""
+
+ signed_dict["hmac"] = utils.Sha1Hmac(key, txt, salt=salt + key_selector)
return DumpJson(signed_dict, indent=False)
"""
signed_dict = LoadJson(txt)
if not isinstance(signed_dict, dict):
- raise errors.SignatureError('Invalid external message')
+ raise errors.SignatureError("Invalid external message")
try:
- msg = signed_dict['msg']
- salt = signed_dict['salt']
- hmac_sign = signed_dict['hmac']
+ msg = signed_dict["msg"]
+ salt = signed_dict["salt"]
+ hmac_sign = signed_dict["hmac"]
except KeyError:
- raise errors.SignatureError('Invalid external message')
+ raise errors.SignatureError("Invalid external message")
if callable(key):
# pylint: disable-msg=E1103
key_selector = ""
hmac_key = key
- if not utils.VerifySha1Hmac(hmac_key, salt + key_selector + msg, hmac_sign):
- raise errors.SignatureError('Invalid Signature')
+ if not utils.VerifySha1Hmac(hmac_key, msg, hmac_sign,
+ salt=salt + key_selector):
+ raise errors.SignatureError("Invalid Signature")
return LoadJson(msg), salt