forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshake.py
More file actions
25 lines (18 loc) · 778 Bytes
/
Copy pathshake.py
File metadata and controls
25 lines (18 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding: UTF-8 -*-
"""Case Codecs - string hashing with SHAKE.
These are codecs for hashing strings, for use with other codecs in encoding chains.
These codecs:
- transform strings from str to str
- transform strings from bytes to bytes
- transform file content from str to bytes (write)
"""
from ..__common__ import *
def shake_hash(i):
def _hash_transform(l):
l = (l or str(i)).lstrip("_-")
def _encode(data, error="strict"):
return getattr(hashlib, "shake_%d" % i)(b(data)).hexdigest(int(l)), len(data)
return _encode
return _hash_transform
add("shake_128", shake_hash(128), pattern=r"^shake[-_]?128(|[-_][1-9]\d*)$", guess=None)
add("shake_256", shake_hash(256), pattern=r"^shake[-_]?256(|[-_][1-9]\d*)$", guess=None)