Class Poly1305Mac
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Mac
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Computes the Poly1305 message authentication code as defined in RFC 8439.
public sealed class Poly1305Mac : IMac, IDisposable
- Inheritance
-
Poly1305Mac
- Implements
- Inherited Members
Examples
byte[] key = new byte[32]; // 32-byte one-time key
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
using var mac = Poly1305Mac.Create(key);
mac.Update(data);
byte[] tag = new byte[16];
mac.Finalize(tag);
Remarks
Poly1305 is a high-speed, one-time authenticator designed by Daniel J. Bernstein. It takes a 32-byte one-time key and a message to produce a 16-byte tag.
Important: The key must be unique for every message. Reusing a key across multiple messages completely compromises authenticity. In practice, the key is typically derived from a session key and nonce (e.g., by ChaCha20-Poly1305).
This class provides a streaming IMac interface for incremental hashing. For stack-only, zero-allocation usage, see CryptoHives.Foundation.Security.Cryptography.Cipher.Poly1305Core.
Constructors
Poly1305Mac(byte[])
Initializes a new instance of the Poly1305Mac class.
public Poly1305Mac(byte[] key)
Parameters
keybyte[]The 32-byte one-time key.
Exceptions
- ArgumentException
The key is not 32 bytes.
Poly1305Mac(ReadOnlySpan<byte>)
Initializes a new instance of the Poly1305Mac class.
public Poly1305Mac(ReadOnlySpan<byte> key)
Parameters
keyReadOnlySpan<byte>The 32-byte one-time key.
Exceptions
- ArgumentException
The key is not 32 bytes.
Fields
KeySizeBytes
The required key size in bytes.
public const int KeySizeBytes = 32
Field Value
TagSizeBytes
The MAC output size in bytes.
public const int TagSizeBytes = 16
Field Value
Properties
AlgorithmName
Gets the name of the MAC algorithm.
public string AlgorithmName { get; }
Property Value
MacSize
Gets the MAC output size in bytes.
public int MacSize { get; }
Property Value
Methods
ComputeHash(byte[])
Computes the Poly1305 tag for the given data in a single operation.
public byte[] ComputeHash(byte[] data)
Parameters
databyte[]The data to authenticate.
Returns
- byte[]
The 16-byte MAC tag.
ComputeHash(ReadOnlySpan<byte>)
Computes the Poly1305 tag for the given data in a single operation.
public byte[] ComputeHash(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The data to authenticate.
Returns
- byte[]
The 16-byte MAC tag.
Create(byte[])
Creates a new instance of the Poly1305Mac class.
public static Poly1305Mac Create(byte[] key)
Parameters
keybyte[]The 32-byte one-time key.
Returns
- Poly1305Mac
A new Poly1305 MAC instance.
Create(ReadOnlySpan<byte>)
Creates a new instance of the Poly1305Mac class.
public static Poly1305Mac Create(ReadOnlySpan<byte> key)
Parameters
keyReadOnlySpan<byte>The 32-byte one-time key.
Returns
- Poly1305Mac
A new Poly1305 MAC instance.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Finalize(Span<byte>)
Computes the final MAC tag and writes it to destination.
public void Finalize(Span<byte> destination)
Parameters
Exceptions
- ArgumentException
destinationis too small.- ObjectDisposedException
Thrown when the instance has been disposed.
Hash(byte[], byte[])
Computes the Poly1305 tag for the specified key and data in a single operation.
public static byte[] Hash(byte[] key, byte[] data)
Parameters
Returns
- byte[]
The 16-byte MAC tag.
Reset()
Resets the MAC to its initial state so it can be reused with the same key.
public void Reset()
Update(ReadOnlySpan<byte>)
Feeds input data into the MAC computation.
public void Update(ReadOnlySpan<byte> input)
Parameters
inputReadOnlySpan<byte>The data to process.
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.