Skip to content
Snippets Groups Projects
Commit fc7fa83b authored by Martin Mareš's avatar Martin Mareš
Browse files

mo.tokens: Obecný parametrizovaný podpis a hash

parent 245e6be8
No related branches found
No related tags found
1 merge request!86Registrace
# Podepsané tokeny
import hashlib
import hmac
import urllib.parse
from typing import Optional, List
......@@ -8,9 +9,7 @@ import mo.config as config
def _sign_token(token: str, use: str) -> str:
key = '-'.join(('sign-token', use, config.SECRET_KEY))
mac = hmac.HMAC(key.encode('us-ascii'), token.encode('us-ascii'), 'sha256')
return mac.hexdigest()[:16]
return sign(token, 'token-' + use)
def sign_token(fields: List[str], use: str) -> str:
......@@ -27,3 +26,18 @@ def verify_token(token: str, use: str) -> Optional[List[str]]:
if _sign_token(':'.join(enc_fields), use) != sign:
return None
return [urllib.parse.unquote(f) for f in enc_fields]
def sign(msg: str, use: str) -> str:
"""Podpis parametrizovaný tajným klíčem a účelem."""
key = use + '#' + config.SECRET_KEY
mac = hmac.HMAC(key.encode('us-ascii'), msg.encode('us-ascii'), 'sha256')
return mac.hexdigest()[:16]
def hash(msg: str, use: str) -> str:
"""Hešovací funkce parametrizovaná tajným klíčem a účelem."""
m = '#'.join((use, config.SECRET_KEY, msg)).encode('us-ascii')
return hashlib.sha256(m).hexdigest()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment