diff options
Diffstat (limited to 'src/mscorlib/corefx/System/IO/Path.Win32.cs')
-rw-r--r-- | src/mscorlib/corefx/System/IO/Path.Win32.cs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/mscorlib/corefx/System/IO/Path.Win32.cs b/src/mscorlib/corefx/System/IO/Path.Win32.cs new file mode 100644 index 0000000000..8a9e62e6e5 --- /dev/null +++ b/src/mscorlib/corefx/System/IO/Path.Win32.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; + +namespace System.IO +{ + public static partial class Path + { + private static unsafe void GetCryptoRandomBytes(byte* bytes, int byteCount) + { + // We need to fill a byte array with cryptographically-strong random bytes, but we can't reference + // System.Security.Cryptography.RandomNumberGenerator.dll due to layering. Instead, we just + // call to BCryptGenRandom directly, which is all that RandomNumberGenerator does. + + Debug.Assert(bytes != null); + Debug.Assert(byteCount >= 0); + + Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(bytes, byteCount); + if (status == Interop.BCrypt.NTSTATUS.STATUS_SUCCESS) + { + return; + } + else if (status == Interop.BCrypt.NTSTATUS.STATUS_NO_MEMORY) + { + throw new OutOfMemoryException(); + } + else + { + Debug.Fail("BCryptGenRandom should only fail due to OOM or invalid args / handle inputs."); + throw new InvalidOperationException(); + } + } + } +} |