当前位置: 首页>>代码示例>>C#>>正文


C# UTF8Encoding.ToHmac方法代码示例

本文整理汇总了C#中System.Text.UTF8Encoding.ToHmac方法的典型用法代码示例。如果您正苦于以下问题:C# UTF8Encoding.ToHmac方法的具体用法?C# UTF8Encoding.ToHmac怎么用?C# UTF8Encoding.ToHmac使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.UTF8Encoding的用法示例。


在下文中一共展示了UTF8Encoding.ToHmac方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FromEncryptionAtRest

        /// <summary>
        /// Decrypt string data with a supported cipher, authenticate the cipher using a supported digest type, deviate the cipher password with a supported divination type, and, finally pad a cipher using a supported padding type.
        /// </summary>
        /// <param name="data">String data.</param>
        /// <param name="password">Cipher password. Will be deviated using the supplied divination type.</param>
        /// <param name="salt">Cipher salt to be used, should be proper format for the cipher type. (Note: Normally 8, or, 14 bytes)</param>
        /// <param name="secondaryVerifier">Secondary data you wish to use to validate the cipher. This is a secondary validation check. The cipher binary is already validated. (Note: This will increase the size of the cipher)</param>
        /// <returns>Byte data</returns>
        public static string FromEncryptionAtRest(this string data, string password, byte[] salt, string secondaryVerifier = null)
        {
            var iv = new UTF8Encoding().GetBytes(data.Substring(0, 16));
            var key = password.ToHash(salt);

            var suspectedInputVerifier = data.Substring(data.Length - 32, data.Length);
            var input = new UTF8Encoding().GetBytes(data.Substring(16, data.Length - 32));
            var expectedInputVerifier = input.ToHmac(key.Data, salt);
            if (!suspectedInputVerifier.Equals(expectedInputVerifier.Data))
            {
                return string.Empty;
            }

            var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7");
            cipher.Init(false, new ParametersWithIV(new KeyParameter(key.Data), iv));
            var outputData = cipher.DoFinal(input);
            cipher.Reset();

            var output = new UTF8Encoding().GetString(outputData);
            if (!string.IsNullOrEmpty(secondaryVerifier))
            {
                var expectedVerifier = secondaryVerifier.ToHmac(key.Data, salt).Data.ToHex();
                var suspectedVerifier = output.Substring(output.Length - 64, 64);
                if (!expectedVerifier.SequenceEqual(suspectedVerifier))
                {
                    return string.Empty;
                }

                output = output.Substring(0, output.Length - 64);
            }

            return output;
        }
开发者ID:TimothyMeadows,项目名称:Blackfeather,代码行数:41,代码来源:Cryptology.cs


注:本文中的System.Text.UTF8Encoding.ToHmac方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。