Функция hash_hmac - C#
Формулировка задачи:
Приветствую Всех.
Подскажите, пжта, есть ли подобный метод в c#, функции из php
string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )
Решение задачи: «Функция hash_hmac»
textual
Листинг программы
public class HMACSHA256example
{
public static void Main(string[] Fileargs)
{
string dataFile;
string signedFile;
string keySekret = "a4d1b77bbb1a4a5ca695ad72c84b77e5";
if (Fileargs.Length < 2)
{
dataFile = @"text.txt";
signedFile = "signedFile.enc";
if (!File.Exists(dataFile))
{
using (StreamWriter sw = File.CreateText(dataFile))
{
sw.WriteLine("bla_bla_bla_bla");
}
}
}
else
{
dataFile = Fileargs[0];
signedFile = Fileargs[1];
}
try
{
byte[] secretkey = Encoding.Default.GetBytes(keySekret);
SignFile(secretkey, dataFile, signedFile);
string str = File.ReadAllText("signedFile.enc", Encoding.Default);
Console.WriteLine("{0}", str);
}
catch (IOException e)
{
Console.WriteLine("Error: File not found", e);
}
}
public static void SignFile(byte[] key, String sourceFile, String destFile)
{
using (HMACSHA256 hmac = new HMACSHA256(key))
{
using (FileStream inStream = new FileStream(sourceFile, FileMode.Open))
{
using (FileStream outStream = new FileStream(destFile, FileMode.Create))
{
byte[] hashValue = hmac.ComputeHash(inStream);
inStream.Position = 0;
outStream.Write(hashValue, 0, hashValue.Length);
int bytesRead;
byte[] buffer = new byte[1024];
do
{
bytesRead = inStream.Read(buffer, 0, 1024);
outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}
return;
}
}