HashingContext
Provides functionality for computing cryptographic hashes chunk by chunk.
Provides functionality for computing cryptographic hashes chunk by chunk.
The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. Useful for computing hashes of big files (so you don't have to load them all in memory), network streams, and data streams in general (so you don't have to hold buffers).
The HashingContext.HashType enum shows the supported hashing algorithms.
public const int ChunkSize = 1024;
public void HashFile(string path)
{
// Check that file exists.
if (!FileAccess.FileExists(path))
{
return;
}
// Start an SHA-256 context.
var ctx = new HashingContext();
ctx.Start(HashingContext.HashType.Sha256);
// Open the file to hash.
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
// Update the context after reading each chunk.
while (file.GetPosition() < file.GetLength())
{
int remaining = (int)(file.GetLength() - file.GetPosition());
ctx.Update(file.GetBuffer(Mathf.Min(remaining, ChunkSize)));
}
// Get the computed hash.
byte[] res = ctx.Finish();
// Print the result as hex string and array.
ReckfellApi.PrintT(res.HexEncode(), (Variant)res);
}Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
HashingContextConstructor
public HashingContext();Methods4
Finishbyte[]
public byte[] Finish();Closes the current context, and return the computed hash.
byte[]StartError
public Error Start(HashingContext.HashType type);Starts a new hash computation of the given type (e.g. HashingContext.HashType.Sha256 to start computation of an SHA-256).
| Name | Type | Default |
|---|---|---|
type | HashingContext.HashType | — |
ErrorUpdate2 overloads
public Error Update(byte[] chunk);Updates the computation with the given chunk of data.
| Name | Type | Default |
|---|---|---|
chunk | byte[] | — |
Errorpublic Error Update(ReadOnlySpan<byte> chunk);Updates the computation with the given chunk of data.
| Name | Type | Default |
|---|---|---|
chunk | ReadOnlySpan<byte> | — |
ErrorHashingContext.HashType
| Value | Description |
|---|---|
Md5 = 0 | Hashing algorithm: MD5. |
Sha1 = 1 | Hashing algorithm: SHA-1. |
Sha256 = 2 | Hashing algorithm: SHA-256. |