Search code examples
c#androidsftpmaui

Why can't Renci.SshNet create a hash algorithm from 'SHA512' for a .NET MAUI Android release?


I have an application in .NET MAUI that connects to a server via sftp and downloads a file. When debugging, everything works as expected. But as soon as I release the app for Android (target V.13, min V.9), I get the following stacktrace when connecting to the server:

Could not create HashAlgorithm from `SHA512`. Arg_ParamName_Name, hashAlgorithmName    at Renci.SshNet.Security.Cryptography.RsaDigitalSignature..ctor(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName)    at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)    at Renci.SshNet.PrivateKeyFile..ctor(Stream privateKey, String passPhrase)

This is the code I am using to connect to the server:

public void Connect()
{
    MemoryStream keyStream = new(Encoding.ASCII.GetBytes(privateKey));

    keyFile = new PrivateKeyFile(keyStream, privateKeyPassword);

    PrivateKeyConnectionInfo keyConnectionInfo = new(host, port, username, keyFile);

    client = new SftpClient(keyConnectionInfo);

    client.Connect();
}

Solution

  • As suggested by Jason, it was a linker issue. To solve this, I disabled trimming and aot for the release.

    Project Properties -> Android -> Options -> Enable AOT compilation & Enable trimming