File: util.py

package info (click to toggle)
python-authlib 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: python: 26,998; makefile: 53; sh: 14
file content (14 lines) | stat: -rw-r--r-- 418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import hashlib

from authlib.common.encoding import to_bytes
from authlib.common.encoding import urlsafe_b64encode


def create_half_hash(s, alg):
    hash_type = f"sha{alg[2:]}"
    hash_alg = getattr(hashlib, hash_type, None)
    if not hash_alg:
        return None
    data_digest = hash_alg(to_bytes(s)).digest()
    slice_index = int(len(data_digest) / 2)
    return urlsafe_b64encode(data_digest[:slice_index])