> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/framework/core/common/checksum.md).

# 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](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/ChecksumTest.cs#L71) (excerpt; see source for context).

{% code title="ChecksumTest.cs" %}

```csharp
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);
}
```

{% endcode %}

`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](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/ChecksumTest.cs#L111) (excerpt; see source for context).

{% code title="ChecksumTest.cs" %}

```csharp
public void Verify()
{
	var checksum = Checksum.Compute("SHA3-512", _data);
	Assert.False(checksum.IsEmpty);
	Assert.False(checksum.Value.IsEmpty);

	var data = new byte[_data.Length];
	Array.Copy(_data, data, data.Length);

	Assert.True(checksum.Verify(data));
	Assert.True(checksum.Verify(_data));
	Assert.False(checksum.Verify(Randomizer.Generate(512)));
	Assert.False(checksum.Verify(Randomizer.Generate(data.Length)));
}
```

{% endcode %}

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](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/ChecksumTest.cs#L95) (excerpt; see source for context).

{% code title="ChecksumTest.cs" %}

```csharp
public void Parse()
{
	var checksum = Checksum.Compute("SHA512", _data);
	Assert.False(checksum.IsEmpty);
	Assert.False(checksum.Value.IsEmpty);
	Assert.Equal("SHA512", checksum.Name, true);
	Assert.Equal(SHA512.HashSizeInBytes, checksum.Value.Length);
	Assert.NotEmpty(checksum.ToString());
	Assert.StartsWith("SHA512:", checksum.ToString());

	var result = Checksum.Parse(checksum.ToString());
	Assert.False(result.IsEmpty);
	Assert.Equal(checksum, result);
}
```

{% endcode %}

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

## Related Resources

* [Checksum.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/src/Common/Checksum.cs)
* [ChecksumTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/ChecksumTest.cs)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/framework/core/common/checksum.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
