Class Argon2Memory
Manages memory for a Argon2.
Implements
Inherited Members
Namespace: Isopoh.Cryptography.Argon2
Assembly: Isopoh.Cryptography.Argon2.dll
Syntax
public sealed class Argon2Memory : IDisposable
Constructors
| Edit this page View SourceArgon2Memory(Argon2Config, Argon2MemoryPolicy, LockMemoryPolicy?)
Initializes a new instance of the Argon2Memory class.
Declaration
public Argon2Memory(Argon2Config config, Argon2MemoryPolicy shrinkMemoryPolicy, LockMemoryPolicy? lockMemory)
Parameters
| Type | Name | Description |
|---|---|---|
| Argon2Config | config | The initial configuration to use. Can be updated later with a call to Reset(Argon2Config) (or Reset(string, Argon2Password)). |
| Argon2MemoryPolicy | shrinkMemoryPolicy | Indicates whether to shrink memory to fit upon calling Reset(string, Argon2Password) or Reset(Argon2Config) with an Argon2Config that requires less memory. Note: the memory will always grow as needed. |
| LockMemoryPolicy? | lockMemory | The lock memory policy to use. Null to not secure memory at all. |
Fields
| Edit this page View SourceArgon2WorkingBufferSize
Size in bytes required for the Argon2 working buffer.
Declaration
public const int Argon2WorkingBufferSize = 2560
Field Value
| Type | Description |
|---|---|
| int |
CsharpMaxBlocksPerArray
C# has a limit of 0X7FEFFFFF elements per array (0x7FFFFFC7 per byte array). The blocks are 1024 bytes long, the elements are 8 bytes (ulong). This gives 0X7FEFFFFF / 128 blocks per C# array.
Declaration
public const int CsharpMaxBlocksPerArray = 16769023
Field Value
| Type | Description |
|---|---|
| int |
Properties
| Edit this page View SourceArgon2WorkingBuffer
Gets the byte-based working buffer sized for Argon2 to use internally.
Declaration
public Memory<byte> Argon2WorkingBuffer { get; }
Property Value
| Type | Description |
|---|---|
| Memory<byte> |
Remarks
This gets used and overwritten on every hash.
AssociatedData
Gets the associated data used in the password hash.
Declaration
public Span<byte> AssociatedData { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
BlockCount
Gets the memory block count for the latest Argon2Config that this Argon2Memory supports.
Declaration
public int BlockCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).
Blocks
Gets the Blocks for this Argon2Memory.
Declaration
public Blocks Blocks { get; }
Property Value
| Type | Description |
|---|---|
| Blocks |
Remarks
This gets used and overwritten on every hash.
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).ClearPassword
Gets a value indicating whether to clear the password as soon as it is no longer needed.
Declaration
public bool ClearPassword { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
If true and the configuration has a password, the configuration cannot be used more than once without resetting the password (unless you want an all zero password).
ClearSecret
Gets a value indicating whether to clear the secret as soon as it is no longer needed.
Declaration
public bool ClearSecret { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
If true and the configuration has a secret, the configuration cannot be used more than once without resetting the secret (unless you want an all zero secret).
FillMemoryBlocksWorkingBuffer
Gets the ulong-based working buffer sized for Argon2's internal memory-filling operation.
Declaration
public Memory<ulong> FillMemoryBlocksWorkingBuffer { get; }
Property Value
| Type | Description |
|---|---|
| Memory<ulong> |
Remarks
This gets used and overwritten on every hash.
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).FillMemoryBlocksWorkingBufferLength
Gets the count of ulong values in the FillMemoryBlocksWorkingBuffer.
Declaration
public int FillMemoryBlocksWorkingBufferLength { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).
Hash
Gets the span associated with the final Argon2 hash value.
Declaration
public Span<byte> Hash { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
Remarks
This gets used and overwritten on every hash.
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).HashLength
Gets the hash length to output. Minimum of 4. Default 32.
Declaration
public int HashLength { get; }
Property Value
| Type | Description |
|---|---|
| int |
InUse
Gets a value indicating whether the memory is currently in use.
Declaration
public bool InUse { get; }
Property Value
| Type | Description |
|---|---|
| bool |
KeyIdentifier
Gets the key identifier used in the password hash.
Declaration
public Span<byte> KeyIdentifier { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
LaneBlockCount
Gets the number of memory blocks per lane. SegmentBlockCount * SyncPointCount.
Declaration
public int LaneBlockCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).
Lanes
Gets the lanes used in the password hash. Minimum of 1. Defaults to 4.
Declaration
public int Lanes { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
This describes the maximum parallelism that can be achieved. Each "lane" can be processed individually in its own thread. Setting Threads to a value greater than one when there is more than one lane will allow the use of multiple cores to speed up hashing.
LockMemory
Gets the lock memory policy. Null to not secure arrays at all.
Declaration
public LockMemoryPolicy? LockMemory { get; }
Property Value
| Type | Description |
|---|---|
| LockMemoryPolicy? |
MemoryCost
Gets the memory cost used in the password hash. Minimum of 1. Defaults to 65536.
Declaration
public int MemoryCost { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
This translates into a target count of memory blocks to use for hashing. A memory block is 1024 bytes so the default 65536 is for a 64MB hash.
If this value is less than 2*SyncPointCount*Lanes, than 2*SyncPointCount*Lanes will be used.
If this value is not a multiple of SyncPointCount*Lanes, then it is rounded down to a multiple of SyncPointCount*Lanes.
Password
Gets the password to hash.
Declaration
public Span<byte> Password { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
Salt
Gets the salt used in the password hash. If non-null, must be at least 8 bytes.
Declaration
public Span<byte> Salt { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
Secret
Gets the secret used in the password hash.
Declaration
public Span<byte> Secret { get; }
Property Value
| Type | Description |
|---|---|
| Span<byte> |
SegmentBlockCount
Gets the number of memory blocks per segment. This value gets derived from the memory cost. The memory cost value is a request for that number of blocks. If that request is less than (2 * SyncPointCount) times the number of lanes requested, it is first bumped up to that amount. Then, it may be reduced to fit on a SyncPointCount times the number of lanes requested boundary.
Declaration
public int SegmentBlockCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Can change on every call to Reset(string, Argon2Password) or Reset(Argon2Config).
ShrinkMemoryPolicy
Gets or sets the policy that determines whether to shrink memory when resetting with a new Argon2Config.
Declaration
public Argon2MemoryPolicy ShrinkMemoryPolicy { get; set; }
Property Value
| Type | Description |
|---|---|
| Argon2MemoryPolicy |
Threads
Gets the threads used in the password hash. Minimum of 1. Defaults to 1.
Declaration
public int Threads { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
This value makes no difference in the result. A value greater than one causes that many threads to get spawned to do the work on top of the main thread that orchestrates which thread does what work.
Lanes defines the maximum parallelism that can be achieved. Setting Threads to a value greater than Lanes will not result in more than Lanes threads running.
TimeCost
Gets the time cost used in the password hash. Minimum of 1. Defaults to 3.
Declaration
public int TimeCost { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
This is the number of iterations to perform. There are attacks on the Argon2Version.Sixteen with less than three iterations (if I'm reading the paper correctly). So, use a value greater than 3 here if you are not using Argon2Version.Nineteen.
Type
Gets the Argon2 type. Default to hybrid.
Declaration
public Argon2Type Type { get; }
Property Value
| Type | Description |
|---|---|
| Argon2Type |
Version
Gets the Argon2 version used in the password hash. Defaults to Argon2Version.Nineteen (0x13).
Declaration
public Argon2Version Version { get; }
Property Value
| Type | Description |
|---|---|
| Argon2Version |
Methods
| Edit this page View SourceDispose()
Dispose.
Declaration
public void Dispose()
EndUse()
Mark this memory as free to use again.
Declaration
public void EndUse()
RequiredBlockCounts(Argon2Config)
Gets the required block count for the given Argon2Config.
Declaration
public static (int SegmentBlockCount, int LaneBlockCount, ulong BlockCount) RequiredBlockCounts(Argon2Config config)
Parameters
| Type | Name | Description |
|---|---|---|
| Argon2Config | config | Used to determine the required block count. |
Returns
| Type | Description |
|---|---|
| (int SegmentBlockCount, int LaneBlockCount, ulong BlockCount) | The required segment, lane, and total block count for the given Argon2Config. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Reset(Argon2Config)
Reset this Argon2Memory to what the given Argon2Config requires.
Declaration
public void Reset(Argon2Config config)
Parameters
| Type | Name | Description |
|---|---|---|
| Argon2Config | config | The configuration used to determine memory required. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | If the memory could not be allocated. Usually because of operating system enforced limits on securing the allocated arrays. |
Reset(string, Argon2Password)
Reset this Argon2Memory to what the given Argon2Config requires.
Declaration
public void Reset(string hash, Argon2Password password)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hash | The configuration used to determine memory required. |
| Argon2Password | password | The password to use after reset (or Argon2Password.Keep for no change). |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | If the memory could not be allocated. Usually because of operating system enforced limits on securing the allocated arrays. |
StartUse()
Start the use of this memory. Argon2Memory should not be used by more than one hash operation as a time.
Declaration
public void StartUse()