For the complete documentation index, see llms.txt. This page is also available as Markdown.

Checksums

Checksum Calculation, parsing and verification of checksum structures.

Checksum represents the named hash check value, which consists of the algorithm name and byte array. It supports calculations, parsing, validation, string conversion, type conversion, and JSON serialization.

Core Members

member
Description

Name

Hash algorithm name, such as MD5, SHA256, SHA512.

Value

Check value byte array.

IsEmpty

Whether the check value is empty.

Compute / ComputeAsync

Calculate checksum from byte data or stream.

Verify / VerifyAsync

Verify that the data matches the current check value.

Parse / TryParse

Parse check value from Name:Hex text.

This page uses an existing test from the framework ChecksumTest; the shared field _data is 1024 bytes of data generated by the test class, and assertions are used to clarify the expected results. Discussions currently does not use Checksum directly.

Calculate Checksum

Source: framework/Zongsoft.Core/test/Common/ChecksumTest.cs (excerpt; see source for context).

ChecksumTest.cs
public void Compute()
{
	var checksum1 = Checksum.Compute("SHA256", _data);
	Assert.False(checksum1.IsEmpty);
	Assert.False(checksum1.Value.IsEmpty);
	Assert.Equal("SHA256", checksum1.Name, true);
	Assert.Equal(SHA256.HashSizeInBytes, checksum1.Value.Length);
	Assert.NotEmpty(checksum1.ToString());
	Assert.StartsWith("SHA256:", checksum1.ToString());

	var checksum2 = new Checksum(SHA256.HashData(_data));
	Assert.False(checksum2.IsEmpty);
	Assert.False(checksum2.Value.IsEmpty);
	Assert.Equal("SHA256", checksum2.Name, true);
	Assert.Equal(SHA256.HashSizeInBytes, checksum2.Value.Length);
	Assert.NotEmpty(checksum2.ToString());
	Assert.StartsWith("SHA256:", checksum2.ToString());

	Assert.Equal(checksum1.Name, checksum2.Name, true);
	Assert.True(checksum1.Value.Span.SequenceEqual(checksum2.Value.Span));
	Assert.Equal(checksum1, checksum2);
}

Compute will create a hash algorithm based on the name and calculate the corresponding check value.

Validate Data

Source: framework/Zongsoft.Core/test/Common/ChecksumTest.cs (excerpt; see source for context).

Streaming data can use Verify(Stream) or VerifyAsync(Stream).

Parse and Convert

Checksum.ToString() outputs Name:Hex format, and Parse can restore this format.

Source: framework/Zongsoft.Core/test/Common/ChecksumTest.cs (excerpt; see source for context).

Checksum has built-in type converters and JSON converters, so it can be used with Convert and Serializer.Json.

Last updated