-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshn.py
44 lines (38 loc) · 1.08 KB
/
shn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from ctypes import CDLL, Structure, byref, c_int, c_uint32
from os.path import dirname
__all__ = 'shn',
shn_c = CDLL(f'{dirname(__file__)}/libshn.so')
SHN_WORDS = 16
WORD = c_uint32
class shn_ctx(Structure):
_fields_ = [
('R', WORD * SHN_WORDS),
('CRC', WORD * SHN_WORDS),
('initR', WORD * SHN_WORDS),
('konst', WORD),
('sbuf', WORD),
('mbuf', WORD),
('nbuf', c_int),
]
class shn:
__slots__ = '_ctx'
def __init__(self, key: bytes):
self._ctx = shn_ctx()
shn_c.shn_key(byref(self._ctx), key, len(key))
def nonce(self, nonce: bytes):
shn_c.shn_nonce(byref(self._ctx), nonce, len(nonce))
def stream(self, buf: bytes):
shn_c.shn_stream(byref(self._ctx), buf, len(buf))
return buf
def maconly(self, buf: bytes):
shn_c.shn_stream(byref(self._ctx), buf, len(buf))
def encrypt(self, buf: bytes):
shn_c.shn_encrypt(byref(self._ctx), buf, len(buf))
return buf
def decrypt(self, buf: bytes):
shn_c.shn_decrypt(byref(self._ctx), buf, len(buf))
return buf
def finish(self, len: int):
buf = bytes(len)
shn_c.shn_finish(byref(self._ctx), buf, len)
return buf