how to let crypt module only work when paramater values are identical to the given ones?

I've been trying to figure out how i can let this code only work when the values match the given ones through an cli on for example, ubuntu. The code works, but it still says the password matches the orginal one when i give another pass/hash than test/npDnXtpN5py4. anyone who can help? regards.

import os, sys
import getopt
import pwd
import crypt


def checkPassword(pswd, cpswd):
    """ Check if `cpwsd` an encrypted version is of `pswd`.
        Return `True` of `False`
    """
    try:
        cpswd = 'npDnXtpN5py4U'
        pswd = 'test'
        cryptedpassword = crypt.crypt(pswd, cpswd)
        return cpswd == cryptedpassword
    except KeyError:
        return 0 # no such pw

if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], 'V', [])
    if len(args) != 2:
        print('Usage: {} [-v] <cpswd> <pswd>'.format(sys.argv[0]))
        sys.exit(0)

    cpswd, pswd = args[0], args[1]
    res = checkPassword(pswd, cpswd)
    if res:
        print("Pass for '{}' is '{}'".format(
            cpswd, pswd))
    else:
        print("Pass for '{}' is not '{}'".format(
            cpswd, pswd))


Read more here: https://stackoverflow.com/questions/65700343/how-to-let-crypt-module-only-work-when-paramater-values-are-identical-to-the-giv

Content Attribution

This content was originally published by Alen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: