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

Full name: Script.bytesToHex
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
val builder : StringBuilder
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 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:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
StringBuilder() : unit
StringBuilder(capacity: int) : unit
StringBuilder(value: string) : unit
StringBuilder(value: string, capacity: int) : unit
StringBuilder(capacity: int, maxCapacity: int) : unit
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
property Array.Length: int
val i : int
StringBuilder.AppendFormat(format: string, [<ParamArray>] args: obj []) : StringBuilder
StringBuilder.AppendFormat(format: string, arg0: obj) : StringBuilder
StringBuilder.AppendFormat(provider: IFormatProvider, format: string, [<ParamArray>] args: obj []) : StringBuilder
StringBuilder.AppendFormat(format: string, arg0: obj, arg1: obj) : StringBuilder
StringBuilder.AppendFormat(format: string, arg0: obj, arg1: obj, arg2: obj) : StringBuilder
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
StringBuilder.ToString() : string
StringBuilder.ToString(startIndex: int, length: int) : string
val keyGen : byteCount:int -> string

Full name: Script.keyGen
val byteCount : int
val rng : RNGCryptoServiceProvider
Multiple items
type RNGCryptoServiceProvider =
  inherit RandomNumberGenerator
  new : unit -> RNGCryptoServiceProvider + 3 overloads
  member GetBytes : data:byte[] -> unit
  member GetNonZeroBytes : data:byte[] -> unit

Full name: System.Security.Cryptography.RNGCryptoServiceProvider

--------------------
RNGCryptoServiceProvider() : unit
RNGCryptoServiceProvider(str: string) : unit
RNGCryptoServiceProvider(rgb: byte []) : unit
RNGCryptoServiceProvider(cspParams: CspParameters) : unit
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
  ...

Full name: System.Array
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
RNGCryptoServiceProvider.GetBytes(data: byte []) : unit
val validationKey : string

Full name: Script.validationKey
val decryptionKey : string

Full name: Script.decryptionKey
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Next Version Raw view Test code New version

More information

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