Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* ====================================================================
- * Basic F# Multi-Core Random Password Generator
- * ====================================================================
- *
- * Usage examples:
- * - let mySafePasswrd = PasswordGen.Generate true true true 12
- * - let another5Passwrds = PasswordGen.GenerateMultiple true true true 12 5
- *
- * Benchmark - create 10.000 12-char passwords
- * - let benchResult = PasswordGen.Benchmark 12 10000
- *
- * That's all folks!
- *
- * ==================================================================== *)
- open System
- module PasswordGen =
- (* List of numbers *)
- let mutable DigitChars = ['0'..'9']
- (* List of letters *)
- let mutable LetterChars = ['a'..'z'] @ ['A'..'Z']
- (* List of special characters *)
- let mutable SpecialChars = "_()[]{}<>!?;:=*-+/\\%.,$£&#@"
- |> fun s -> s.ToCharArray()
- |> List.ofArray
- (* Creates an async computation that generates a random (based on the seed value) password *)
- let private Gen seed allowedChars length = async {
- let random = new Random(seed)
- let count = (allowedChars:char[]).Length
- return
- Array.zeroCreate<char> length
- |> Array.map (fun char -> allowedChars.[random.Next(count)])
- |> fun passwrd -> new String(passwrd) }
- (* Generate a single random password *)
- let Generate allowDigits allowLetters allowSpecial length =
- [| if allowDigits then yield! DigitChars
- if allowLetters then yield! LetterChars
- if allowSpecial then yield! SpecialChars |]
- |> fun chars -> Gen (Environment.TickCount) chars length
- |> Async.RunSynchronously
- (* Generate multiple passwords in parallel *)
- let GenerateMultiple allowDigits allowLetters allowSpecial length amount =
- let seedGen = new Random()
- [| if allowDigits then yield! DigitChars
- if allowLetters then yield! LetterChars
- if allowSpecial then yield! SpecialChars |]
- |> fun chars -> [| for _ in 1 .. amount -> Gen (seedGen.Next()) chars length |]
- |> Async.Parallel
- |> Async.RunSynchronously
- (* Basic benchmark :) *)
- let Benchmark passLength passAmount =
- let str = "Seconds: {0:n4}"
- if passAmount = 1 then
- let sw = System.Diagnostics.Stopwatch.StartNew()
- let pw = Generate true true true passLength
- sw.Stop()
- String.Format(str, sw.Elapsed.TotalSeconds)
- elif passAmount > 1 then
- let sw = System.Diagnostics.Stopwatch.StartNew()
- let pw = GenerateMultiple true true true passLength passAmount
- sw.Stop()
- String.Format(str, sw.Elapsed.TotalSeconds)
- else
- "Seconds: N/A"
Add Comment
Please, Sign In to add comment