wasSharpNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 9  →  ?path2? @ 10
/Cryptography/SHA1.cs
@@ -5,7 +5,9 @@
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
 
namespace wasSharpNET.Cryptography
{
@@ -20,9 +22,44 @@
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
 
 
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
{
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string.
/// </summary>
/// <param name="sha1">the SHA1 object to use</param>
/// <param name="data">the data to hash</param>
/// <param name="outStream">the output stream</param>
public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream)
{
var buffer = new char[1];
using (MemoryStream SHA1OutputStream = new MemoryStream())
{
using (StringReader SHA1InputStream = new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "")))
{
int count;
while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0)
{
switch (buffer[0])
{
case '-':
continue;
default:
SHA1OutputStream.WriteByte((byte)buffer[0]);
break;
}
}
}
SHA1OutputStream.Position = 0L;
await SHA1OutputStream.CopyToAsync(outStream);
}
}
}
}