Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b67b14a

Browse files
committed
Minor cleanup of DeriveBytes based on review feedback.
Inlined property calls from the constructor so that Initialize is called only once, at the end (gets rid of comments explaining why it isn't called). Removed some unnecessary "return;" statements. Changed "String" to "string".
1 parent ee26901 commit b67b14a

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/System.Security.Cryptography.DeriveBytes/src/System/Security/Cryptography/DeriveBytes.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ namespace System.Security.Cryptography
55
{
66
public abstract class DeriveBytes : IDisposable
77
{
8-
protected DeriveBytes()
9-
{
10-
}
11-
128
public abstract byte[] GetBytes(int cb);
139
public abstract void Reset();
1410

@@ -20,7 +16,6 @@ public void Dispose()
2016

2117
protected virtual void Dispose(bool disposing)
2218
{
23-
return;
2419
}
2520
}
2621
}

src/System.Security.Cryptography.DeriveBytes/src/System/Security/Cryptography/Rfc2898DeriveBytes.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,53 @@ public class Rfc2898DeriveBytes : DeriveBytes
1212
{
1313
public Rfc2898DeriveBytes(byte[] password, byte[] salt, int iterations)
1414
{
15-
Salt = salt;
16-
IterationCount = iterations;
15+
if (salt == null)
16+
throw new ArgumentNullException("salt");
17+
if (salt.Length < MinimumSaltSize)
18+
throw new ArgumentException(SR.Cryptography_PasswordDerivedBytes_FewBytesSalt, "salt");
19+
if (iterations <= 0)
20+
throw new ArgumentOutOfRangeException("iterations", SR.ArgumentOutOfRange_NeedPosNum);
1721
if (password == null)
1822
throw new NullReferenceException(); // This "should" be ArgumentNullException but for compat, we throw NullReferenceException.
23+
24+
_salt = salt.CloneByteArray();
25+
_iterations = (uint)iterations;
1926
_password = password.CloneByteArray();
2027
_hmacSha1 = new HMACSHA1(_password);
2128

22-
// We "should" call Initialize() here but we've already called it twice indirectly through setting the Salt and IterationCount properties.
23-
return;
29+
Initialize();
2430
}
2531

26-
public Rfc2898DeriveBytes(String password, byte[] salt)
32+
public Rfc2898DeriveBytes(string password, byte[] salt)
2733
: this(password, salt, 1000)
2834
{
2935
}
3036

31-
public Rfc2898DeriveBytes(String password, byte[] salt, int iterations)
37+
public Rfc2898DeriveBytes(string password, byte[] salt, int iterations)
3238
: this(new UTF8Encoding(false).GetBytes(password), salt, iterations)
3339
{
3440
}
3541

36-
public Rfc2898DeriveBytes(String password, int saltSize)
42+
public Rfc2898DeriveBytes(string password, int saltSize)
3743
: this(password, saltSize, 1000)
3844
{
3945
}
4046

41-
public Rfc2898DeriveBytes(String password, int saltSize, int iterations)
47+
public Rfc2898DeriveBytes(string password, int saltSize, int iterations)
4248
{
4349
if (saltSize < 0)
4450
throw new ArgumentOutOfRangeException("saltSize", SR.ArgumentOutOfRange_NeedNonNegNum);
51+
if (saltSize < MinimumSaltSize)
52+
throw new ArgumentException(SR.Cryptography_PasswordDerivedBytes_FewBytesSalt, "saltSize");
53+
if (iterations <= 0)
54+
throw new ArgumentOutOfRangeException("iterations", SR.ArgumentOutOfRange_NeedPosNum);
4555

46-
Salt = Helpers.GenerateRandom(saltSize);
47-
IterationCount = iterations;
56+
_salt = Helpers.GenerateRandom(saltSize);
57+
_iterations = (uint)iterations;
4858
_password = new UTF8Encoding(false).GetBytes(password);
4959
_hmacSha1 = new HMACSHA1(_password);
5060

51-
// We "should" call Initialize() here but we've already called it twice indirectly through setting the Salt and IterationCount properties.
52-
return;
61+
Initialize();
5362
}
5463

5564
public int IterationCount
@@ -79,7 +88,7 @@ public byte[] Salt
7988
{
8089
if (value == null)
8190
throw new ArgumentNullException("value");
82-
if (value.Length < 8)
91+
if (value.Length < MinimumSaltSize)
8392
throw new ArgumentException(SR.Cryptography_PasswordDerivedBytes_FewBytesSalt);
8493
_salt = value.CloneByteArray();
8594
Initialize();
@@ -204,5 +213,6 @@ private byte[] Func()
204213
private int _endIndex;
205214

206215
private const int BlockSize = 20;
216+
private const int MinimumSaltSize = 8;
207217
}
208218
}

0 commit comments

Comments
 (0)