Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Last active September 5, 2015 07:24
Show Gist options
  • Save ecounysis/456037 to your computer and use it in GitHub Desktop.
Save ecounysis/456037 to your computer and use it in GitHub Desktop.
#light
open System
let conc ls :string =
if List.length ls = 0 then ""
else List.reduce (fun x y -> x + y) ls
let av_chars = ["0"; "1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"; "9"; "A"; "B"; "C"; "D"; "E"; "F"; "G"; "H"; "I"; "J"; "K"; "L"; "M"; "N"; "O"; "P"; "Q"; "R"; "S"; "T"; "U"; "V"; "W"; "X"; "Y"; "Z"; "a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j"; "k"; "l"; "m"; "n"; "o"; "p"; "q"; "r"; "s"; "t"; "u"; "v"; "w"; "x"; "y"; "z";]
let special_chars = ["!"; "#"; "$"; "%"; "&"; "*"; "+"; "-"; "_"; "?"; "@";]
let getPass stringPart randLen specialLen =
let rnd = System.Random()
let rand = List.init randLen (fun i -> av_chars.[rnd.Next(List.length av_chars)])
let spec = List.init specialLen (fun i -> special_chars.[rnd.Next(List.length special_chars)])
stringPart + conc rand + conc spec
let usage x =
"usage: password first random special\ne.g. 'password ted 4 3' -> " + (getPass "ted" 4 3) + " or 'password milk 7 2' -> " + (getPass "milk" 7 2)
let validate (stringPart:string) (randLen:string) (specialLen:string) =
let randValid, randNum = Int32.TryParse(randLen)
let specialValid, specialNum = Int32.TryParse(specialLen)
match randNum, specialNum with
| y, z when y > 0 && z > 0 -> getPass stringPart y z
| y, _ when y > 0 -> getPass stringPart y 0
| y, z when y <= 0 -> "must be at least one random element\n" + usage()
| _ -> "invalid entry\n" + usage()
[<EntryPoint>]
let main args =
match args with
| [|_;_;_;|] -> printfn "%s" (validate args.[0] args.[1] args.[2])
| _ | [|"--help";_;_;|] | [|"-h";_;_;|] | [|"?";_;_;|] | [|"/?";_;_;|] -> printfn "%s" (usage 0)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment