What is the Argon2 function?
Argon2 is a cryptographic hashing algorithm specifically used to hash passwords. It provides better protection against password cracking than other hashing algorithms like Bcrypt, Scrypt, and PBKDF2.
The Argon2 function takes in the password and outputs the hash of the specified length.
Variants
The argon2 function has three variants.
-
Argon2iprovides strong GPU resistance but has potential side-channel attacks. -
Argon2dProvides strong side-channel attack resistance. -
Argoin2idis the safest variant because it is a combination of the above two variants.
Parameters
memory_cost: the amount of memory in KB to be used.password: password string to be hashed.salt_len: length of random salt to be generated for each password.parallelism: Number of parallel threads.time_cost: Number of iterations over memory.hash_len: Desired length of the output.type: Argon2 variant to be used.
Code
import argon2argon2Hasher = argon2.PasswordHasher(time_cost=2,memory_cost=64,parallelism=1,hash_len=16,salt_len=16,type = argon2.low_level.Type(2))password = "@Educative123"hash = argon2Hasher.hash(password)print("Hashed password: ", hash)
Explanation
In the code above:
- Line 1: we import the python argon2 module.
- Line 3: we create an instance of the
PasswordHasherclass with custom parameters. - Line 9: we specify a variant of the
argon2function using theTypefunction of anotherargon2class namedlow_level. Type IDs forargon2d,argon2iandargon2idare 0, 1, and 2 respectively. - Line 12: we declare the password string that we want to hash.
- Line 14: we call the
hashfunction with the password string.
Note: Output hash changes every time you press the
Runbutton. This is because randomly generated salt changes every time the hash function is called.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved