Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Renci.SshNet/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public static BigInteger ToBigInteger2(this byte[] data)
return data.ToBigInteger();
}

public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false, bool isBigEndian = false)
{
var data = bigInt.ToByteArray();

if (isUnsigned && data[data.Length - 1] == 0)
{
data = data.Take(data.Length - 1);
}

if (isBigEndian)
{
_ = data.Reverse();
}

return data;
}

/// <summary>
/// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
/// </summary>
Expand Down
19 changes: 0 additions & 19 deletions src/Renci.SshNet/Security/Cryptography/AsymmetricCipher.cs

This file was deleted.

This file was deleted.

158 changes: 0 additions & 158 deletions src/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs

This file was deleted.

52 changes: 15 additions & 37 deletions src/Renci.SshNet/Security/Cryptography/RsaDigitalSignature.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
using System;
#nullable enable
using System;
using System.Security.Cryptography;

using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers;

namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
/// Implements RSA digital signature algorithm.
/// </summary>
public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
public class RsaDigitalSignature : DigitalSignature, IDisposable
{
#if NET462
private readonly HashAlgorithm _hash;
#else
private readonly IncrementalHash _hash;
#endif
private readonly RsaKey _key;
private readonly HashAlgorithmName _hashAlgorithmName;

/// <summary>
/// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class with the SHA-1 hash algorithm.
Expand All @@ -32,36 +27,22 @@ public RsaDigitalSignature(RsaKey rsaKey)
/// <param name="rsaKey">The RSA key.</param>
/// <param name="hashAlgorithmName">The hash algorithm to use in the digital signature.</param>
public RsaDigitalSignature(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName)
: base(ObjectIdentifier.FromHashAlgorithmName(hashAlgorithmName), new RsaCipher(rsaKey))
{
#if NET462
_hash = CryptoConfig.CreateFromName(hashAlgorithmName.Name) as HashAlgorithm
?? throw new ArgumentException($"Could not create {nameof(HashAlgorithm)} from `{hashAlgorithmName}`.", nameof(hashAlgorithmName));
#else
// CryptoConfig.CreateFromName is a somewhat legacy API and is incompatible with trimming.
// Use IncrementalHash instead (which is also more modern and lighter-weight than HashAlgorithm).
_hash = IncrementalHash.CreateHash(hashAlgorithmName);
#endif
_key = rsaKey;
_hashAlgorithmName = hashAlgorithmName;
}

/// <summary>
/// Hashes the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// Hashed data.
/// </returns>
protected override byte[] Hash(byte[] input)
/// <inheritdoc/>
public override bool Verify(byte[] input, byte[] signature)
{
#if NET462
return _hash.ComputeHash(input);
#else
_hash.AppendData(input);
return _hash.GetHashAndReset();
#endif
return _key.RSA.VerifyData(input, signature, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
}

#region IDisposable Members
/// <inheritdoc/>
public override byte[] Sign(byte[] input)
{
return _key.RSA.SignData(input, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand All @@ -78,9 +59,6 @@ public void Dispose()
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
_hash.Dispose();
}

#endregion
}
}
Loading