Python Secrets compare_digest














































Python Secrets compare_digest



Python Secrets Module : compare_digest


compare_digest function is used to compare two strings in a manner which is cryptographically secure. It return True if strings a and b are equal, otherwise False, in such a way as to reduce the risk of timing attacks. Timing attack is a side-channel attack in which the attacker attempts to compromise a cryptosystem by analyzing the time taken to execute cryptographic algorithms. Hence to prevent this type of attack in production compare_digest function is used, which does not linearly compares two strings but instead compare their characters randomly.

compare_digest function is a part of hmac module from where it is imported into secrets module. This function uses an approach designed to prevent timing analysis by avoiding content-based short circuiting behaviour, making it appropriate for cryptography. a and b must both be of the same type: either str or a bytes-like object. If a and b are of different lengths, or if an error occurs, a timing attack could theoretically reveal information about the types and lengths of a and b%u2014but not their values.


Syntax

secrets.compare_digest(ab)


Parameters

a, b : Strings to be compared.


Sample Program

#importing secrets module
import secrets def printEquality(result):
if(result):
print("Passwords Match")
else:
print("Passwords Donot Match") password1 = "deekshant123"
password2 = "deekshant223"
result = secrets.compare_digest(password1,password2)
printEquality(result) password1 = "deekshant123"
password2 = "gibberisj"
result = secrets.compare_digest(password1,password2)
printEquality(result) password1 = "deekshant123"
password2 = "deekshant123"
result = secrets.compare_digest(password1,password2)
printEquality(result)


Output

Passwords Donot Match

Passwords Donot Match

Passwords Match

----------------------------------------* END *----------------------------------------


More Articles of Deekshant Wadhwa:

Name Views Likes
Python Secrets token_urlsafe 1375 1
Python Bisect Module 527 1
Python Base64 Decodebytes 466 1
Python Copy copy 428 1
Python JSON Module 430 1
Python Copy Module 408 1
Python Base64 Encode 527 1
Python Base64 urlsafe_b64decode 571 1
Python Copy deepcopy 440 1
Python 3.9 New removeprefix() and removesuffix() string methods 952 1
Python 3.9 Dictionary Merge & Update Operators 648 1
Python Base64 Decode 441 1
Python Secrets compare_digest 1732 1
Python Itertools Chain 544 1
Python Itertools Permutations 1242 1
Python Itertools Module 425 1
Python Base64 b85decode 619 1
Python Secrets token_bytes 789 1
Python ModuleNotFoundError: No module named cv2 744 1
Python Base64 standard_b64encode 497 1
Python Itertools Count 688 1
Python Secrets Module 669 1
Python Itertools Groupby 446 1
Python Base64 b32decode 709 1
Python Itertools Filterfalse 677 1
Python Itertools Product 471 1
Python Base64 Encodebytes 497 1
Python Itertools Cycle 474 2
Python Base64 b64decode 459 1
Python Itertools Starmap 600 1
Python Itertools combinations_with_replacement 502 1
Python Base64 urlsafe_b64encode 564 1
Python Itertools Repeat 507 1
Python Base64 standard_b64decode 478 1
Python Base64 b85encode 479 1
Python Itertools Islice 574 1
Python Bisect insort 585 1
Python Itertools Combinations 670 1
Python Secrets token_hex 792 1
Python Itertools Compress 494 1
Python Itertools Tee 527 1
Python Base64 b64encode 423 1
Python Itertools zip_longest 922 1
Python Itertools Accumulate 1451 1
Python Itertools Dropwhile 543 1
Python Secrets Randbits 465 1
Python Base64 Module 432 1
Python Itertools combinations_with_replacement 371 10
Python Secrets Choice 450 1
Python Secrets Randbelow 438 1
Python Itertools Takewhile 498 1
Python Bisect bisect 376 1
Python Base64 b32encode 557 1

Comments