Guest User

Hyperz

a guest
Apr 7th, 2010
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.83 KB | None | 0 0
  1. (* ====================================================================
  2.  * Basic F# Multi-Core Random Password Generator
  3.  * ====================================================================
  4.  *
  5.  * Usage examples:
  6.  * - let mySafePasswrd = PasswordGen.Generate true true true 12
  7.  * - let another5Passwrds = PasswordGen.GenerateMultiple true true true 12 5
  8.  *
  9.  * Benchmark - create 10.000 12-char passwords
  10.  * - let benchResult = PasswordGen.Benchmark 12 10000
  11.  *
  12.  * That's all folks!
  13.  *
  14.  * ==================================================================== *)
  15.  
  16. open System
  17.  
  18.  
  19. module PasswordGen =
  20.  
  21.     (* List of numbers *)
  22.     let mutable DigitChars   = ['0'..'9']
  23.     (* List of letters *)
  24.     let mutable LetterChars  = ['a'..'z'] @ ['A'..'Z']
  25.     (* List of special characters *)
  26.     let mutable SpecialChars = "_()[]{}<>!?;:=*-+/\\%.,$£&#@"
  27.                                |> fun s -> s.ToCharArray()
  28.                                |> List.ofArray
  29.  
  30.     (* Creates an async computation that generates a random (based on the seed value) password *)
  31.     let private Gen seed allowedChars length = async {
  32.         let random = new Random(seed)
  33.         let count = (allowedChars:char[]).Length
  34.         return
  35.             Array.zeroCreate<char> length
  36.             |> Array.map (fun char -> allowedChars.[random.Next(count)])
  37.             |> fun passwrd -> new String(passwrd) }
  38.  
  39.     (*  Generate a single random password *)
  40.     let Generate allowDigits allowLetters allowSpecial length =
  41.         [| if allowDigits then yield! DigitChars
  42.            if allowLetters then yield! LetterChars
  43.            if allowSpecial then yield! SpecialChars |]
  44.         |> fun chars -> Gen (Environment.TickCount) chars length
  45.         |> Async.RunSynchronously
  46.  
  47.     (* Generate multiple passwords in parallel *)
  48.     let GenerateMultiple allowDigits allowLetters allowSpecial length amount =
  49.         let seedGen = new Random()
  50.         [| if allowDigits then yield! DigitChars
  51.            if allowLetters then yield! LetterChars
  52.            if allowSpecial then yield! SpecialChars |]
  53.         |> fun chars -> [| for _ in 1 .. amount -> Gen (seedGen.Next()) chars length |]
  54.         |> Async.Parallel
  55.         |> Async.RunSynchronously
  56.  
  57.     (* Basic benchmark :) *)
  58.     let Benchmark passLength passAmount =
  59.         let str = "Seconds: {0:n4}"
  60.         if passAmount = 1 then
  61.             let sw = System.Diagnostics.Stopwatch.StartNew()
  62.             let pw = Generate true true true passLength
  63.             sw.Stop()
  64.             String.Format(str, sw.Elapsed.TotalSeconds)
  65.         elif passAmount > 1 then
  66.             let sw = System.Diagnostics.Stopwatch.StartNew()
  67.             let pw = GenerateMultiple true true true passLength passAmount
  68.             sw.Stop()
  69.             String.Format(str, sw.Elapsed.TotalSeconds)
  70.         else
  71.             "Seconds: N/A"
Add Comment
Please, Sign In to add comment