Example #1
0
 public static byte[] ToSHA384(this byte[] s)
 {
     using (var sha384 = new SHA384Cng())
     {
         return(sha384.ComputeHash(s));
     }
 }
Example #2
0
 public static byte[] ToSHA384Cng(this string s, Encoding encoding)
 {
     using (var sha384 = new SHA384Cng())
     {
         return(sha384.ComputeHash(s.GetBytes(encoding)));
     }
 }
Example #3
0
        /// <summary>
        /// Returns a hash value from a byte array.
        /// </summary>
        /// <param name="input">The byte array to hash.</param>
        /// <param name="bits">The length of the hash (256, 384, or 512).</param>
        /// <returns></returns>
        private static Byte[] BytesToHash(Byte[] input, Int32 bits)
        {
            Byte[] hash = new Byte[0];

            switch (bits)
            {
            case 256:
                hash = new SHA256Cng().ComputeHash(input);
                break;

            case 384:
                hash = new SHA384Cng().ComputeHash(input);
                break;

            case 512:
                hash = new SHA512Cng().ComputeHash(input);
                break;

            default:
                hash = new SHA256Cng().ComputeHash(input);
                break;
            }

            return(hash);
        }
Example #4
0
        /// <summary>
        /// Hash the data passed using the hash size specified defaulting to 256 if nothing is specified.
        /// </summary>
        /// <param name="data">The data to get a hash value (signature).</param>
        /// <param name="hashSize">The hash size to use (1, 256, 384 or 512)</param>
        /// <returns>A Byte[] the is a unique signature of the data passed.</returns>
        public static Byte[] SHACngHash(Byte[] data, Int32 hashSize)
        {
            Byte[] lHash = null;

            if (data != null)
            {
                if (hashSize == 512)
                {
                    using (SHA512Cng sha = new SHA512Cng()) { lHash = sha.ComputeHash(data); }
                }
                else if (hashSize == 384)
                {
                    using (SHA384Cng sha = new SHA384Cng()) { lHash = sha.ComputeHash(data); }
                }
                else if (hashSize == 256)
                {
                    using (SHA256Cng sha = new SHA256Cng()) { lHash = sha.ComputeHash(data); }
                }
                else
                {
                    using (SHA1Cng sha = new SHA1Cng()) { lHash = sha.ComputeHash(data); }
                }
            }

            return(lHash);
        }
Example #5
0
 public static String ComputeSHA384(byte[] bytes)
 {
     using (var hashAlgorithmImpl = new SHA384Cng())
     {
         var hashBytes = hashAlgorithmImpl.ComputeHash(bytes);
         return(String.Concat(hashBytes.Select(b => b.ToString("x2"))));
     }
 }
Example #6
0
 /// <summary>
 /// CheckSum384 method implementation
 /// </summary>
 public static byte[] CheckSum384(byte[] value)
 {
     byte[] hash = null;
     using (SHA384 sha384 = SHA384Cng.Create())
     {
         hash = sha384.ComputeHash(value);
     }
     return(hash);
 }
 //хэш
 public string GetHash(string pswd)
 {
     using (var hash = SHA384Cng.Create())
     {
         string sHash = string.Concat(hash.ComputeHash(Encoding.UTF8.GetBytes(pswd)).Select(x => x.ToString("x2")));
         for (int i = 0; i < 10; i++)
         {
             sHash = string.Concat(hash.ComputeHash(Encoding.UTF8.GetBytes(sHash)).Select(x => x.ToString("x2")));
         }
         return(sHash);
     }
 }
Example #8
0
        /// <summary>
        /// 计算文件的 sha384 值
        /// </summary>
        /// <param name="path">要计算 MD5 值的文件名和路径</param>
        /// <returns>MD5 值16进制字符串</returns>
        public static string GetFileSHA384(string path)
        {
            if (System.IO.File.Exists(path) == false)
            {
                return(string.Empty);
            }

            FileStream fs     = new FileStream(path, FileMode.Open, FileAccess.Read);
            SHA384     sha384 = SHA384Cng.Create();

            byte[] hashByts = sha384.ComputeHash(fs);
            return(ConvertHashBytes(hashByts));
        }
