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.

Demonstration of working of Argon2 function

Variants

The argon2 function has three variants.

  1. Argon2i provides strong GPU resistance but has potential side-channel attacks.

  2. Argon2d Provides strong side-channel attack resistance.

  3. Argoin2id is 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 argon2
argon2Hasher = 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 PasswordHasher class with custom parameters.
  • Line 9: we specify a variant of the argon2 function using the Type function of another argon2 class named low_level. Type IDs for argon2d, argon2i and argon2id are 0, 1, and 2 respectively.
  • Line 12: we declare the password string that we want to hash.
  • Line 14: we call the hash function with the password string.

Note: Output hash changes every time you press the Run button. This is because randomly generated salt changes every time the hash function is called.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved