This is an old revision of the document!

Verify checksum of a remote file against a local file over SFTP/FTP protocol

If you do not have your favorite language, use PowerShell.

You can use the script (e.g. verify.ps1) from WinSCP GUI as a local custom command:

powershell.exe -File C:\path\verify.ps1 -sessionUrl !S -localPath "!^!" -remotePath "!/!" -pause

Advertisement

Make sure you check Use remote files option.

The script uses features available in the latest beta release only.

Note that calculation of remote file checksum is supported with SFTP and FTP protocols only, subject to support of respective protocol extension.

param (
    [Parameter(Mandatory)]
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
    [Parameter(Mandatory)]
    $localPath,
    [Parameter(Mandatory)]
    $remotePath,
    [Switch]
    $pause = $False
)
 
try
{
    Write-Host $localPath
 
    # Calculate local file checksum
    $sha1 = [System.Security.Cryptography.SHA1]::Create()
    $localStream = [System.IO.File]::OpenRead($localPath)
    $localChecksum = [BitConverter]::ToString($sha1.ComputeHash($localStream))
 
    Write-Host $localChecksum
    
    # Load WinSCP .NET assembly
    Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.ParseUrl($sessionUrl);
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
        
        Write-Host $remotePath
 
        # Calculate remote file checksum
        $remoteChecksum = [BitConverter]::ToString($session.CalculateFileChecksum("sha-1", $remotePath))
        Write-Host $remoteChecksum
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    # Compare cheksums
    if ($localChecksum -eq $remoteChecksum)
    {
        Write-Host "Match"
        $result = 0
    }
    else
    {
        Write-Host "Does NOT match"
        $result = 1
    }
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    $result = 1
}
 
# Pause use -pause switch was used
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}
 
exit $result

Advertisement

Last modified: by martin