Example #9
0
 /// <summary></summary>
 public void Dispose()
 {
     if (_AesServices != null)
     {
         _AesServices.Clear();
         _AesServices = null;
     }
     if (_MD5Services != null)
     {
         _MD5Services.Dispose();
         _MD5Services = null;
     }
     if (_SHA1Services != null)
     {
         _SHA1Services.Dispose();
         _SHA1Services = null;
     }
     if (_SHA256Services != null)
     {
         _SHA256Services.Dispose();
         _SHA256Services = null;
     }
     if (_SHA384Services != null)
     {
         _SHA384Services.Dispose();
         _SHA384Services = null;
     }
     if (_SHA512Services != null)
     {
         _SHA512Services.Dispose();
         _SHA512Services = null;
     }
     if (_Pkcs11Library != null)
     {
         _Pkcs11Library.Dispose();
         _Pkcs11Library = null;
     }
     if (_Randomness != null)
     {
         _Randomness.Dispose();
         _Randomness = null;
     }
     if (_RsaServices != null)
     {
         _RsaServices.Clear();
         _RsaServices = null;
     }
 }
Example #10
0
        public byte[] ComputeHash(byte[] input)
        {
            switch (SelectedAlgorithm)
            {
            case Algorithm.SHA1:
                return(SHA1Cng.Create().ComputeHash(input));

            case Algorithm.SHA256:
                return(SHA256Cng.Create().ComputeHash(input));

            case Algorithm.SHA384:
                return(SHA384Cng.Create().ComputeHash(input));

            case Algorithm.SHA512:
                return(SHA512Cng.Create().ComputeHash(input));

            default:
                throw new ArgumentException("Valid options are: SHA1, SHA256, SHA384 or SHA512");
            }
        }
Example #11
0
        public EncryptionServices()
        {
            _AesServices = new AesCng
            {
                BlockSize   = ciAesBlockLength << 3,
                    Mode    = CipherMode.CBC,
                    Padding = PaddingMode.PKCS7
            };

            _MD5Services            = new MD5Cng();
            _SHA1Services           = new SHA1Cng();
            _SHA256Services         = new SHA256Cng();
            _SHA384Services         = new SHA384Cng();
            _SHA512Services         = new SHA512Cng();
            _RsaServices            = new RSACng();
            _Randomness             = new RNGCryptoServiceProvider();
            _abInitialisationVector = new byte[ciAesBlockLength];

            _sPkcs11Library    = string.Empty;
            _Pkcs11LibraryInfo = null;
            CheckForOpenSc();
        }
        public bool ValidatePacket(MFUpdate_Emu update, IntPtr packet, int packetLen, IntPtr validationData, int validationLen)
        {
            bool valid = false;

            switch ((MFUpdateType)update.Header.UpdateType)
            {
            case MFUpdateType.AssemblyUpdate:
                HashAlgorithm alg = null;

                switch (validationLen)
                {
                case 512 / 8:
                    alg = new SHA512Cng();
                    break;

                case 256 / 8:
                    alg = new SHA256Cng();
                    break;

                case 384 / 8:
                    alg = new SHA384Cng();
                    break;

                case 160 / 8:
                    alg = new SHA1Cng();
                    break;
                }

                if (alg != null)
                {
                    byte[] data = new byte[packetLen];
                    Marshal.Copy(packet, data, 0, packetLen);

                    byte[] hash = alg.ComputeHash(data);

                    if (hash.Length == validationLen)
                    {
                        valid = true;
                        byte[] vHash = new byte[validationLen];
                        Marshal.Copy(validationData, vHash, 0, validationLen);

                        for (int i = 0; i < validationLen; i++)
                        {
                            if (vHash[i] != hash[i])
                            {
                                valid = false;
                                break;
                            }
                        }
                    }
                }
                break;

            case MFUpdateType.FirmwareUpdate:
                valid = true;
                break;

            case MFUpdateType.KeyUpdate:
                // TODO:
                break;
            }
            return(valid);
        }
