github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/hash/csharp/func.cs (about) 1 using System; 2 using System.Text; 3 using System.Security.Cryptography; 4 using System.IO; 5 6 namespace ConsoleApplication 7 { 8 public class Program 9 { 10 public static void Main(string[] args) 11 { 12 // if nothing is being piped in, then exit 13 if (!IsPipedInput()) 14 return; 15 16 var input = Console.In.ReadToEnd(); 17 var stream = DownloadRemoteImageFile(input); 18 var hash = CreateChecksum(stream); 19 Console.WriteLine(hash); 20 } 21 22 private static bool IsPipedInput() 23 { 24 try 25 { 26 bool isKey = Console.KeyAvailable; 27 return false; 28 } 29 catch 30 { 31 return true; 32 } 33 } 34 private static byte[] DownloadRemoteImageFile(string uri) 35 { 36 37 var request = System.Net.WebRequest.CreateHttp(uri); 38 var response = request.GetResponseAsync().Result; 39 var stream = response.GetResponseStream(); 40 using (MemoryStream ms = new MemoryStream()) 41 { 42 stream.CopyTo(ms); 43 return ms.ToArray(); 44 } 45 } 46 private static string CreateChecksum(byte[] stream) 47 { 48 using (var md5 = MD5.Create()) 49 { 50 var hash = md5.ComputeHash(stream); 51 var sBuilder = new StringBuilder(); 52 53 // Loop through each byte of the hashed data 54 // and format each one as a hexadecimal string. 55 for (int i = 0; i < hash.Length; i++) 56 { 57 sBuilder.Append(hash[i].ToString("x2")); 58 } 59 60 // Return the hexadecimal string. 61 return sBuilder.ToString(); 62 } 63 } 64 } 65 }