forked from microsoft/fsharplu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Security.fs
29 lines (26 loc) · 1.07 KB
/
Security.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// Security-related functions
module Microsoft.FSharpLu.Security
#nowarn "9"
open System.Security
open System.Runtime.InteropServices
/// Decrypt a secure string
let convertToUnsecureString (secureString:SecureString) =
let unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString)
try
Marshal.PtrToStringUni(unmanagedString)
finally
if unmanagedString <> System.IntPtr.Zero then
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString)
/// Create a secure string
/// Converted from http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly-convert-securestring-to-string.aspx,
/// courtesy of F# Discussion <[email protected]>.
let convertToSecureString (s: string) =
if isNull s then
raise <| System.ArgumentNullException("s")
let gcHandle = GCHandle.Alloc(s, GCHandleType.Pinned)
try
let secureString = new SecureString(NativeInterop.NativePtr.ofNativeInt (gcHandle.AddrOfPinnedObject()), s.Length)
secureString.MakeReadOnly()
secureString
finally
gcHandle.Free()