Example #13
0
        public BDCrypto(string Password)
        {
            if (!Info.Moduls.Contains("Crypto/BDCrypto.cs"))
            {
                Info.Moduls.Add("Crypto/BDCrypto.cs");
            }

            OneKeyHasher Translator = new OneKeyHasher();

            Encryption = new Dictionary <byte, byte>();
            Decryption = new Dictionary <byte, byte>();

            byte[] TMPStore;

            using (SHA384Cng Seg2 = new SHA384Cng())
                using (HMACSHA384 Seg1 = new HMACSHA384(Encoding.UTF32.GetBytes(Password)))
                {
                    Seg1.Initialize();
                    Translator.TheHashSize    = 512;
                    Translator.TheCharsetUsed = Encoding.UTF8;
                    TMPStore = Translator.Hash(Password);

                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    TMPStore = Seg1.TransformFinalBlock(TMPStore, 0, TMPStore.Length);
                    TMPStore = Seg2.ComputeHash(TMPStore);
                }

            Translator.TheCharsetUsed = Encoding.Unicode;
            Translator.TheHashSize    = 255;
            TMPStore = Translator.Hash(TMPStore);

            int  pos = 0;
            byte tmp = 0;

            while (true)
            {
                if (!Encryption.ContainsValue(TMPStore[pos]) && !Decryption.ContainsKey(TMPStore[pos]))
                {
                    Encryption.Add(tmp, TMPStore[pos]);
                    Decryption.Add(TMPStore[pos], tmp);
                    if (tmp == 255)
                    {
                        break;
                    }
                    else
                    {
                        tmp++;
                    }
                }

                pos++;
                if (pos == TMPStore.Length)
                {
                    TMPStore = Translator.Hash(TMPStore);
                    pos      = 0;
                }
            }

            _Key   = new byte[256];
            Layers = 5;

            byte x = 0;

            while (true)
            {
                _Key[x] = Encryption[x];
                if (x == 255)
                {
                    break;
                }
                else
                {
                    x++;
                }
            }
        }
Example #14
0
 public override void SetUp()
 {
     hash = new SHA384Cng();
 }
Example #15
0
        /// <summary>ハッシュ(キー無し)サービスプロバイダの生成</summary>
        /// <param name="eha">ハッシュ(キー無し)サービスプロバイダの列挙型</param>
        /// <returns>ハッシュ(キー無し)サービスプロバイダ</returns>
        /// <remarks>
        /// EnumHashAlgorithmから、HashAlgorithmを生成するために追加。
        /// HashAlgorithm.Create(HashNameConst.SHA256) は .NET Core 2 で動作せず。
        /// - KeyedHashAlgorithm.Create("HMACSHA1") throw PNSE (on .NET Core 2
        ///   https://github.com/dotnet/standard/issues/530#issuecomment-375043416
        /// </remarks>
        public static HashAlgorithm CreateHashAlgorithmSP(EnumHashAlgorithm eha)
        {
            // ハッシュ(キー無し)サービスプロバイダ
            HashAlgorithm ha = null;

            if (eha == EnumHashAlgorithm.Default)
            {
                // 既定の暗号化サービスプロバイダ
                ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703)
            }

            #region MD5
            else if (eha == EnumHashAlgorithm.MD5_CSP)
            {
                // MD5CryptoServiceProviderサービスプロバイダ
                ha = MD5CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.MD5_CNG)
            {
                // MD5Cngサービスプロバイダ
                ha = MD5Cng.Create(); // devps(1703)
            }
