Spotter-VM/build/usr/lib/python3.6/lxcbuild/crypto.py

29 lines
955 B
Python
Raw Normal View History

2019-09-20 10:13:41 +02:00
# -*- 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
2019-09-20 15:43:01 +02:00
def sign_file(private_key_path, input_path):
2019-09-20 10:13:41 +02:00
# Generate SHA512 signature of a file using EC private key
print('Signing packages')
2019-09-20 15:43:01 +02:00
with open(private_key_path, 'rb') as f:
2019-09-20 10:13:41 +02:00
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()