Rebex
Products Downloads Buy Support Contact
Show / Hide Table of Contents

Argon2.HashPassword Method

Namespace: Rebex.Security.Cryptography
Assembly: Rebex.Security.dll (version 7.0.9147)

HashPassword(ArraySegment<Byte>, ArraySegment<Byte>, Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(ArraySegment<byte> password, ArraySegment<byte> salt, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
ArraySegment<Byte> password

The ArraySegment<T> that contains a password to hash. Parameter 'P' from Argon2 specification.

ArraySegment<Byte> salt

The ArraySegment<T> that contains the salt. Must be at least 8 bytes long. Parameter 'S' from Argon2 specification. When the salt is empty, a random (16 bytes long) salt will be generated.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(ArraySegment<byte> password, ArraySegment<byte> salt)
{
    // Convert password to a byte array segment.
    var passwordBytes = new ArraySegment<byte>(Encoding.UTF8.GetBytes(password));

    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    string encodedHash = Argon2.HashPassword(passwordBytes, salt, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure:
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentException

The salt length is invalid.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, ArraySegment<Byte>)

HashPassword(String, ArraySegment<Byte>, Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(string password, ArraySegment<byte> salt, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
String password

The string that contains a password to hash. Parameter 'P' from Argon2 specification. The password is converted to a byte array (using the UTF-8 character encoding) before the hashing operation. When the password is null, it will be treated as an empty string.

ArraySegment<Byte> salt

The ArraySegment<T> that contains the salt. Must be at least 8 bytes long. Parameter 'S' from Argon2 specification. When the salt is empty, a random (16 bytes long) salt will be generated.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(string password, ArraySegment<byte> salt)
{
    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    string encodedHash = Argon2.HashPassword(password, salt, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure:
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentException

The salt length is invalid.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, String)

HashPassword(ArraySegment<Byte>, Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The method generates a random (16 bytes long) salt (parameter 'S' from Argon2 specification) before hashing. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(ArraySegment<byte> password, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
ArraySegment<Byte> password

The ArraySegment<T> that contains a password to hash. Parameter 'P' from Argon2 specification.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(ArraySegment<byte> password)
{
    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    // This method generates a random (16 bytes long) salt value.
    string encodedHash = Argon2.HashPassword(password, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure.
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, ArraySegment<Byte>)

HashPassword(String, Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The method generates a random (16 bytes long) salt (parameter 'S' from Argon2 specification) before hashing. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(string password, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
String password

The string that contains a password to hash. Parameter 'P' from Argon2 specification. The password is converted to a byte array (using the UTF-8 character encoding) before the hashing operation. When the password is null, it will be treated as an empty string.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(string password)
{
    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    // This method generates a random (16 bytes long) salt value.
    string encodedHash = Argon2.HashPassword(password, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure.
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, String)

HashPassword(Byte[], Byte[], Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(byte[] password, byte[] salt, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
Byte[] password

The byte array that contains a password to hash. Parameter 'P' from Argon2 specification. When the password is null, it will be treated as an empty array.

Byte[] salt

The byte array that contains the salt. Must be at least 8 bytes long. Parameter 'S' from Argon2 specification. When the salt is null or empty, a random (16 bytes long) salt will be generated.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(string password, byte[] salt)
{
    // Convert password to a byte array.
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    string encodedHash = Argon2.HashPassword(passwordBytes, salt, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure:
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentException

The salt length is invalid.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, Byte[])

HashPassword(String, Byte[], Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(string password, byte[] salt, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
String password

The string that contains a password to hash. Parameter 'P' from Argon2 specification. The password is converted to a byte array (using the UTF-8 character encoding) before the hashing operation. When the password is null, it will be treated as an empty string.

Byte[] salt

The byte array that contains the salt. Must be at least 8 bytes long. Parameter 'S' from Argon2 specification. When the salt is null or empty, a random (16 bytes long) salt will be generated.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(string password, byte[] salt)
{
    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    string encodedHash = Argon2.HashPassword(password, salt, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure:
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentException

The salt length is invalid.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, String)

HashPassword(Byte[], Int32, Argon2Configuration)

Computes a hash of the specified password and returns it in encoded form. Generates a random (16 bytes long) salt (parameter 'S' from Argon2 specification) before hashing. The format of the encoded hash is described in the remarks section of Argon2 class documentation.

Declaration
public static string HashPassword(byte[] password, int hashLength, Argon2Configuration configuration)
Parameters
Type Name Description
Byte[] password

The byte array that contains a password to hash. Parameter 'P' from Argon2 specification. When the password is null, it will be treated as an empty string.

Int32 hashLength

The required hash length. Must be at least 4 bytes long. Parameter 'T' from Argon2 specification.

Argon2Configuration configuration

Argon2 configuration.

Returns
Type Description
String

A string that contains an encoded hash of the password.

Examples
public string HashPassword(string password)
{
    // Convert password to a byte array.
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

    // Use Argon2 configuration suitable for your environment.
    var configuration = new Argon2Configuration(argon2Type: Argon2Type.Argon2id,
        numberOfLanes: 4, numberOfIterations: 10, memoryCost: 65536);

    // The size of the raw hash to compute. Must be at least 4 bytes long.
    // Please note that the encoded hash (in string form) will be longer than the raw hash.
    int hashLength = 32;

    // Compute the hash of the password.
    // This method generates a random 16-bytes long salt value.
    string encodedHash = Argon2.HashPassword(passwordBytes, hashLength, configuration);

    // Return the computed hash. Encoded hash has the following structure:
    // $argon2id$v=19$m=65536,t=10,p=4$MTIzNDU2Nzg$GVfTf0x89BTwcW7HhQMYRcgPwOzswaw6UUBWDBXP0kc
    return encodedHash;
}
Exceptions
Type Condition
ArgumentNullException

The configuration is null.

ArgumentOutOfRangeException

The hashLength is invalid.

See Also
VerifyPassword(String, String)
In This Article
  • HashPassword(ArraySegment<Byte>, ArraySegment<Byte>, Int32, Argon2Configuration)
  • HashPassword(String, ArraySegment<Byte>, Int32, Argon2Configuration)
  • HashPassword(ArraySegment<Byte>, Int32, Argon2Configuration)
  • HashPassword(String, Int32, Argon2Configuration)
  • HashPassword(Byte[], Byte[], Int32, Argon2Configuration)
  • HashPassword(String, Byte[], Int32, Argon2Configuration)
  • HashPassword(Byte[], Int32, Argon2Configuration)
© REBEX ČR s.r.o. Back to top
Privacy policy
Manage cookies