29 lines
955 B
Python
29 lines
955 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
import hashlib
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
from cryptography.hazmat.primitives import hashes
|
|
from cryptography.hazmat.primitives.asymmetric import ec
|
|
from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
|
|
|
def sign_file(private_key_path, input_path):
|
|
# Generate SHA512 signature of a file using EC private key
|
|
print('Signing packages')
|
|
with open(private_key_path, 'rb') as f:
|
|
priv_key = load_pem_private_key(f.read(), None, default_backend())
|
|
with open(input_path, 'rb') as f:
|
|
data = f.read()
|
|
return priv_key.sign(data, ec.ECDSA(hashes.SHA512()))
|
|
|
|
def hash_file(file_path):
|
|
# Calculate SHA512 hash of a file
|
|
sha512 = hashlib.sha512()
|
|
with open(file_path, 'rb') as f:
|
|
while True:
|
|
data = f.read(65536)
|
|
if not data:
|
|
break
|
|
sha512.update(data)
|
|
return sha512.hexdigest()
|