Class SHA256
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Hash
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Computes the SHA-256 hash for the input data.
public sealed class SHA256 : Sha2HashAlgorithm<uint>, ICryptoTransform, IDisposable, IResettable
- Inheritance
-
SHA256
- Implements
- Inherited Members
Examples
using CryptoHives.Foundation.Security.Cryptography.Hash;
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
using var sha256 = SHA256.Create();
byte[] hash = sha256.ComputeHash(data);
Remarks
This is a fully managed implementation of SHA-256 that does not rely on OS or hardware cryptographic APIs, ensuring deterministic behavior across all platforms and runtimes.
SHA-256 produces a 256-bit (32-byte) hash value.
Constructors
SHA256()
Initializes a new instance of the SHA256 class.
public SHA256()
Fields
HashSizeBits
The hash size in bits.
public const int HashSizeBits = 256
Field Value
HashSizeBytes
The hash size in bytes.
public const int HashSizeBytes = 32
Field Value
Properties
AlgorithmName
Gets the name of the hash algorithm.
public override string AlgorithmName { get; }
Property Value
BlockSize
Gets the block size in bytes used by the hash algorithm.
public override int BlockSize { get; }
Property Value
BlockSizeBytes
Gets the block size in bytes for this algorithm.
protected override int BlockSizeBytes { get; }
Property Value
OutputSizeBytes
Gets the hash output size in bytes.
protected override int OutputSizeBytes { get; }
Property Value
Methods
Create()
Creates a new instance of the SHA256 class.
public static SHA256 Create()
Returns
- SHA256
A new SHA-256 hash algorithm instance.
HashData(in ReadOnlySequence<byte>)
Computes the SHA-256 hash of source and returns it as a new byte array.
public static byte[] HashData(in ReadOnlySequence<byte> source)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
Returns
- byte[]
A new byte array containing the SHA-256 hash.
HashData(ReadOnlySpan<byte>)
Computes the SHA-256 hash of source and returns it as a new byte array.
public static byte[] HashData(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
Returns
- byte[]
A new byte array containing the SHA-256 hash.
InitializeState()
Initializes the hash state with algorithm-specific initial values.
protected override void InitializeState()
OutputHash(Span<byte>, uint[])
Writes the final hash output from the state to the destination.
protected override void OutputHash(Span<byte> destination, uint[] state)
Parameters
destinationSpan<byte>The destination span for the hash output.
stateuint[]The state array containing the hash values.
PadAndFinalize(Span<byte>, int, long, Span<uint>)
Pads and finalizes the hash computation.
protected override void PadAndFinalize(Span<byte> buffer, int bufferLength, long bytesProcessed, Span<uint> state)
Parameters
bufferSpan<byte>The buffer containing remaining unprocessed bytes.
bufferLengthintThe number of valid bytes in the buffer.
bytesProcessedlongTotal bytes processed before this finalization.
stateSpan<uint>The state array to update.
ProcessBlock(ReadOnlySpan<byte>, Span<uint>)
Processes a single block, updating the state.
protected override void ProcessBlock(ReadOnlySpan<byte> block, Span<uint> state)
Parameters
blockReadOnlySpan<byte>The block data to process.
stateSpan<uint>The state array to update.
TryHashData(in ReadOnlySequence<byte>, Span<byte>, out int)
Computes the SHA-256 hash of source and writes it into destination.
public static bool TryHashData(in ReadOnlySequence<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least HashSizeBytes bytes.
bytesWrittenintWhen this method returns, the number of bytes written into
destination.
Returns
TryHashData(ReadOnlySpan<byte>, Span<byte>, out int)
Computes the SHA-256 hash of source and writes it into destination.
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The input data to hash.
destinationSpan<byte>The buffer to receive the hash value. Must be at least HashSizeBytes bytes.
bytesWrittenintWhen this method returns, the number of bytes written into
destination.