Example #1
0
        private static string Hash(byte[] clearBuffer, HashAlgorithm algorithm)
        {
            System.Security.Cryptography.HashAlgorithm hashAlgorithm;
            switch (algorithm)
            {
            case HashAlgorithm.MD5:
                hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA1:
            default:
                hashAlgorithm = new SHA1CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA256:
                hashAlgorithm = new SHA256CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA384:
                hashAlgorithm = new SHA384CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA512:
                hashAlgorithm = new SHA512CryptoServiceProvider();
                break;
            }
            var encryptedBuffer = hashAlgorithm.ComputeHash(clearBuffer);

            return(Convert.ToBase64String(encryptedBuffer));
        }
Example #2
0
 public static byte[] SHA384(byte[] data)
 {
     using (var sha = new SHA384CryptoServiceProvider())
     {
         return(sha.ComputeHash(data));
     }
 }
Example #3
0
        internal static string ComputeSHA384(string Input)
        {
            SHA384CryptoServiceProvider CryptoSrv = new SHA384CryptoServiceProvider();

            byte[] Bytes = CryptoSrv.ComputeHash(Encoding.ASCII.GetBytes(Input));
            return(BuildString(Bytes));
        }
        public override string ComputeHash(string plainText)
        {
            var alg  = new SHA384CryptoServiceProvider();
            var hash = alg.ComputeHash(Encoding.UTF8.GetBytes(plainText));

            return($"{Convert.ToBase64String(hash)}");
        }
Example #5
0
        public User(string name, string password, string email) : base()
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));

            byte[] salt1 = new byte[32];
            using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
            {
                rngCsp.GetBytes(salt1);
            }

            Salt = Encoding.Default.GetString(salt1);

            password += Salt;

            SHA384 sha1 = new SHA384CryptoServiceProvider();

            byte[] hashbytes = sha1.ComputeHash(Encoding.Default.GetBytes(password));
            Password = 0;
            for (int i = 0; i < hashbytes.Length; i++)
            {
                Password += hashbytes[i];
            }

            Email = email ?? throw new ArgumentNullException(nameof(email));
        }
Example #6
0
 /// <summary>
 /// 48字节,384位
 /// </summary>
 /// <param name="str">内容</param>
 /// <returns></returns>
 public static string SHA384(string str)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(str);
     using SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
     byte[] byteArr = SHA384.ComputeHash(buffer);
     return(BitConverter.ToString(byteArr));
 }
Example #7
0
        public static string[] GetSHAHash(string Message)
        {
            string[] sha = new string[4];
            try
            {
                SHA1   sha1   = new SHA1CryptoServiceProvider();
                SHA256 sha256 = new SHA256CryptoServiceProvider();
                SHA384 sha384 = new SHA384CryptoServiceProvider();
                SHA512 sha512 = new SHA512CryptoServiceProvider();

                byte[] sha_in = UTF8Encoding.Default.GetBytes(Message);

                byte[] sha1_out   = sha1.ComputeHash(sha_in);
                byte[] sha256_out = sha256.ComputeHash(sha_in);
                byte[] sha384_out = sha384.ComputeHash(sha_in);
                byte[] sha512_out = sha512.ComputeHash(sha_in);

                sha[0] = BitConverter.ToString(sha1_out).Replace("-", "");
                sha[1] = BitConverter.ToString(sha256_out).Replace("-", "");
                sha[2] = BitConverter.ToString(sha384_out).Replace("-", "");
                sha[3] = BitConverter.ToString(sha512_out).Replace("-", "");
                return(sha);
            }
            catch { MessageBox.Show("校验失败"); return(sha); }
        }
        static HashAlgorithm GetHashAlgorithm(string algorithm)
        {
            HashAlgorithm hashAlgorithm;

            if (algorithm == ("SHA1"))
            {
                hashAlgorithm = new SHA1CryptoServiceProvider();
            }
            else if (algorithm == ("SHA256"))
            {
                hashAlgorithm = new SHA256CryptoServiceProvider();
            }
            else if (algorithm == ("SHA384"))
            {
                hashAlgorithm = new SHA384CryptoServiceProvider();
            }
            else if (algorithm == ("SHA512"))
            {
                hashAlgorithm = new SHA512CryptoServiceProvider();
            }
            else
            {
                hashAlgorithm = new SHA1CryptoServiceProvider();
            }
            return(hashAlgorithm);
        }
        public static byte[] ComputeHash(byte[] buffer, HashAlgorithm algorithm)
        {
            System.Security.Cryptography.HashAlgorithm hashAlgorithm;
            switch (algorithm)
            {
            case HashAlgorithm.MD5:
                hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA1:
                hashAlgorithm = new SHA1CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA256:
                hashAlgorithm = new SHA256CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA384:
                hashAlgorithm = new SHA384CryptoServiceProvider();
                break;

            default:
            case HashAlgorithm.SHA512:
                hashAlgorithm = new SHA512CryptoServiceProvider();
                break;
            }
            return(hashAlgorithm.ComputeHash(buffer));
        }
