wasSharpNET – Rev 13
?pathlinks?
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace wasSharpNET.Cryptography
{
public static class SHA1
{
/// <summary>
/// Return a 40 character hex representation of a SHA1 hash.
/// </summary>
/// <param name="sha1">the SHA1 hash object</param>
/// <param name="data">the byte data to compute the hash from</param>
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data)
{
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
/// <summary>
/// Return a 40 character hex representation of a SHA1 hash.
/// </summary>
/// <param name="sha1">the SHA1 hash object</param>
/// <param name="data">the string data to compute the hash from</param>
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
{
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
}
/// <summary>
/// Return a 40 character hex representation of a SHA1 hash.
/// </summary>
/// <param name="sha1">the SHA1 hash object</param>
/// <param name="data">the memorystream to compute the hash from</param>
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, MemoryStream data)
{
return BitConverter.ToString(sha1.ComputeHash(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);
}
}
}
}