//---------------------------------------------------------------------
        public static KeyedHashAlgorithm Create_KeyedHashAlgorithm(KeyedHashAlgorithms KeyedHashAlgorithm_in, string Password_in)
        {
            KeyedHashAlgorithm KHA = null;

            switch (KeyedHashAlgorithm_in)
            {
            case KeyedHashAlgorithms.None:
                return(null);

            case KeyedHashAlgorithms.HMACSHA1_160:
                KHA = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(Password_in));
                break;

            case KeyedHashAlgorithms.MACTripleDES_64:
                if ((Password_in.Length <= 16))
                {
                    Password_in = Password_in.PadRight(16, "~"[0]);
                }
                else if ((Password_in.Length <= 24))
                {
                    Password_in = Password_in.PadRight(24, "~"[0]);
                }
                else
                {
                    Password_in = Password_in.Substring(0, 24);
                }
                KHA = new MACTripleDES(System.Text.Encoding.ASCII.GetBytes(Password_in));
                break;

            default:
                return(null);
            }
            return(KHA);
        }
        private static KeyedHashAlgorithm GetKeyedHashAlgorithm(KeyedHashAlgorithms keyedHashAlgorithm)
        {
            switch (keyedHashAlgorithm)
            {
            case KeyedHashAlgorithms.HMACMD5:
                return(new HMACMD5());

#if NETFRAMEWORK
            case KeyedHashAlgorithms.HMACRIPEMD160:
                return(new HMACRIPEMD160());
#endif

            case KeyedHashAlgorithms.HMACSHA1:
                return(new HMACSHA1());

            case KeyedHashAlgorithms.HMACSHA256:
                return(new HMACSHA256());

            case KeyedHashAlgorithms.HMACSHA384:
                return(new HMACSHA384());

            case KeyedHashAlgorithms.HMACSHA512:
                return(new HMACSHA512());

#if NETFRAMEWORK
            case KeyedHashAlgorithms.MACTripleDES: // TODO: What about padding mode?
                return(new MACTripleDES());        // TODO: Use TripleDESCng after upgrading to .NET Framework 4.6.2
#endif

            default:
                throw new InvalidOperationException(Resources.UnsupportedKeyedHashAlgorithmException);
            }
        }
Exemple #3
0
        public static bool IsFipsCompliant(KeyedHashAlgorithms keyedHashAlgorithm)
        {
            switch (keyedHashAlgorithm)
            {
            case KeyedHashAlgorithms.HMACMD5:
            case KeyedHashAlgorithms.HMACRIPEMD160:
                return(false);

            default:
                return(true);
            }
        }
        public static byte[] HashDataWithKey(KeyedHashAlgorithms keyedHashAlgorithm, byte[] inputBytes, byte[] keyBytes)
        {
            byte[] result;

            using (KeyedHashAlgorithm algorithm = GetKeyedHashAlgorithm(keyedHashAlgorithm))
            {
                algorithm.Key = keyBytes;
                result        = algorithm.ComputeHash(inputBytes);

                algorithm.Clear();
            }

            return(result);
        }
        public void KeyedHashAlgorithmsMatch(KeyedHashAlgorithms enumValue)
        {
            string toHash = "`~1234567890-=qwertyuiop[]\\ASDFGHJKL:\"ZXCVBNM<>?ăîșțâ";
            string key    = "{>@#F09\0";

            KeyedHashText keyedHash = new KeyedHashText
            {
                Algorithm = enumValue,
                Encoding  = new InArgument <Encoding>(ExpressionServices.Convert((env) => System.Text.Encoding.Unicode))
            };
            Dictionary <string, object> arguments = new Dictionary <string, object>();

            arguments.Add(nameof(KeyedHashText.Input), toHash);
            arguments.Add(nameof(KeyedHashText.Key), key);

            WorkflowInvoker invoker        = new WorkflowInvoker(keyedHash);
            string          activityString = (string)invoker.Invoke(arguments)[nameof(keyedHash.Result)];

            byte[] algorithmBytes = CryptographyHelper.HashDataWithKey(enumValue, Encoding.Unicode.GetBytes(toHash), Encoding.Unicode.GetBytes(key));

            Assert.Equal(activityString, BitConverter.ToString(algorithmBytes).Replace("-", string.Empty));
        }