///////////////////////////////////////////////////////////////////////////
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace wasSharpNET.Cryptography
{
public static class SHA1
{
///
/// Return a 40 character hex representation of a SHA1 hash.
///
/// the SHA1 hash object
/// the byte data to compute the hash from
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data)
{
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
///
/// Return a 40 character hex representation of a SHA1 hash from multiple sequential byte arrays.
///
/// the SHA1 hash object
/// the byte data to compute the hash from
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, IEnumerable data)
{
return sha1.ToHex(data.Where(i => i != null).SelectMany(i => i).ToArray());
}
///
/// Return a 40 character hex representation of a SHA1 hash.
///
/// the SHA1 hash object
/// the string data to compute the hash from
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
{
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
}
///
/// Return a 40 character hex representation of a SHA1 hash.
///
/// the SHA1 hash object
/// the memorystream to compute the hash from
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, Stream data)
{
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
///
/// Return a 40 character hex representation of a SHA1 hash.
///
/// the SHA1 hash object
/// a filestream to read the file from
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, FileStream data)
{
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string.
///
/// the SHA1 object to use
/// the data to hash
/// the output stream
public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream)
{
var buffer = new char[1];
using (var SHA1OutputStream = new MemoryStream())
{
using (var 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);
}
}
}
}