using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace TEAMModelOS.SDK.Helper.Security.ShaHash
{
    public class ShaHashHelper
    {

        public static string GetSHA1(string Code) {
            var resbuffer = Encoding.Default.GetBytes(Code);
            HashAlgorithm iSha = new SHA1CryptoServiceProvider();
            resbuffer = iSha.ComputeHash(resbuffer);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(strRes);
        }
        public static string GetSHA1(byte[] buffer)
        {
            var resbuffer = buffer;
            HashAlgorithm iSha = new SHA1CryptoServiceProvider();
            resbuffer = iSha.ComputeHash(buffer);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            // return Convert.ToBase64String(strRes);
        }
        public static string GetSHA1(Stream stream)
        {
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            // stream.Close();
            var resbuffer = buffer;
            HashAlgorithm iSha = new SHA1CryptoServiceProvider();
            resbuffer = iSha.ComputeHash(buffer);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            // return Convert.ToBase64String(strRes);
        }
        public static string GetSHA1(string Code,string SecretKey)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(SecretKey));
            byte[] resbuffer = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(Code));

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(rstRes); 
        }
        public static string GetSHA1(byte[] buffer, string SecretKey)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(SecretKey));
            byte[] resbuffer = hmacsha1.ComputeHash(buffer);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //eturn Convert.ToBase64String(buffer);
        }
        public static string GetSHA256(byte[] buffer)
        {
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] resbuffer = Sha256.ComputeHash(buffer);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(retval);

        }

        public static string GetSHA256(string Code)
        {
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] resbuffer = Sha256.ComputeHash(Encoding.UTF8.GetBytes(Code));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(retval);
        }

        public static string GetSHA256(byte[] buffer, string SecretKey)
        {
            byte[] messageBytes = buffer;
            byte[] keyByte = Encoding.UTF8.GetBytes(SecretKey);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] resbuffer = hmacsha256.ComputeHash(messageBytes);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(hashmessage);
        }

        public static string GetSHA256(string Code, string SecretKey)
        {
            byte[] messageBytes = Encoding.UTF8.GetBytes(Code);
            byte[] keyByte = Encoding.UTF8.GetBytes(SecretKey);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] resbuffer = hmacsha256.ComputeHash(messageBytes);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < resbuffer.Length; i++)
            {
                builder.Append(resbuffer[i].ToString("x2"));
            }
            return builder.ToString();
            //return Convert.ToBase64String(resbuffer);
        }
    }
}
