Skip to content

Instantly share code, notes, and snippets.

@pessom
Forked from xen/check_hash.py
Created February 24, 2018 12:29
Show Gist options
  • Save pessom/afe1bec46f7bf6931d53d9d56ccce627 to your computer and use it in GitHub Desktop.
Save pessom/afe1bec46f7bf6931d53d9d56ccce627 to your computer and use it in GitHub Desktop.
telegram site auth
# implementation of Telegram site authorization checking algorithm
# for more information https://core.telegram.org/widgets/login#checking-authorization
import collections
import hmac
import hashlib
def check_string(d, token):
secret = hashlib.sha256()
secret.update(token.encode('utf-8'))
sorted_params = collections.OrderedDict(sorted(d.items()))
param_hash = sorted_params.pop('hash')
msg = "\n".join(["{}={}".format(k, v) for k, v in sorted_params.items()])
if param_hash == hmac.new(secret.digest(), msg.encode('utf-8'), digestmod=hashlib.sha256).hexdigest():
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment