Table of Contents

Method GetFileHashAsync

Namespace
SshSharp.Sftp
Assembly
SshSharp.dll

GetFileHashAsync(string, IReadOnlyList<string>?, long, long?, CancellationToken)

Asks the server for a hash of path, or of the range from offset of length bytes, which it computes where the file is (check-file): to check a transfer without reading the file back. Compare it with a hash of the local copy.

public Task<SftpFileHash> GetFileHashAsync(string path, IReadOnlyList<string>? algorithms = null, long offset = 0, long? length = null, CancellationToken cancellationToken = default)

Parameters

path string

The remote file.

algorithms IReadOnlyList<string>

The hash algorithms to accept, in order of preference, by their check-file names (sha256, sha512, ...); null for SHA-2 only.

offset long

Where the range starts.

length long?

The range's length; null to the end of the file.

cancellationToken CancellationToken

Cancels the request.

Returns

Task<SftpFileHash>

The algorithm the server used and the hash.

Remarks

The server picks the first of algorithms it supports. By default they are SHA-2 only (sha256, sha512, sha384, sha224). md5, sha1 and crc32 are used only when named: they are kept for servers that offer nothing better, and catch accidental damage (a truncated or corrupted file), not a deliberate change. A server with only md5-hash is used when md5 is named.

The hash is what the server says it is: it shows that the server holds the bytes it was sent, as far as the server can be trusted to compute it. It does not protect against a server that lies.

ProFTPD's mod_sftp (1.3.9) hashes a range on to the end of the file unless length is a multiple of its read size (four file system blocks, 16 KiB on most file systems), so ask it for the whole file, or for a multiple of 64 KiB.

Exceptions

NotSupportedException

The server announces no file hash extension, or none of algorithms among those it lists.

SftpException

The server refused: the file does not exist, or the server cannot hash with any of algorithms.

ArgumentException

algorithms is empty or has a name that is not an algorithm name.