Skip to content

Commit

Permalink
Merge pull request #6 from 2Toad/jp-4
Browse files Browse the repository at this point in the history
Fixes #4: Add Reset method to Settings
  • Loading branch information
JasonPierce committed Feb 1, 2017
2 parents f27cd60 + c95b524 commit 7c16046
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ The *Settings* object is a collection of mutable defaults used throughout the li
// with Rijndael.Encrypt being just one of them. After making this change,
// any future calls to Rijndael.Encrypt will make use of this new value
Settings.HashIterations = 25000;

// To reset all the settings to their default values
Settings.Reset();
```

## Appendix
Expand Down
31 changes: 21 additions & 10 deletions Rijndael256.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,31 @@
using System.Linq;
using Xunit;

// WARNING: HERE BE DRAGONS!
// We must disable xUnit's asyncrhonous test excution because the Settings
// object's default values are required by the other unit tests
[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace Rijndael256.Tests
{
public class SettingsTests
{
const string Plaintext = "A secret phrase to test hashing.";
const string Salt = "0A9FDB669FA44FF1BEC484A1BE6B6E2A";

[Fact]
public void Reset()
{
var defaultHashIterations = Settings.HashIterations;

Settings.HashIterations = 777;
Assert.Equal(Settings.HashIterations, 777);
Assert.NotEqual(Settings.HashIterations, defaultHashIterations);

Settings.Reset();
Assert.Equal(Settings.HashIterations, defaultHashIterations);
}

[Fact]
public void HashIterations()
{
Expand All @@ -30,16 +48,9 @@ public void HashIterations()
var hashChanged = Hash.Pbkdf2(Plaintext, Salt, Settings.HashIterations);
Assert.False(hashChanged.SequenceEqual(proofDefault));

RestoreDefaults();
}

/// <summary>
/// Settings is global, so we need to restore defaults before the other
/// unit tests, which depend on said defaults, are run.
/// </summary>
private void RestoreDefaults()
{
Settings.HashIterations = 10000;
// Settings is global, so we need to restore defaults before the other
// unit tests, which depend on said defaults, are run
Settings.Reset();
}
}
}
18 changes: 17 additions & 1 deletion Rijndael256/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,26 @@ namespace Rijndael256
/// </summary>
public static class Settings
{
static Settings()
{
// Set defaults during initialization
Reset();
}

/// <summary>
/// Resets all the settings to their default values
/// </summary>
public static void Reset()
{
HashIterations = _hashIterations;
}

/// <summary>
/// The number of iterations used to derive hashes.
/// Default is 10000.
/// </summary>
public static int HashIterations = 10000;
public static int HashIterations;

private const int _hashIterations = 10000;
}
}

0 comments on commit 7c16046

Please sign in to comment.