More corelib cleanup (dotnet/coreclr#26993)
[mono-project.git] / netcore / System.Private.CoreLib / shared / Interop / Windows / BCrypt / Interop.BCryptGenRandom.GetRandomBytes.cs
blob42824c7eb736a9aa6d1c63f4bae7eeb204847148
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System;
6 using System.Diagnostics;
8 internal static partial class Interop
10 internal static unsafe void GetRandomBytes(byte* buffer, int length)
12 Debug.Assert(buffer != null);
13 Debug.Assert(length >= 0);
15 BCrypt.NTSTATUS status = BCrypt.BCryptGenRandom(IntPtr.Zero, buffer, length, BCrypt.BCRYPT_USE_SYSTEM_PREFERRED_RNG);
16 if (status != BCrypt.NTSTATUS.STATUS_SUCCESS)
18 if (status == BCrypt.NTSTATUS.STATUS_NO_MEMORY)
20 throw new OutOfMemoryException();
22 else
24 throw new InvalidOperationException();