Example #10
0
        public static string Hash(this string str, HashAlgorithm algorithm = HashAlgorithm.SHA512)
        {
            var inputBytes = Encoding.ASCII.GetBytes(str);

            System.Security.Cryptography.HashAlgorithm hashAlgorithm = new SHA512CryptoServiceProvider();

            switch (algorithm)
            {
            case HashAlgorithm.MD5: hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA1: hashAlgorithm = new SHA1CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA256: hashAlgorithm = new SHA256CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA384: hashAlgorithm = new SHA384CryptoServiceProvider();
                break;

            case HashAlgorithm.SHA512: hashAlgorithm = new SHA512CryptoServiceProvider();
                break;
            }

            var result = hashAlgorithm.ComputeHash(inputBytes);

            return(result.ToHexString());
        }
Example #11
0
        private static string SHA384_Hash(byte[] source)
        {
            SHA384 sha384 = new SHA384CryptoServiceProvider(); //建立SHA384

            byte[] crypto = sha384.ComputeHash(source);        //進行SHA384加密
            return(Convert.ToBase64String(crypto));            //把加密後的字串從Byte[]轉為字串
        }
Example #12
0
 public static string SHA384(this string content, string salt = null)
 {
     using (var provider = new SHA384CryptoServiceProvider())
     {
         return(BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(content + salt))).Replace("-", ""));
     }
 }
Example #13
0
        /// <summary>
        /// 创建Hash算法
        /// </summary>
        /// <param name="hashProvider"></param>
        /// <returns></returns>
        internal static HashAlgorithm CreateHashAlgorithm(EnumHashProvider hashProvider)
        {
            HashAlgorithm hashAlgorithm = null;

            switch (hashProvider)
            {
            case EnumHashProvider.MD5CryptoServiceProvider:
                hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            //case EnumHashProvider.RIPEMD160Managed:                //not exist in .net core
            //    hashAlgorithm = RIPEMD160Managed();
            //    break;
            case EnumHashProvider.SHA1CryptoServiceProvider:
                hashAlgorithm = new SHA1CryptoServiceProvider();
                break;

            case EnumHashProvider.SHA1Managed:
                hashAlgorithm = new SHA1Managed();
                break;

            case EnumHashProvider.SHA256Managed:
                hashAlgorithm = new SHA256CryptoServiceProvider();
                break;

            case EnumHashProvider.SHA384Managed:
                hashAlgorithm = new SHA384CryptoServiceProvider();
                break;

            case EnumHashProvider.SHA512Managed:
                hashAlgorithm = new SHA512CryptoServiceProvider();
                break;
            }
            return(hashAlgorithm);
        }
Example #14
0
        public static string GetHashCodeString(this byte[] bytes, HashType type = HashType.MD5)
        {
            switch (type)
            {
            case HashType.SHA1:
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                return(BitConverter.ToString(sha1.ComputeHash(bytes)));

            case HashType.SHA256:
                SHA256 sha256 = new SHA256CryptoServiceProvider();
                return(BitConverter.ToString(sha256.ComputeHash(bytes)));

            case HashType.SHA384:
                SHA384 sha384 = new SHA384CryptoServiceProvider();
                return(BitConverter.ToString(sha384.ComputeHash(bytes)));

            case HashType.SHA512:
                SHA512 sha512 = new SHA512CryptoServiceProvider();
                return(BitConverter.ToString(sha512.ComputeHash(bytes)));

            default:
                MD5 md5 = MD5.Create();
                return(BitConverter.ToString(md5.ComputeHash(bytes)));
            }
        }
 /// <summary>
 /// SHA384
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public string SHA384(byte[] data)
 {
     using (var hashAlgorithm = new SHA384CryptoServiceProvider())
     {
         return(ComputeHash(data, hashAlgorithm));
     }
 }
