Commit 69c4e60a authored by Tom Niget's avatar Tom Niget

Use cryptography instead of crypto for sign function because of deprecation

parent 5cb423e3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import calendar, hashlib, hmac, logging, os, struct, subprocess, threading, time import calendar, hashlib, hmac, logging, os, struct, subprocess, threading, time
from OpenSSL import crypto from OpenSSL import crypto
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from . import utils from . import utils
from .version import protocol from .version import protocol
...@@ -93,7 +97,9 @@ class Cert: ...@@ -93,7 +97,9 @@ class Cert:
with open(ca, "rb") as f: with open(ca, "rb") as f:
self.ca = crypto.load_certificate(crypto.FILETYPE_PEM, f.read()) self.ca = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
with open(key, "rb") as f: with open(key, "rb") as f:
self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, f.read()) key_pem = f.read()
self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, key_pem)
self.key_crypto = load_pem_private_key(key_pem, password=None)
if cert: if cert:
with open(cert) as f: with open(cert) as f:
self.cert = self.loadVerify(f.read().encode()) self.cert = self.loadVerify(f.read().encode())
...@@ -155,8 +161,14 @@ class Cert: ...@@ -155,8 +161,14 @@ class Cert:
def verify(self, sign: bytes, data): def verify(self, sign: bytes, data):
crypto.verify(self.ca, sign, data, 'sha512') crypto.verify(self.ca, sign, data, 'sha512')
def sign(self, data) -> bytes: def sign(self, data: bytes) -> bytes:
return crypto.sign(self.key, data, 'sha512') assert isinstance(data, bytes)
#return crypto.sign(self.key, data, 'sha512') DEPRECATED
return self.key_crypto.sign(
data,
padding.PKCS1v15(),
hashes.SHA512()
)
def decrypt(self, data: bytes) -> bytes: def decrypt(self, data: bytes) -> bytes:
p = openssl('rsautl', '-decrypt', '-inkey', self.key_path) p = openssl('rsautl', '-decrypt', '-inkey', self.key_path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment