Skip to content

Commit 1d5d58e

Browse files
committed
Rename HashAlgorithmFactory to CryptoAbstraction, and move it to the Renci.SshNet.Abstractions namespace.
1 parent 4d6a868 commit 1d5d58e

File tree

13 files changed

+47
-45
lines changed

13 files changed

+47
-45
lines changed

src/Renci.SshNet.Tests/Classes/Security/Cryptography/HMacTest.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Renci.SshNet.Security.Cryptography;
32
using Renci.SshNet.Tests.Common;
43
using Renci.SshNet.Tests.Properties;
5-
using System.Security.Cryptography;
4+
using Renci.SshNet.Abstractions;
65

76
namespace Renci.SshNet.Tests.Classes.Security.Cryptography
87
{
@@ -19,7 +18,7 @@ public void Test_HMac_MD5_Connection()
1918
{
2019
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
2120
connectionInfo.HmacAlgorithms.Clear();
22-
connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, HashAlgorithmFactory.CreateHMACMD5));
21+
connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, CryptoAbstraction.CreateHMACMD5));
2322

2423
using (var client = new SshClient(connectionInfo))
2524
{
@@ -34,7 +33,7 @@ public void Test_HMac_Sha1_Connection()
3433
{
3534
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
3635
connectionInfo.HmacAlgorithms.Clear();
37-
connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, HashAlgorithmFactory.CreateHMACSHA1));
36+
connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, CryptoAbstraction.CreateHMACSHA1));
3837

3938
using (var client = new SshClient(connectionInfo))
4039
{
@@ -49,7 +48,7 @@ public void Test_HMac_MD5_96_Connection()
4948
{
5049
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
5150
connectionInfo.HmacAlgorithms.Clear();
52-
connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, key => HashAlgorithmFactory.CreateHMACMD5(key, 96)));
51+
connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, key => CryptoAbstraction.CreateHMACMD5(key, 96)));
5352

5453
using (var client = new SshClient(connectionInfo))
5554
{
@@ -64,7 +63,7 @@ public void Test_HMac_Sha1_96_Connection()
6463
{
6564
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
6665
connectionInfo.HmacAlgorithms.Clear();
67-
connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, key => HashAlgorithmFactory.CreateHMACSHA1(key, 96)));
66+
connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, key => CryptoAbstraction.CreateHMACSHA1(key, 96)));
6867

6968
using (var client = new SshClient(connectionInfo))
7069
{
@@ -79,7 +78,7 @@ public void Test_HMac_Sha256_Connection()
7978
{
8079
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
8180
connectionInfo.HmacAlgorithms.Clear();
82-
connectionInfo.HmacAlgorithms.Add("hmac-sha2-256", new HashInfo(32 * 8, HashAlgorithmFactory.CreateHMACSHA256));
81+
connectionInfo.HmacAlgorithms.Add("hmac-sha2-256", new HashInfo(32 * 8, CryptoAbstraction.CreateHMACSHA256));
8382

8483
using (var client = new SshClient(connectionInfo))
8584
{
@@ -94,7 +93,7 @@ public void Test_HMac_Sha256_96_Connection()
9493
{
9594
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
9695
connectionInfo.HmacAlgorithms.Clear();
97-
connectionInfo.HmacAlgorithms.Add("hmac-sha2-256-96", new HashInfo(32 * 8, (key) => HashAlgorithmFactory.CreateHMACSHA256(key, 96)));
96+
connectionInfo.HmacAlgorithms.Add("hmac-sha2-256-96", new HashInfo(32 * 8, (key) => CryptoAbstraction.CreateHMACSHA256(key, 96)));
9897

9998
using (var client = new SshClient(connectionInfo))
10099
{
@@ -109,7 +108,7 @@ public void Test_HMac_RIPEMD160_Connection()
109108
{
110109
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
111110
connectionInfo.HmacAlgorithms.Clear();
112-
connectionInfo.HmacAlgorithms.Add("hmac-ripemd160", new HashInfo(160, HashAlgorithmFactory.CreateHMACRIPEMD160));
111+
connectionInfo.HmacAlgorithms.Add("hmac-ripemd160", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160));
113112

114113
using (var client = new SshClient(connectionInfo))
115114
{
@@ -124,7 +123,7 @@ public void Test_HMac_RIPEMD160_OPENSSH_Connection()
124123
{
125124
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
126125
connectionInfo.HmacAlgorithms.Clear();
127-
connectionInfo.HmacAlgorithms.Add("[email protected]", new HashInfo(160, HashAlgorithmFactory.CreateHMACRIPEMD160));
126+
connectionInfo.HmacAlgorithms.Add("[email protected]", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160));
128127

129128
using (var client = new SshClient(connectionInfo))
130129
{

src/Renci.SshNet/Abstractions/CryptoAbstraction.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using Renci.SshNet.Security.Cryptography;
23

3-
namespace Renci.SshNet.Security.Cryptography
4+
namespace Renci.SshNet.Abstractions
45
{
5-
internal static class HashAlgorithmFactory
6+
internal static class CryptoAbstraction
67
{
78
#if FEATURE_RNG_CREATE || FEATURE_RNG_CSP
89
private static readonly System.Security.Cryptography.RandomNumberGenerator Randomizer = CreateRandomNumberGenerator();

src/Renci.SshNet/Common/BigInteger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
using System.Collections.Generic;
5252
using System.Diagnostics.CodeAnalysis;
5353
using System.Globalization;
54-
using Renci.SshNet.Security.Cryptography;
54+
using Renci.SshNet.Abstractions;
5555

5656
/*
5757
Optimization
@@ -174,7 +174,7 @@ public static BigInteger PositiveMod(BigInteger dividend, BigInteger divisor)
174174
public static BigInteger Random(int bitLength)
175175
{
176176
var bytesArray = new byte[bitLength / 8 + (((bitLength % 8) > 0) ? 1 : 0)];
177-
HashAlgorithmFactory.GenerateRandom(bytesArray);
177+
CryptoAbstraction.GenerateRandom(bytesArray);
178178
bytesArray[bytesArray.Length - 1] = (byte)(bytesArray[bytesArray.Length - 1] & 0x7F); // Ensure not a negative value
179179
return new BigInteger(bytesArray);
180180
}

src/Renci.SshNet/Common/HostKeyEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using Renci.SshNet.Security.Cryptography;
2+
using Renci.SshNet.Abstractions;
33
using Renci.SshNet.Security;
44

55
namespace Renci.SshNet.Common
@@ -54,7 +54,7 @@ public HostKeyEventArgs(KeyHostAlgorithm host)
5454

5555
KeyLength = host.Key.KeyLength;
5656

57-
using (var md5 = HashAlgorithmFactory.CreateMD5())
57+
using (var md5 = CryptoAbstraction.CreateMD5())
5858
{
5959
FingerPrint = md5.ComputeHash(host.Data);
6060
}

src/Renci.SshNet/ConnectionInfo.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using Renci.SshNet.Abstractions;
56
using Renci.SshNet.Security;
67
using Renci.SshNet.Messages.Connection;
78
using Renci.SshNet.Common;
89
using Renci.SshNet.Messages.Authentication;
9-
using Renci.SshNet.Security.Cryptography;
1010
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
1111
using Renci.SshNet.Security.Cryptography.Ciphers;
1212

@@ -332,17 +332,17 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy
332332

333333
HmacAlgorithms = new Dictionary<string, HashInfo>
334334
{
335-
{"hmac-md5", new HashInfo(16*8, HashAlgorithmFactory.CreateHMACMD5)},
336-
{"hmac-md5-96", new HashInfo(16*8, key => HashAlgorithmFactory.CreateHMACMD5(key, 96))},
337-
{"hmac-sha1", new HashInfo(20*8, HashAlgorithmFactory.CreateHMACSHA1)},
338-
{"hmac-sha1-96", new HashInfo(20*8, key => HashAlgorithmFactory.CreateHMACSHA1(key, 96))},
339-
{"hmac-sha2-256", new HashInfo(32*8, HashAlgorithmFactory.CreateHMACSHA256)},
340-
{"hmac-sha2-256-96", new HashInfo(32*8, key => HashAlgorithmFactory.CreateHMACSHA256(key, 96))},
341-
{"hmac-sha2-512", new HashInfo(64 * 8, HashAlgorithmFactory.CreateHMACSHA512)},
342-
{"hmac-sha2-512-96", new HashInfo(64 * 8, key => HashAlgorithmFactory.CreateHMACSHA512(key, 96))},
335+
{"hmac-md5", new HashInfo(16*8, CryptoAbstraction.CreateHMACMD5)},
336+
{"hmac-md5-96", new HashInfo(16*8, key => CryptoAbstraction.CreateHMACMD5(key, 96))},
337+
{"hmac-sha1", new HashInfo(20*8, CryptoAbstraction.CreateHMACSHA1)},
338+
{"hmac-sha1-96", new HashInfo(20*8, key => CryptoAbstraction.CreateHMACSHA1(key, 96))},
339+
{"hmac-sha2-256", new HashInfo(32*8, CryptoAbstraction.CreateHMACSHA256)},
340+
{"hmac-sha2-256-96", new HashInfo(32*8, key => CryptoAbstraction.CreateHMACSHA256(key, 96))},
341+
{"hmac-sha2-512", new HashInfo(64 * 8, CryptoAbstraction.CreateHMACSHA512)},
342+
{"hmac-sha2-512-96", new HashInfo(64 * 8, key => CryptoAbstraction.CreateHMACSHA512(key, 96))},
343343
//{"[email protected]", typeof(HMacSha1)},
344-
{"hmac-ripemd160", new HashInfo(160, HashAlgorithmFactory.CreateHMACRIPEMD160)},
345-
{"[email protected]", new HashInfo(160, HashAlgorithmFactory.CreateHMACRIPEMD160)},
344+
{"hmac-ripemd160", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160)},
345+
{"[email protected]", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160)},
346346
//{"none", typeof(...)},
347347
};
348348

src/Renci.SshNet/Messages/Message.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Globalization;
55
using Renci.SshNet.Abstractions;
66
using Renci.SshNet.Compression;
7-
using Renci.SshNet.Security.Cryptography;
87

98
namespace Renci.SshNet.Messages
109
{
@@ -100,7 +99,7 @@ internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor)
10099

101100
// add padding bytes
102101
var paddingBytes = new byte[paddingLength];
103-
HashAlgorithmFactory.GenerateRandom(paddingBytes);
102+
CryptoAbstraction.GenerateRandom(paddingBytes);
104103
sshDataStream.Write(paddingBytes, 0, paddingLength);
105104

106105
var packetDataLength = GetPacketDataLength(messageLength, paddingLength);
@@ -140,7 +139,7 @@ internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor)
140139

141140
// add padding bytes
142141
var paddingBytes = new byte[paddingLength];
143-
HashAlgorithmFactory.GenerateRandom(paddingBytes);
142+
CryptoAbstraction.GenerateRandom(paddingBytes);
144143
sshDataStream.Write(paddingBytes, 0, paddingLength);
145144
}
146145

src/Renci.SshNet/Messages/Transport/KeyExchangeInitMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Renci.SshNet.Security.Cryptography;
1+
using Renci.SshNet.Abstractions;
22

33
namespace Renci.SshNet.Messages.Transport
44
{
@@ -14,7 +14,7 @@ public class KeyExchangeInitMessage : Message, IKeyExchangedAllowed
1414
public KeyExchangeInitMessage()
1515
{
1616
var cookie = new byte[16];
17-
HashAlgorithmFactory.GenerateRandom(cookie);
17+
CryptoAbstraction.GenerateRandom(cookie);
1818
Cookie = cookie;
1919
}
2020

src/Renci.SshNet/PrivateKeyFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.IO;
44
using System.Text;
55
using System.Text.RegularExpressions;
6+
using Renci.SshNet.Abstractions;
67
using Renci.SshNet.Security;
78
using Renci.SshNet.Common;
89
using System.Globalization;
9-
using Renci.SshNet.Security.Cryptography;
1010
using Renci.SshNet.Security.Cryptography.Ciphers;
1111
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
1212
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
@@ -278,7 +278,7 @@ private static byte[] GetCipherKey(string passphrase, int length)
278278
{
279279
var cipherKey = new List<byte>();
280280

281-
using (var md5 = HashAlgorithmFactory.CreateMD5())
281+
using (var md5 = CryptoAbstraction.CreateMD5())
282282
{
283283
var passwordBytes = Encoding.UTF8.GetBytes(passphrase);
284284

@@ -319,7 +319,7 @@ private static byte[] DecryptKey(CipherInfo cipherInfo, byte[] cipherData, strin
319319

320320
var cipherKey = new List<byte>();
321321

322-
using (var md5 = HashAlgorithmFactory.CreateMD5())
322+
using (var md5 = CryptoAbstraction.CreateMD5())
323323
{
324324
var passwordBytes = Encoding.UTF8.GetBytes(passPhrase);
325325

src/Renci.SshNet/Security/Cryptography/DsaDigitalSignature.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Security.Cryptography;
3+
using Renci.SshNet.Abstractions;
34
using Renci.SshNet.Common;
45

56
namespace Renci.SshNet.Security.Cryptography
@@ -25,7 +26,7 @@ public DsaDigitalSignature(DsaKey key)
2526

2627
_key = key;
2728

28-
_hash = HashAlgorithmFactory.CreateSHA1();
29+
_hash = CryptoAbstraction.CreateSHA1();
2930
}
3031

3132
/// <summary>

src/Renci.SshNet/Security/Cryptography/RsaDigitalSignature.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Security.Cryptography;
3+
using Renci.SshNet.Abstractions;
34
using Renci.SshNet.Common;
45
using Renci.SshNet.Security.Cryptography.Ciphers;
56

@@ -19,7 +20,7 @@ public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
1920
public RsaDigitalSignature(RsaKey rsaKey)
2021
: base(new ObjectIdentifier(1, 3, 14, 3, 2, 26), new RsaCipher(rsaKey))
2122
{
22-
_hash = HashAlgorithmFactory.CreateSHA1();
23+
_hash = CryptoAbstraction.CreateSHA1();
2324
}
2425

2526
/// <summary>

0 commit comments

Comments
 (0)