[#2] Add methods sign and verify
Signed-off-by: ilyas585 <niyazov2023@gmail.com>
This commit is contained in:
parent
4433fc425a
commit
a51ef880fe
9 changed files with 225 additions and 15 deletions
50
frostfs_api/cryptography/verifier.py
Normal file
50
frostfs_api/cryptography/verifier.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import ecdsa
|
||||
from hashlib import sha256, sha512
|
||||
|
||||
|
||||
class Verifier:
|
||||
def verify_rfc6979(self, public_key: bytes, message: bytes, signature: bytes) -> bool:
|
||||
"""
|
||||
Verify a signature using the public key.
|
||||
|
||||
:param public_key: Public key in byte format.
|
||||
:param message: Signature verification message in byte format.
|
||||
:param signature: Signature in byte format.
|
||||
:return: True if the signature is correct, otherwise False.
|
||||
:raises: ValueError: If the public_key key is incorrect.
|
||||
"""
|
||||
if len(public_key) == 0 or public_key is None:
|
||||
raise ValueError(f"Incorrect public key: {public_key}")
|
||||
|
||||
if message is None or signature is None:
|
||||
return False
|
||||
|
||||
vk = ecdsa.VerifyingKey.from_string(public_key, curve=ecdsa.NIST256p, hashfunc=sha256)
|
||||
|
||||
try:
|
||||
return vk.verify(signature, message)
|
||||
except ecdsa.BadSignatureError:
|
||||
return False
|
||||
|
||||
def verify(self, public_key: bytes, message: bytes, signature: bytes) -> bool:
|
||||
"""
|
||||
Verify a signature using the public key.
|
||||
|
||||
:param public_key: Public key in byte format.
|
||||
:param message: Signature verification message in byte format.
|
||||
:param signature: Signature in byte format.
|
||||
:return: True if the signature is correct, otherwise False.
|
||||
:raises: ValueError: If the public_key key is incorrect.
|
||||
"""
|
||||
if len(public_key) == 0 or public_key is None:
|
||||
raise ValueError(f"Incorrect public key: {public_key}")
|
||||
|
||||
if message is None or signature is None:
|
||||
return False
|
||||
|
||||
vk = ecdsa.VerifyingKey.from_string(public_key, curve=ecdsa.NIST256p, hashfunc=sha512)
|
||||
|
||||
try:
|
||||
return vk.verify(signature, message)
|
||||
except ecdsa.BadSignatureError:
|
||||
return False
|
Loading…
Add table
Add a link
Reference in a new issue