Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RandomNumberGenerator instead of System.Random #120

Merged
merged 2 commits into from
Apr 9, 2024
Merged
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
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