Skip to content

Commit

Permalink
[Neo Core Fix]use strong randomness (#3432)
Browse files Browse the repository at this point in the history
* use strong randomness

* Update src/Plugins/DBFTPlugin/Consensus/ConsensusContext.MakePayload.cs

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 22, 2024
1 parent d0a2028 commit d8a4564
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Plugins/DBFTPlugin/Consensus/ConsensusContext.MakePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,20 @@ public ExtensiblePayload MakePrepareResponse()
});
}

// Related to issue https://github.com/neo-project/neo/issues/3431
// Ref. https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator?view=net-8.0
//
//The System.Random class relies on a seed value that can be predictable,
//especially if the seed is based on the system clock or other low-entropy sources.
//RandomNumberGenerator, however, uses sources of entropy provided by the operating
//system, which are designed to be unpredictable.
private static ulong GetNonce()
{
Random _random = new();
Span<byte> buffer = stackalloc byte[8];
_random.NextBytes(buffer);
using (var rng = System.Security.Cryptography.RandomNumberGenerator.Create())
{
rng.GetBytes(buffer);
}
return BinaryPrimitives.ReadUInt64LittleEndian(buffer);
}
}
Expand Down

0 comments on commit d8a4564

Please sign in to comment.