-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.py
More file actions
executable file
·32 lines (24 loc) · 898 Bytes
/
Copy pathcrypto.py
File metadata and controls
executable file
·32 lines (24 loc) · 898 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
25
26
27
28
29
30
31
32
#!/usr/bin/python3
from cryptography.hazmat.backends.openssl.backend import backend as c_openssl
from cryptography.hazmat.primitives import hashes
from binascii import hexlify
import hashlib
def binstr_to_ascii(binstr):
return hexlify(binstr).decode()
def c_openssl_sha256_hexdigest(data):
digest = hashes.Hash(hashes.SHA256(), backend=c_openssl)
digest.update(data.encode())
bin_str = digest.finalize()
ascii_str = binstr_to_ascii(bin_str)
return ascii_str
def python_hashlib_sha256_hexdigest(data):
m = hashlib.sha256()
m.update(data.encode())
bin_str = m.digest()
ascii_str = binstr_to_ascii(bin_str)
return ascii_str
test_str = "cat"
print("c_openssl_sha256_hexdigest(", test_str, ") = ",
c_openssl_sha256_hexdigest(test_str))
print("python_hashlib_sha256_hexdigest(", test_str, ") = ",
python_hashlib_sha256_hexdigest(test_str))