#endif
            #endregion

            #region RIPEMD160
            else if (eha == EnumHashAlgorithm.RIPEMD160_M)
            {
#if NETSTD
                ha = null; // BouncyCastleを使用する。
#else
                // RIPEMD160Managedサービスプロバイダ
                ha = RIPEMD160Managed.Create(); // devps(1703)
#endif
            }
            #endregion

            #region SHA1
            else if (eha == EnumHashAlgorithm.SHA1_CSP)
            {
                // SHA1CryptoServiceProviderサービスプロバイダ
                ha = SHA1CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA1_CNG)
            {
                // SHA1Cngサービスプロバイダ
                ha = SHA1Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA1_M)
            {
                // SHA1Managedサービスプロバイダ
                ha = SHA1Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA256
            else if (eha == EnumHashAlgorithm.SHA256_CSP)
            {
                // SHA256CryptoServiceProviderサービスプロバイダ
                ha = SHA256CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA256_CNG)
            {
                // SHA256Cngサービスプロバイダ
                ha = SHA256Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA256_M)
            {
                // SHA256Managedサービスプロバイダ
                ha = SHA256Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA384
            else if (eha == EnumHashAlgorithm.SHA384_CSP)
            {
                // SHA384CryptoServiceProviderサービスプロバイダ
                ha = SHA384CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA384_CNG)
            {
                // SHA384Cngサービスプロバイダ
                ha = SHA384Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA384_M)
            {
                // SHA384Managedサービスプロバイダ
                ha = SHA384Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA512
            else if (eha == EnumHashAlgorithm.SHA512_CSP)
            {
                // SHA512CryptoServiceProviderサービスプロバイダ
                ha = SHA512CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA512_CNG)
            {
                // SHA512Cngサービスプロバイダ
                ha = SHA512Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA512_M)
            {
                // SHA512Managedサービスプロバイダ
                ha = SHA512Managed.Create(); // devps(1703)
            }
            #endregion

            else
            {
                // 既定の暗号化サービスプロバイダ
                ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703)
            }

            return(ha);
        }
Example #16
0
 protected override void SetUp()
 {
     hash = new SHA384Cng();
 }
Example #17
0
        private void calcHash()
        {/*
          * if (this.InvokeRequired)
          * {
          *     lock (sync)
          *     {
          *         this.Invoke(new openFileDialogWindowDelegate(openFileDialogWindow));*/
            /*while (!Monitor.Wait(sync, 0))    // так никогда не выйдет, видимо, своё место играет 0
             * {
             *  Application.DoEvents();
             * }*/
            /*Monitor.Wait(sync);
             * }
             * }
             * else
             * openFileDialogWindow();*/

            // openFileDialogWindow();

            if (fileName == null)
            {
                return;
            }

            richTextBox1.Text = "Подождите, хэш вычисляется";
            int si = hashTypeBox.SelectedIndex;

            ThreadPool.QueueUserWorkItem
            (
                delegate
            {
                try
                {
                    byte[] data = null;
                    if (si < 7 || si > 10)
                    {
                        if (si < 12 || si > 15)
                        {
                            data = File.ReadAllBytes(fileName);
                        }
                    }

                    byte[] hash   = null;
                    string base64 = null;
                    switch (si)
                    {
                    case 1:
                        hash = new MD5Cng().ComputeHash(data);
                        break;

                    case 2:
                        hash = new SHA1Cng().ComputeHash(data);
                        break;

                    case 3:
                        hash = new RIPEMD160Managed().ComputeHash(data);
                        break;

                    case 4:
                        hash = new SHA256Cng().ComputeHash(data);
                        break;

                    case 5:
                        hash = new SHA384Cng().ComputeHash(data);
                        break;

                    case 6:
                        hash = new SHA512Cng().ComputeHash(data);
                        break;

                    case 7:
                        hash = GetHash224(fileName);
                        break;

                    case 8:
                        hash = GetHash256(fileName);
                        break;

                    case 9:
                        hash = GetHash384(fileName);
                        break;

                    case 10:
                        hash = GetHash512(fileName);
                        break;

                    case 11:
                        base64 = Convert.ToBase64String(data);
                        break;

                    /*case 12:
                     *  hash = keccak.Gost_34_11_2012.getHash256(data, false, false);
                     *  break;
                     * case 13:
                     *  hash = keccak.Gost_34_11_2012.getHash512(data, false, false);
                     *  break;*/
                    case 12:
                        hash = GetHash256_3411(fileName);
                        //hash = HexStringToBytes(BitConverter.ToString(hash).Replace("-", "").ToLower());
                        break;

                    case 13:
                        hash = GetHash512_3411(fileName);
                        //hash = HexStringToBytes(BitConverter.ToString(hash).Replace("-", "").ToLower());
                        break;

                    case 14:
                        hash = GetHash256_3411(fileName, true);
                        break;

                    case 15:
                        hash = GetHash512_3411(fileName, true);
                        break;

                    default:
                        MessageBox.Show("Ошибка в программе: хеш выбран, но в программе не предусмотрен его расчёт. Сообщите разработчику");
                        return;
                    }

                    this.Invoke(new hashToTextBoxDelegate(hashToTextBox), hash, base64, "");
                }
                catch (Exception ex)
                {
                    this.Invoke(new hashToTextBoxDelegate(hashToTextBox), null, null, ex.Message);
                }
            });
        }