wasSharpNET – Rev 27

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  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
    {
        /// <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 from multiple sequential byte arrays.
        /// </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, IEnumerable<byte[]> data)
        {
            return sha1.ToHex(data.Where(i => i != null).SelectMany(i => i).ToArray());
        }

        /// <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, Stream 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">a filestream to read the file from</param>
        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      //
        ///////////////////////////////////////////////////////////////////////////
        /// <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 (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);
            }
        }
    }
}

Generated by GNU Enscript 1.6.5.90.