Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA-512 validation on Python

Tags:

python

regex

hash

How to check whether the sha-512 hash is valid? Wrote the following code

 def is_pwd(passwd):
    result = re.findall(r'[a-f0-9]{128}', passwd)
    if result:
        print "Valid"
    else:
        print "NOT a Valid"

But when I pass the hash: 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2''''!@#

Return - VALID

Thx.

like image 411
ZanMax Avatar asked Jan 24 '26 05:01

ZanMax


1 Answers

If you definitely want to use regex, you should use re.match instead:

def is_pwd(passwd):
    result = None
    try: result = re.match(r'^\w{128}$', passwd).group(0)
    except: pass
    return result is not None

This method returns True for a valid password and False for an invalid one.

Breakdown:

  1. ^ - Checks from the start of the string
  2. \w{128} - Checks for exactly 128 occurrences of an alphanumeric character
  3. $ - Confirms the end of the string (i.e. if you had more than 128 alphanums, it would return False)
like image 65
sshashank124 Avatar answered Jan 26 '26 18:01

sshashank124



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!