github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/hash/fsharp/func.fs (about)

     1  open System
     2  open System.IO
     3  open System.Security.Cryptography
     4  
     5  let createChecksum (data:byte array) =
     6      use md5 = MD5.Create ()
     7      let hash = data |> md5.ComputeHash
     8      BitConverter.ToString(hash).Replace("-", "").ToLower ()
     9  
    10  let downloadRemoteImageFile uri =
    11      async {
    12          let req = System.Net.HttpWebRequest.Create (System.Uri (uri)) :?> System.Net.HttpWebRequest
    13          let! res = req.AsyncGetResponse ()
    14          let stm = res.GetResponseStream ()
    15          use ms = new MemoryStream ()
    16          stm.CopyTo(ms)
    17          return ms.ToArray ()
    18      }
    19  
    20  [<EntryPoint>]
    21  let main argv =
    22      let isPipedInput =
    23          try
    24              Console.KeyAvailable |> ignore
    25              false
    26          with
    27          | _ -> true
    28  
    29      match isPipedInput with
    30      | true ->
    31          let input = stdin
    32          async {
    33              let! uri = input.ReadToEndAsync () |> Async.AwaitTask
    34              return! downloadRemoteImageFile uri
    35          } |> Async.RunSynchronously
    36          |> createChecksum
    37          |> printfn "%s"
    38      | false -> () // if nothing is being piped in, then exit
    39      0