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
streamStreamThe stream to hash.
algorithmHashAlgorithmThe hash algorithm to use.
cancellationTokenCancellationTokenCancellation 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
streamis 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
filePathstringThe path to the file to hash.
algorithmHashAlgorithmThe hash algorithm to use.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<Result<HashedFile>>
A result containing the hash information on success, or an error on failure.
Exceptions
- ArgumentNullException
Thrown when
filePathis null.- ArgumentException
Thrown when
filePathis empty or whitespace.- OperationCanceledException
Thrown when the operation is cancelled.