<Member Name="GetPathRoot(System.String)" />
<Member Name="HasExtension(System.String)" />
<Member Name="IsPathRooted(System.String)" />
+ <Member Name="GetRandomFileName" />
+ <Member Name="GetTempFileName" />
+ <Member Name="GetTempPath" />
</Type>
<Type Name="System.IO.PathTooLongException">
<Member Name="#ctor" />
#if FEATURE_CORECLR
#if FEATURE_PAL
[DllImport(KERNEL32, EntryPoint = "PAL_Random")]
- [ResourceExposure(ResourceScope.None)]
internal extern static bool Random(bool bStrong,
[Out, MarshalAs(UnmanagedType.LPArray)] byte[] buffer, int length);
#else
- internal const int PROV_RSA_FULL = 1;
- internal const int CRYPT_VERIFYCONTEXT = unchecked((int)0xF0000000);
+ private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002;
- [DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool CryptAcquireContext([Out] out IntPtr phProv,
- string pszContainer,
- string pszProvider,
- int dwProvType,
- int dwFlags);
+ [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
+ private static extern uint BCryptGenRandom(IntPtr hAlgorithm, [In, Out] byte[] pbBuffer, int cbBuffer, int dwFlags);
- [DllImport("advapi32", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool CryptGenRandom(IntPtr hProv,
- int dwLen,
- [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbBuffer);
+ internal static void Random(bool bStrong, byte[] buffer, int length)
+ {
+ uint status = BCryptGenRandom(IntPtr.Zero, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
+ if (status != STATUS_SUCCESS)
+ {
+ if (status == STATUS_NO_MEMORY)
+ {
+ throw new OutOfMemoryException();
+ }
+ else
+ {
+ throw new InvalidOperationException();
+ }
+ }
+ }
#endif
#endif
}
}
private const int bufferSize = 1024;
-#if !FEATURE_PAL
-#if FEATURE_CORECLR
- private static IntPtr hCryptProv;
-#else
- private static RandomNumberGenerator rng;
-#endif
+#if !FEATURE_CORECLR
#endif
private static byte[] data;
private static int currentIndex = bufferSize;
{
data = new byte[bufferSize];
Contract.Assert(bufferSize % 8 == 0, "We increment our current index by 8, so our buffer size must be a multiple of 8");
-#if !FEATURE_PAL
-#if FEATURE_CORECLR
+#if !FEATURE_CORECLR
Microsoft.Win32.Win32Native.CryptAcquireContext(out hCryptProv, null, null,
Microsoft.Win32.Win32Native.PROV_RSA_FULL, Microsoft.Win32.Win32Native.CRYPT_VERIFYCONTEXT);
-#else
- rng = RandomNumberGenerator.Create();
-#endif
#endif
}
-#if FEATURE_PAL
- Microsoft.Win32.Win32Native.Random(true, data, data.Length);
-#else
#if FEATURE_CORECLR
- Microsoft.Win32.Win32Native.CryptGenRandom(hCryptProv, data.Length, data);
+ Microsoft.Win32.Win32Native.Random(true, data, data.Length);
#else
rng.GetBytes(data);
#endif
-#endif
currentIndex = 0;
}
}
-#if !FEATURE_CORECLR
// Returns a cryptographically strong random 8.3 string that can be
// used as either a folder name or a file name.
#if FEATURE_PAL
// This gives us exactly 8 chars. We want to avoid the 8.3 short name issue
byte[] key = new byte[10];
-#if FEATURE_PAL
+#if FEATURE_CORECLR
Win32Native.Random(true, key, key.Length);
#else
// RNGCryptoServiceProvider is disposable in post-Orcas desktop mscorlibs, but not in CoreCLR's
if (r==0) __Error.WinIOError();
return sb.ToString();
}
-#endif // FEATURE_CORECLR
// Tests if a path includes a file extension. The result is
// true if the characters that follow the last directory