elringus / bootsharp

Compile C# solution into single-file ES module with auto-generated JavaScript bindings and type definitions

Home Page:https://bootsharp.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guid.NewGuid() throws Exception

Skleni opened this issue · comments

Hey, thank you for this awesome project! I'm currently trying to get some existing .NET code to run in a VS Code web extension, so it came just at the right time for me, too.

I got the basics to work, however a call to Guid.NewGuid() results in an exception:

System.AggregateException: AggregateException_ctor_DefaultMessage (Arg_CryptographyException)
 ---> System.Security.Cryptography.CryptographicException: Arg_CryptographyException
   at Interop.GetCryptographicallySecureRandomBytes(Byte* , Int32 )
   at System.Guid.NewGuid()
   at Program.Main(String[] args)
   Exception_EndOfInnerExceptionStack

Do you have any idea why this happens and how it can be prevented? It works in a normal Blazor app.
It might be related to this change:

On non-Windows platforms, starting with .NET 6, this function calls the OS's underlying cryptographically secure pseudo-random number generator (CSPRNG) to generate 122 bits of strong entropy. In previous versions of .NET, the entropy is not guaranteed to be generated by a CSPRNG.

Thanks for the info! This should be fixed in #3.

Microsoft provides the implementation only for browsers, hence it wasn't working in vscode (or node): https://github.com/dotnet/runtime/blob/release/6.0/src/libraries/Native/Unix/System.Native/pal_random.js#L27

I've added a naive polyfill with Math.random, but that's probably not ideal. If someone is aware of a pure JS (without dependencies on node or browser APIs) implementation for crypto-safe random algorithm, please let me know.

Thanks for the fast reply, for our usecase the workaround should be good enough.