Skip to content

Commit

Permalink
Use RandomNumberGenerator instead of System.Random
Browse files Browse the repository at this point in the history
Needed for an internal assessment task.
  • Loading branch information
jstedfast committed Apr 8, 2024
1 parent b454d45 commit b5ca765
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Xamarin.MacDev/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Security.Cryptography;

#if USE_CSHARP_SQLITE
using Sqlite3 = Community.CsharpSqlite.Sqlite3;
Expand Down Expand Up @@ -93,7 +94,7 @@ public partial class SQLiteConnection : IDisposable {
private long _elapsedMilliseconds = 0;

private int _transactionDepth = 0;
private Random _rand = new Random ();
private RandomNumberGenerator _rand = RandomNumberGenerator.Create();

public Sqlite3DatabaseHandle Handle { get; private set; }
internal static readonly Sqlite3DatabaseHandle NullHandle = default (Sqlite3DatabaseHandle);
Expand Down Expand Up @@ -855,7 +856,11 @@ public void BeginTransaction ()
public string SaveTransactionPoint ()
{
int depth = Interlocked.Increment (ref _transactionDepth) - 1;
string retVal = "S" + _rand.Next (short.MaxValue) + "D" + depth;
byte[] random = new byte[2];

_rand.GetNonZeroBytes (random);
var sv = BitConverter.ToUInt16 (random, 0);
string retVal = "S" + sv + "D" + depth;

try {
Execute ("savepoint " + retVal);
Expand Down Expand Up @@ -1391,6 +1396,9 @@ public void Dispose ()

protected virtual void Dispose (bool disposing)
{
if (disposing)
_rand.Dispose ();

Close ();
}

Expand Down

0 comments on commit b5ca765

Please sign in to comment.