Removed PyCrypto code and translated to cryptography code

This commit is contained in:
Adolfo Gómez García 2021-06-08 19:08:41 +02:00
parent f6ddc7eef1
commit 3b18597d8e

View File

@ -29,10 +29,6 @@
'''
@author: Adolfo Gómez, dkmaster at dkmon dot com
'''
from __future__ import unicode_literals
from base64 import b64decode
import tempfile
import string
import random
@ -41,8 +37,8 @@ import socket
import stat
import sys
import time
import base64
import six
from .log import logger
@ -53,7 +49,7 @@ _execBeforeExit = []
sys_fs_enc = sys.getfilesystemencoding() or 'mbcs'
# Public key for scripts
PUBLIC_KEY = '''-----BEGIN PUBLIC KEY-----
PUBLIC_KEY = b'''-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuNURlGjBpqbglkTTg2lh
dU5qPbg9Q+RofoDDucGfrbY0pjB9ULgWXUetUWDZhFG241tNeKw+aYFTEorK5P+g
ud7h9KfyJ6huhzln9eyDu3k+kjKUIB1PLtA3lZLZnBx7nmrHRody1u5lRaLVplsb
@ -179,13 +175,21 @@ def verifySignature(script, signature):
return: Boolean. True if the signature is valid; False otherwise.
'''
# For signature checking
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import utils, padding
rsakey = RSA.importKey(PUBLIC_KEY)
signer = PKCS1_v1_5.new(rsakey)
digest = SHA256.new(script) # Script is "binary string" here
if signer.verify(digest, b64decode(signature)):
return True
public_key = serialization.load_pem_public_key(data=PUBLIC_KEY)
try:
public_key.verify(
base64.b64decode(signature),
script,
padding.PKCS1v15(),
hashes.SHA256()
)
except Exception: # InvalidSignature
return False
# If no exception, the script was fine...
return True