5 people like it.

Generate MachineKey for Web.Config

Creates a valid random machine key to place in your web.config or machine.config file. Useful when using multiple web servers where each web server needs the same machine key for encryption/validation.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open System
open System.Text
open System.Security.Cryptography

let bytesToHex (bytes:byte[]) =
    let builder = new StringBuilder(bytes.Length * 2)  
    for i = 0 to (bytes.Length - 1) do
        builder.AppendFormat("{0:X2}", bytes.[i]) |> ignore
    builder.ToString()
    
let keyGen byteCount =
    use rng = new RNGCryptoServiceProvider()
    let bytes = Array.zeroCreate<byte> byteCount
    rng.GetBytes(bytes);
    bytesToHex bytes

let validationKey = keyGen 64
let decryptionKey = keyGen 32

printfn "<machineKey validationKey=\"%s\" decryptionKey=\"%s\" validation=\"SHA1\" />" validationKey decryptionKey
namespace System
namespace System.Text
namespace System.Security
namespace System.Security.Cryptography
val bytesToHex : bytes:byte [] -> string
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

--------------------
type byte = Byte
val builder : StringBuilder
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 23 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 7 overloads
  member AppendJoin : separator:string * [<ParamArray>] values:obj[] -> StringBuilder + 5 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:Span<char> * count:int -> unit + 1 overload
  member EnsureCapacity : capacity:int -> int
  ...
  nested type ChunkEnumerator

--------------------
StringBuilder() : StringBuilder
StringBuilder(capacity: int) : StringBuilder
StringBuilder(value: string) : StringBuilder
StringBuilder(value: string, capacity: int) : StringBuilder
StringBuilder(capacity: int, maxCapacity: int) : StringBuilder
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder
val i : int
val ignore : value:'T -> unit
val keyGen : byteCount:int -> string
val byteCount : int
val rng : RNGCryptoServiceProvider
Multiple items
type RNGCryptoServiceProvider =
  inherit RandomNumberGenerator
  new : unit -> RNGCryptoServiceProvider + 3 overloads
  member GetBytes : data:byte[] -> unit + 2 overloads
  member GetNonZeroBytes : data:byte[] -> unit + 1 overload

--------------------
RNGCryptoServiceProvider() : RNGCryptoServiceProvider
RNGCryptoServiceProvider(str: string) : RNGCryptoServiceProvider
RNGCryptoServiceProvider(rgb: byte []) : RNGCryptoServiceProvider
RNGCryptoServiceProvider(cspParams: CspParameters) : RNGCryptoServiceProvider
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...
val zeroCreate : count:int -> 'T []
val validationKey : string
val decryptionKey : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

More information

Link:http://fssnip.net/t0
Posted:5 months ago
Author:Russ Cam
Tags: machine key , encyption , web.config , sha1 , aes , y-combinator