Skip to content

Commit 0770be4

Browse files
committed
use extension members for CryptoAbstractions
1 parent e4fb0c9 commit 0770be4

File tree

42 files changed

+207
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+207
-160
lines changed

src/Renci.SshNet/Abstractions/CryptoAbstraction.cs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,5 @@ internal static class CryptoAbstraction
1010
private static readonly RandomNumberGenerator Randomizer = RandomNumberGenerator.Create();
1111

1212
internal static readonly SecureRandom SecureRandom = new SecureRandom(new CryptoApiRandomGenerator(Randomizer));
13-
14-
/// <summary>
15-
/// Generates a <see cref="byte"/> array of the specified length, and fills it with a
16-
/// cryptographically strong random sequence of values.
17-
/// </summary>
18-
/// <param name="length">The length of the array generate.</param>
19-
public static byte[] GenerateRandom(int length)
20-
{
21-
var random = new byte[length];
22-
Randomizer.GetBytes(random);
23-
return random;
24-
}
25-
26-
public static byte[] HashMD5(byte[] source)
27-
{
28-
#if NET
29-
return MD5.HashData(source);
30-
#else
31-
using (var md5 = MD5.Create())
32-
{
33-
return md5.ComputeHash(source);
34-
}
35-
#endif
36-
}
37-
38-
public static byte[] HashSHA1(byte[] source)
39-
{
40-
#if NET
41-
return SHA1.HashData(source);
42-
#else
43-
using (var sha1 = SHA1.Create())
44-
{
45-
return sha1.ComputeHash(source);
46-
}
47-
#endif
48-
}
49-
50-
public static byte[] HashSHA256(byte[] source)
51-
{
52-
#if NET
53-
return SHA256.HashData(source);
54-
#else
55-
using (var sha256 = SHA256.Create())
56-
{
57-
return sha256.ComputeHash(source);
58-
}
59-
#endif
60-
}
61-
62-
public static byte[] HashSHA384(byte[] source)
63-
{
64-
#if NET
65-
return SHA384.HashData(source);
66-
#else
67-
using (var sha384 = SHA384.Create())
68-
{
69-
return sha384.ComputeHash(source);
70-
}
71-
#endif
72-
}
73-
74-
public static byte[] HashSHA512(byte[] source)
75-
{
76-
#if NET
77-
return SHA512.HashData(source);
78-
#else
79-
using (var sha512 = SHA512.Create())
80-
{
81-
return sha512.ComputeHash(source);
82-
}
83-
#endif
84-
}
8513
}
8614
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#nullable enable
2+
namespace System.Security.Cryptography
3+
{
4+
internal static class MD5Extensions
5+
{
6+
extension(MD5)
7+
{
8+
#if !NET
9+
public static byte[] HashData(byte[] source)
10+
{
11+
using (var md5 = MD5.Create())
12+
{
13+
return md5.ComputeHash(source);
14+
}
15+
}
16+
#endif
17+
}
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#nullable enable
2+
namespace System.Security.Cryptography
3+
{
4+
internal static class RandomNumberGeneratorExtensions
5+
{
6+
#if !NET
7+
private static readonly RandomNumberGenerator Randomizer = RandomNumberGenerator.Create();
8+
#endif
9+
10+
extension(RandomNumberGenerator)
11+
{
12+
#if !NET
13+
public static byte[] GetBytes(int length)
14+
{
15+
var random = new byte[length];
16+
Randomizer.GetBytes(random);
17+
return random;
18+
}
19+
#endif
20+
}
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace System.Security.Cryptography
2+
{
3+
internal static class SHA1Extensions
4+
{
5+
extension(SHA1)
6+
{
7+
#if !NET
8+
public static byte[] HashData(byte[] source)
9+
{
10+
using (var sha1 = SHA1.Create())
11+
{
12+
return sha1.ComputeHash(source);
13+
}
14+
}
15+
#endif
16+
}
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#nullable enable
2+
namespace System.Security.Cryptography
3+
{
4+
internal static class SHA256Extensions
5+
{
6+
extension(SHA256)
7+
{
8+
#if !NET
9+
public static byte[] HashData(byte[] source)
10+
{
11+
using (var sha256 = SHA256.Create())
12+
{
13+
return sha256.ComputeHash(source);
14+
}
15+
}
16+
#endif
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#nullable enable
2+
namespace System.Security.Cryptography
3+
{
4+
internal static class SHA384Extensions
5+
{
6+
extension(SHA384)
7+
{
8+
#if !NET
9+
public static byte[] HashData(byte[] source)
10+
{
11+
using (var sha384 = SHA384.Create())
12+
{
13+
return sha384.ComputeHash(source);
14+
}
15+
}
16+
#endif
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#nullable enable
2+
namespace System.Security.Cryptography
3+
{
4+
internal static class SHA512Extensions
5+
{
6+
extension(SHA512)
7+
{
8+
#if !NET
9+
public static byte[] HashData(byte[] source)
10+
{
11+
using (var sha512 = SHA512.Create())
12+
{
13+
return sha512.ComputeHash(source);
14+
}
15+
}
16+
#endif
17+
}
18+
}
19+
}

src/Renci.SshNet/Common/HostKeyEventArgs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#nullable enable
22
using System;
3+
using System.Security.Cryptography;
34

4-
using Renci.SshNet.Abstractions;
55
using Renci.SshNet.Security;
66

77
namespace Renci.SshNet.Common
@@ -104,9 +104,9 @@ public HostKeyEventArgs(KeyHostAlgorithm host)
104104
HostKeyName = host.Name;
105105
KeyLength = host.Key.KeyLength;
106106

107-
_lazyFingerPrint = new Lazy<byte[]>(() => CryptoAbstraction.HashMD5(HostKey));
107+
_lazyFingerPrint = new Lazy<byte[]>(() => MD5.HashData(HostKey));
108108

109-
_lazyFingerPrintSHA256 = new Lazy<string>(() => Convert.ToBase64String(CryptoAbstraction.HashSHA256(HostKey)).TrimEnd('='));
109+
_lazyFingerPrintSHA256 = new Lazy<string>(() => Convert.ToBase64String(SHA256.HashData(HostKey)).TrimEnd('='));
110110

111111
_lazyFingerPrintMD5 = new Lazy<string>(() =>
112112
{

src/Renci.SshNet/Messages/Message.cs

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

3-
using Renci.SshNet.Abstractions;
44
using Renci.SshNet.Common;
55
using Renci.SshNet.Compression;
66

@@ -83,7 +83,7 @@ internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor, bool ex
8383
var paddingLength = GetPaddingLength(paddingMultiplier, excludePacketLengthFieldWhenPadding ? packetLength - 4 : packetLength);
8484

8585
// add padding bytes
86-
var paddingBytes = CryptoAbstraction.GenerateRandom(paddingLength);
86+
var paddingBytes = RandomNumberGenerator.GetBytes(paddingLength);
8787
sshDataStream.Write(paddingBytes, 0, paddingLength);
8888

8989
var packetDataLength = GetPacketDataLength(messageLength, paddingLength);
@@ -127,7 +127,7 @@ internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor, bool ex
127127
WriteBytes(sshDataStream);
128128

129129
// add padding bytes
130-
var paddingBytes = CryptoAbstraction.GenerateRandom(paddingLength);
130+
var paddingBytes = RandomNumberGenerator.GetBytes(paddingLength);
131131
sshDataStream.Write(paddingBytes, 0, paddingLength);
132132

133133
return sshDataStream.ToArray();

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.Abstractions;
1+
using System.Security.Cryptography;
22

33
namespace Renci.SshNet.Messages.Transport
44
{
@@ -12,7 +12,7 @@ public class KeyExchangeInitMessage : Message, IKeyExchangedAllowed
1212
/// </summary>
1313
public KeyExchangeInitMessage()
1414
{
15-
Cookie = CryptoAbstraction.GenerateRandom(16);
15+
Cookie = RandomNumberGenerator.GetBytes(16);
1616
}
1717

1818
#region Message Properties

0 commit comments

Comments
 (0)