Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 35 additions & 1 deletion JoinFS.Tests/ProgramCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Security.Cryptography;

namespace JoinFS.Tests
{
Expand All @@ -9,6 +10,39 @@ namespace JoinFS.Tests
/// </summary>
public static class ProgramCode
{
/// <summary>
/// Cryptographically secure random number generator seeded with a key
/// </summary>
private class SeededCryptoRandom
{
private readonly byte[] seed;
private int counter;

public SeededCryptoRandom(int key)
{
seed = BitConverter.GetBytes(key);
counter = 0;
}

/// <summary>
/// Generate a deterministic but cryptographically derived random number in range [0, maxValue)
/// </summary>
public int Next(int maxValue)
{
if (maxValue <= 0)
throw new ArgumentOutOfRangeException(nameof(maxValue));

// Use HMACSHA256 to derive random value from seed and counter
using (var hmac = new HMACSHA256(seed))
{
byte[] counterBytes = BitConverter.GetBytes(counter++);
byte[] hash = hmac.ComputeHash(counterBytes);
uint randomValue = BitConverter.ToUInt32(hash, 0);
return (int)(randomValue % (uint)maxValue);
}
}
}

public static string? Code(string? s, bool bToCode, int nKey)
{
if (s == null)
Expand Down Expand Up @@ -70,7 +104,7 @@ public static class ProgramCode
int nInc = 0;
int k;
const int nPasses = 11;
Random rnd = new Random(nKey);
SeededCryptoRandom rnd = new SeededCryptoRandom(nKey);
for (int j = 0; j < nPasses; ++j)
{
k = (bToCode) ? j : nPasses - j - 1;
Expand Down
36 changes: 35 additions & 1 deletion JoinFS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Security.Cryptography;
using JoinFS.Properties;

namespace JoinFS
Expand Down Expand Up @@ -1935,6 +1936,39 @@ public void MonitorAircraftDetails()

static class Program
{
/// <summary>
/// Cryptographically secure random number generator seeded with a key
/// </summary>
private class SeededCryptoRandom
{
private readonly byte[] seed;
private int counter;

public SeededCryptoRandom(int key)
{
seed = BitConverter.GetBytes(key);
counter = 0;
}

/// <summary>
/// Generate a deterministic but cryptographically derived random number in range [0, maxValue)
/// </summary>
public int Next(int maxValue)
{
if (maxValue <= 0)
throw new ArgumentOutOfRangeException(nameof(maxValue));

// Use HMACSHA256 to derive random value from seed and counter
using (var hmac = new HMACSHA256(seed))
{
byte[] counterBytes = BitConverter.GetBytes(counter++);
byte[] hash = hmac.ComputeHash(counterBytes);
uint randomValue = BitConverter.ToUInt32(hash, 0);
return (int)(randomValue % (uint)maxValue);
}
}
}

public static string Code(string s, bool bToCode, int nKey)
{
if (s == null)
Expand Down Expand Up @@ -1996,7 +2030,7 @@ public static string Code(string s, bool bToCode, int nKey)
int nInc = 0;
int k;
const int nPasses = 11;
Random rnd = new Random(nKey);
SeededCryptoRandom rnd = new SeededCryptoRandom(nKey);
for (int j = 0; j < nPasses; ++j)
{
k = (bToCode) ? j : nPasses - j - 1;
Expand Down