Example #16
0
        public static byte[] HashRaw(EHashType type, Stream bs, bool seekBegin)
        {
            if (bs == null)
            {
                return(null);
            }

            HashAlgorithm cmd5 = null;

            switch (type)
            {
            case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;

            case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;

            case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;

            case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;

            case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            if (seekBegin)
            {
                bs.Seek(0, SeekOrigin.Begin);
            }

            byte[] bsh = cmd5.ComputeHash(bs);
            cmd5.Dispose();
            return(bsh);
        }
Example #17
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <remarks>64Base</remarks>
        /// <returns></returns>
        public static string Encrypt(string source, SHA2Type type = SHA2Type.Use256)
        {
            string encrypt = string.Empty;

            byte[] bytes = Encoding.Default.GetBytes(source);
            byte[] temp  = null;

            switch (type)
            {
            case SHA2Type.Use256:
                temp = new SHA256CryptoServiceProvider().ComputeHash(bytes);
                break;

            case SHA2Type.Use384:
                temp = new SHA384CryptoServiceProvider().ComputeHash(bytes);
                break;

            case SHA2Type.Use512:
                temp = new SHA512CryptoServiceProvider().ComputeHash(bytes);
                break;
            }
            encrypt = Convert.ToBase64String(temp);

            return(encrypt);
        }
        public static byte[] ComputeHash(byte[] bytesToHash, HashSize hashSize = HashSize.S160)
        {
            if (bytesToHash == null || bytesToHash.Length <= 0)
            {
                throw new ArgumentNullException("bytesToHash");
            }

            HashAlgorithm hasher;

            switch (hashSize)
            {
            case HashSize.S256:
                hasher = new SHA256CryptoServiceProvider();
                break;

            case HashSize.S384:
                hasher = new SHA384CryptoServiceProvider();
                break;

            case HashSize.S512:
                hasher = new SHA512CryptoServiceProvider();
                break;

            default:                     //HashSize.S160:
                hasher = new SHA1CryptoServiceProvider();
                break;
            }
            byte[] hash = hasher.ComputeHash(bytesToHash);
            hasher.Dispose();
            return(hash);
        }
Example #19
0
        public static byte[] HashRaw(EHashType type, byte[] bs, int index, int length)
        {
            if (bs == null)
            {
                return(null);
            }

            HashAlgorithm cmd5 = null;

            switch (type)
            {
            case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;

            case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;

            case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;

            case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;

            case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            bs = cmd5.ComputeHash(bs, index, length);
            cmd5.Dispose();
            return(bs);
        }
        /// <summary>
        /// Method for creating a detached signature.
        /// </summary>
        /// <param name="messageToSign">The message to sign</param>
        /// <param name="signingKey">The <see cref="X509Certificate2"/> containing the private key</param>
        /// <param name="signatureAlgorithm">The <see cref="SignatureAlgorithm"/> to use for signing the message</param>
        /// <returns>A string containing the detached signature value</returns>
        public static string SignDetached(string messageToSign, X509Certificate2 signingKey, SignatureAlgorithm signatureAlgorithm)
        {
            // Get the certificate private key
            RSACryptoServiceProvider privateKey = signingKey.PrivateKey as RSACryptoServiceProvider;

            // Get the bytes and encrypt
            byte[] buffer = Encoding.UTF8.GetBytes(messageToSign);

            object algorithm = null;

            switch (signatureAlgorithm)
            {
            case SignatureAlgorithm.Sha1:
                algorithm = new SHA1CryptoServiceProvider();
                break;

            case SignatureAlgorithm.Sha256:
                algorithm = new SHA256CryptoServiceProvider();
                break;

            case SignatureAlgorithm.Sha384:
                algorithm = new SHA384CryptoServiceProvider();
                break;

            case SignatureAlgorithm.Sha512:
                algorithm = new SHA512CryptoServiceProvider();
                break;
            }

            byte[] encryptedBytes = privateKey.SignData(buffer, algorithm);

            return(Convert.ToBase64String(encryptedBytes));
        }
        /// <summary>
        /// Extensão que computa um hash de um vetor de bytes.
        /// </summary>
        /// <param name="bytes">Objeto referenciado</param>
        /// <param name="hashAlgoritm">Algoritmo utilizado no cálculo de hash. Veja <see cref="HashAlgorithmName"/></param>
        /// <returns>Hash gerado para o byte array informado</returns>
        public static byte[] ComputeHash(this byte[] bytes, HashAlgorithmName hashAlgoritm)
        {
            var hasher = default(HashAlgorithm);

            switch (hashAlgoritm.Name)
            {
            case "MD5":
                hasher = new MD5CryptoServiceProvider();
                break;

            case "SHA1":
                hasher = new SHA1CryptoServiceProvider();
                break;

            case "SHA256":
                hasher = new SHA256CryptoServiceProvider();
                break;

            case "SHA384":
                hasher = new SHA384CryptoServiceProvider();
                break;

            case "SHA512":
                hasher = new SHA512CryptoServiceProvider();
                break;

            default:
                break;
            }

            return(hasher.ComputeHash(bytes));
        }
Example #22
0
        private string HashPass(string Password)
        {
            HashAlgorithm algorithm = SHA384CryptoServiceProvider.Create();

            byte[] bytePassword = algorithm.ComputeHash(Encoding.UTF8.GetBytes(Password));
            return(Convert.ToBase64String(bytePassword));
        }
Example #23
0
        public static string[] GetSHAHashFromFile(string fileName)
        {
            string[] sha = new string[4];
            try
            {
                SHA1   sha1   = new SHA1CryptoServiceProvider();
                SHA256 sha256 = new SHA256CryptoServiceProvider();
                SHA384 sha384 = new SHA384CryptoServiceProvider();
                SHA512 sha512 = new SHA512CryptoServiceProvider();

                FileStream file     = new FileStream(fileName, FileMode.Open);
                byte[]     sha1_out = sha1.ComputeHash(file);
                file.Close();
                file = new FileStream(fileName, FileMode.Open);
                byte[] sha256_out = sha256.ComputeHash(file);
                file.Close();
                file = new FileStream(fileName, FileMode.Open);
                byte[] sha384_out = sha384.ComputeHash(file);
                file.Close();
                file = new FileStream(fileName, FileMode.Open);
                byte[] sha512_out = sha512.ComputeHash(file);
                file.Close();

                sha[0] = BitConverter.ToString(sha1_out).Replace("-", "");
                sha[1] = BitConverter.ToString(sha256_out).Replace("-", "");
                sha[2] = BitConverter.ToString(sha384_out).Replace("-", "");
                sha[3] = BitConverter.ToString(sha512_out).Replace("-", "");
                return(sha);
            }
            catch
            { MessageBox.Show("校验失败"); return(sha); }
        }
Example #24
0
        /// <summary>
        /// Generate KDF.
        /// </summary>
        /// <param name="securitySuite">Security suite.</param>
        /// <param name="z">z Shared Secret.</param>
        /// <param name="otherInfo">Other info.</param>
        /// <returns></returns>
        public static byte[] GenerateKDF(SecuritySuite securitySuite, byte[] z, byte[] otherInfo)
        {
            GXByteBuffer bb = new GXByteBuffer();

            bb.SetUInt32(1);
            bb.Set(z);
            bb.Set(otherInfo);
            if (securitySuite == SecuritySuite.Ecdsa256)
            {
                using (SHA256 sha = new SHA256CryptoServiceProvider())
                {
                    return(sha.ComputeHash(bb.Array()));
                }
            }
            else if (securitySuite == SecuritySuite.Ecdsa384)
            {
                using (SHA384 sha = new SHA384CryptoServiceProvider())
                {
                    return(sha.ComputeHash(bb.Array()));
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("Invalid sevurity suite.");
            }
        }
Example #25
0
        public static string SHA384Encrypt(this string normalTxt)
        {
            var bytes        = Encoding.Default.GetBytes(normalTxt);
            var SHA384       = new SHA384CryptoServiceProvider();
            var encryptbytes = SHA384.ComputeHash(bytes);

            return(Base64To16(encryptbytes));
        }
Example #26
0
        // SHA384
        public static string SHA384(string str)
        {
            SHA384CryptoServiceProvider provider = new SHA384CryptoServiceProvider();

            byte[] input  = ASCIIEncoding.Default.GetBytes(str);
            byte[] output = provider.ComputeHash(input);
            return(BitConverter.ToString(output).Replace("-", "").ToLower());
        }
Example #27
0
        /// <summary>
        /// SHA384,384 bits 加密
        /// </summary>
        /// <param name="text"></param>
        public string ToSHA384(string text)
        {
            SHA384 sha384 = new SHA384CryptoServiceProvider(); //建立一個SHA384

            byte[] source = Encoding.Default.GetBytes(text);   //將字串轉為Byte[]
            byte[] crypto = sha384.ComputeHash(source);        //進行SHA384加密
            return(Convert.ToBase64String(crypto));            //把加密後的字串從Byte[]轉為字串
        }
Example #28
0
        public string ToSHA384(string text)
        {
            SHA384 sha384 = new SHA384CryptoServiceProvider();

            byte[] source = Encoding.Default.GetBytes(text);
            byte[] crypto = sha384.ComputeHash(source);
            return(Convert.ToBase64String(crypto));
        }
Example #29
0
        /// <summary>
        /// SHA384加密
        /// </summary>
        /// <param name="normalTxt"></param>
        /// <returns></returns>
        public static string SHA384Encrypt(string normalTxt)
        {
            var bytes        = Encoding.Unicode.GetBytes(normalTxt);
            var SHA384       = new SHA384CryptoServiceProvider();
            var encryptbytes = SHA384.ComputeHash(bytes);

            return(Convert.ToBase64String(encryptbytes));
        }
Example #30
0
        public static string SHA384Encrypt(this string source, string strFormat = "X2")
        {
            var bytes  = Encoding.Default.GetBytes(source);
            var SHA384 = new SHA384CryptoServiceProvider();
            var data   = SHA384.ComputeHash(bytes);

            return(data.ToString(strFormat));
        }