Files
fn-serverless/examples/hash/func.cs
Seif Lotfy سيف لطفي 717d8455e9 fn: support for dotnet (#326)
* Add initial support for dotnet

* Initial work on dotnet example

* fn: fix docker incantation

* fn: .gitignore

* Add README.md for dotnet example

* Update docs
2016-12-05 23:13:52 +01:00

66 lines
1.8 KiB
C#
Executable File

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// if nothing is being piped in, then exit
if (!IsPipedInput())
return;
var input = Console.In.ReadToEnd();
var stream = DownloadRemoteImageFile(input);
var hash = CreateChecksum(stream);
Console.WriteLine(hash);
}
private static bool IsPipedInput()
{
try
{
bool isKey = Console.KeyAvailable;
return false;
}
catch
{
return true;
}
}
private static byte[] DownloadRemoteImageFile(string uri)
{
var request = System.Net.WebRequest.CreateHttp(uri);
var response = request.GetResponseAsync().Result;
var stream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
}
private static string CreateChecksum(byte[] stream)
{
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(stream);
var sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < hash.Length; i++)
{
sBuilder.Append(hash[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
}
}