Table of Contents

Class FileHashingService

Namespace
CivitaiSharp.Tools.Hashing
Assembly
CivitaiSharp.Tools.dll

Default implementation of IFileHashingService.

public sealed class FileHashingService : IFileHashingService
Inheritance
FileHashingService
Implements
Inherited Members

Remarks

This service supports the following hash algorithms:

  • Sha256 - SHA-256 (64 hex characters)
  • Sha512 - SHA-512 (128 hex characters)
  • Blake3 - BLAKE3 (64 hex characters)
  • Crc32 - CRC32 (8 hex characters)

All hash values are returned as lowercase hexadecimal strings.

Methods

ComputeHashAsync(Stream, HashAlgorithm, CancellationToken)

Computes the hash of the data available in the provided stream.

public Task<Result<HashedFile>> ComputeHashAsync(Stream stream, HashAlgorithm algorithm, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to hash.

algorithm HashAlgorithm

The hash algorithm to use.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<HashedFile>>

A result containing the hash information on success, or an error on failure.

Remarks

The stream will be read to completion. Callers should assume the stream position is advanced to the end after this call.

For non-seekable streams, the reported file size in the result will be -1.

Exceptions

ArgumentNullException

Thrown when stream is null.

OperationCanceledException

Thrown when the operation is cancelled.

ComputeHashAsync(string, HashAlgorithm, CancellationToken)

Computes the hash of a file at the given path using the specified algorithm.

public Task<Result<HashedFile>> ComputeHashAsync(string filePath, HashAlgorithm algorithm, CancellationToken cancellationToken = default)

Parameters

filePath string

The path to the file to hash.

algorithm HashAlgorithm

The hash algorithm to use.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<HashedFile>>

A result containing the hash information on success, or an error on failure.

Exceptions

ArgumentNullException

Thrown when filePath is null.

ArgumentException

Thrown when filePath is empty or whitespace.

OperationCanceledException

Thrown when the operation is cancelled.