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


C# HMACSHA1.Initialize方法代码示例

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


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

示例1: ComputeCombinedKey

 public static byte[] ComputeCombinedKey(byte[] requestorEntropy, byte[] issuerEntropy, int keySizeInBits)
 {
     if (requestorEntropy == null)
     {
         throw new ArgumentNullException("requestorEntropy");
     }
     if (issuerEntropy == null)
     {
         throw new ArgumentNullException("issuerEntropy");
     }
     int num = ValidateKeySizeInBytes(keySizeInBits);
     byte[] array = new byte[num];
     
     using (KeyedHashAlgorithm algorithm = new HMACSHA1())
     {
         algorithm.Key = requestorEntropy;
         byte[] buffer = issuerEntropy;
         byte[] buffer3 = new byte[(algorithm.HashSize / 8) + buffer.Length];
         byte[] buffer4 = null;
         try
         {
             try
             {
                 int num2 = 0;
                 while (num2 < num)
                 {
                     algorithm.Initialize();
                     buffer = algorithm.ComputeHash(buffer);
                     buffer.CopyTo(buffer3, 0);
                     issuerEntropy.CopyTo(buffer3, buffer.Length);
                     algorithm.Initialize();
                     buffer4 = algorithm.ComputeHash(buffer3);
                     for (int i = 0; i < buffer4.Length; i++)
                     {
                         if (num2 >= num)
                         {
                             continue;
                         }
                         array[num2++] = buffer4[i];
                     }
                 }
             }
             catch
             {
                 Array.Clear(array, 0, array.Length);
                 throw;
             }
             return array;
         }
         finally
         {
             if (buffer4 != null)
             {
                 Array.Clear(buffer4, 0, buffer4.Length);
             }
             Array.Clear(buffer3, 0, buffer3.Length);
             algorithm.Clear();
         }
     }
 }
开发者ID:bencoveney,项目名称:Thinktecture.IdentityModel.40,代码行数:60,代码来源:KeyGenerator.cs

示例2: ToHMAC_SHA1_Encrypted

 public static string ToHMAC_SHA1_Encrypted(this string text, string key)
 {
     HMACSHA1 hmacSha = new HMACSHA1(Encoding.UTF8.GetBytes(key));
     hmacSha.Initialize();
     byte[] hmac = hmacSha.ComputeHash(Encoding.UTF8.GetBytes(text));
     return System.Text.Encoding.UTF8.GetString(hmac);
 }
开发者ID:GovindMalviya,项目名称:IAnswerable.Utility,代码行数:7,代码来源:EncryptionExtensions.cs

示例3: GetHmacHash

        public string GetHmacHash(string stringToSign)
        {
            var enc = Encoding.ASCII;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(_privateKey.ToString()));
            hmac.Initialize();

            byte[] buffer = enc.GetBytes(stringToSign);
            return BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();
        }
开发者ID:tomgallard,项目名称:recurly-client-net,代码行数:9,代码来源:RecurlyJs.cs

示例4: HashHMAC

        public static string HashHMAC(string PublicKey, string PrivateKey)
        {
            var enc = Encoding.UTF8;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(PrivateKey));
            hmac.Initialize();
            byte[] buffer = enc.GetBytes(PublicKey);

            return Convert.ToBase64String(hmac.ComputeHash(buffer));
        }
开发者ID:xuyongjie,项目名称:GeoWeatherQuery,代码行数:9,代码来源:Util.cs

示例5: CalculateHMACSHA1

        public static byte[] CalculateHMACSHA1(string data, string key)
        {
            var enc = System.Text.Encoding.UTF8;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(key));
            hmac.Initialize();

            byte[] buffer = enc.GetBytes(data);
            return hmac.ComputeHash(buffer);
        }
开发者ID:sk1tt1sh,项目名称:Infogram-CSharp,代码行数:9,代码来源:Helpers.cs

示例6: GetBase64Digest

        private static string GetBase64Digest(string signatureString, string secretKey)
        {
            var enc = Encoding.UTF8;
            var hmac = new HMACSHA1(enc.GetBytes(secretKey));
            hmac.Initialize();

            var buffer = enc.GetBytes(signatureString);
            return Convert.ToBase64String(hmac.ComputeHash(buffer));
        }
开发者ID:Silobreaker,项目名称:Api,代码行数:9,代码来源:Program.cs

示例7: Auth

        public void Auth()
        {
            // setup connection to endpoint
            request = WebRequest.Create(baseUrl + "auth");

            // compute HMAC
            var enc = Encoding.ASCII;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(privateKey));
            hmac.Initialize();

            var timestamp = DateTime.Now.ToString(@"MM\/dd\/yyyy hh\:mm");
            byte[] buffer = enc.GetBytes(publicKey + timestamp + salt);
            var hash = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();

            request.Headers ["X-Coursio-apikey"] = publicKey;
            request.Headers ["X-Coursio-time"] = timestamp;
            request.Headers ["X-Coursio-random"] = salt;
            request.Headers ["X-Coursio-hmac"] = hash;
            request.Method = "POST";

            byte[] byteArray = Encoding.UTF8.GetBytes ("{\"method\":\"loginHmac\"}");

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            // Write data to the Stream
            Stream dataStream = request.GetRequestStream ();
            dataStream.Write (byteArray, 0, byteArray.Length);
            dataStream.Close ();

            // Get the response.
            WebResponse response = request.GetResponse ();

            // Get the stream content and read it
            Stream dataStream2 = response.GetResponseStream ();
            StreamReader reader = new StreamReader (dataStream2);

            // Read the content.
            string responseFromServer = reader.ReadToEnd ();

            // Clean up
            reader.Close ();
            dataStream2.Close ();
            response.Close ();

            Regex regex = new Regex(@"""sessionId"":""(.*?)""");
            Match match = regex.Match(responseFromServer);
            if (match.Success)
            {
                sessionId = match.Groups[1].Value;
            }
            else
            {
                throw new System.Exception ("Login failed");
            }
        }
开发者ID:punarinta,项目名称:coursio-api,代码行数:56,代码来源:CoursioApi.cs

示例8: VerifyWebhookPayloadSigned

        /// <summary>
        /// Verifies that a GitHub webhook payload is correctly signed.
        /// </summary>
        public bool VerifyWebhookPayloadSigned(byte[] content, string signature)
        {
            string secret = _webhookSecret.Value;

            using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(secret)))
            {
                hmac.Initialize();
                var hash = hmac.ComputeHash(content);
                var str = BitConverter.ToString(hash);
                var expectedSignature = $"sha1={str.Replace("-", "").ToLower()}";

                return signature == expectedSignature;
            }
        }
开发者ID:CSClassroom,项目名称:CSClassroom,代码行数:17,代码来源:GitHubWebhookValidator.cs

示例9: digest

        public byte[] digest()
        {
            byte[] keyBytes = new byte[secret.Length];
            System.Buffer.BlockCopy(secret, 0, keyBytes, 0, secret.Length);
            System.Security.Cryptography.HMAC mac = new HMACSHA1(keyBytes);
            mac.Initialize();

            byte[] challenge = BitConverter.GetBytes(currentInterval);
            Array.Reverse(challenge); //Java is using Big Endian so we have to convert it
            mac.ComputeHash(challenge);
            byte[] hash = mac.Hash;

            return hash;
        }
开发者ID:manzke,项目名称:aerogear-otp-dotnet,代码行数:14,代码来源:Hmac.cs

示例10: ComputeSignature

        public static string ComputeSignature(string baseString, string keyString)
        {
            byte[] keyBytes = UTF8Encoding.UTF8.GetBytes(keyString);

            HMACSHA1 sha1 = new HMACSHA1(keyBytes);
            sha1.Initialize();

            byte[] baseBytes = UTF8Encoding.UTF8.GetBytes(baseString);

            byte[] text = sha1.ComputeHash(baseBytes);

            string signature = Convert.ToBase64String(text).Trim();

            return signature;
        }
开发者ID:smithydll,项目名称:boxsocial,代码行数:15,代码来源:OAuth.cs

示例11: generateSignature

        /// <summary>
        /// Generate signature
        /// </summary>
        /// <param name="apiUser"></param>
        /// <param name="apiKey"></param>
        /// <returns></returns>
        public string generateSignature(string apiUser, string apiKey)
        {
            string nonce = generateNonce(NONCE_LENGTH);
            DateTime time = DateTime.UtcNow;
            string data = apiUser + nonce + time;

            Encoding enc = Encoding.UTF8;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(apiKey));
            hmac.Initialize();

            byte[] buffer = enc.GetBytes(data);
            string signature = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();

            JObject obj = new JObject(
                new JProperty("REB-APIUSER", apiUser),
                new JProperty("REB-NONCE", nonce),
                new JProperty("REB-TIMESTAMP", time.ToString()),
                new JProperty("REB-SIGNATURE", signature)
            );

            return Convert.ToBase64String(enc.GetBytes(obj.ToString()));
        }
开发者ID:Rebilly,项目名称:rebilly-dotnet-client,代码行数:28,代码来源:RebillySignature.cs

示例12: HmacSha1

 /// <summary>
 /// HMAC sha1
 /// </summary>
 ///
 /// <param name="key">the key must be at least 8 bytes in length.</param>
 /// <param name="ins0">byte array to HMAC.</param>
 /// <returns>the hash</returns>
 /// @throws GeneralSecurityException
 public static byte[] HmacSha1(byte[] key, byte[] ins0)
 {
     if (key.Length < MIN_HMAC_KEY_LEN)
     {
         throw new Exception("HMAC key should be at least "
                 + MIN_HMAC_KEY_LEN + " bytes.");
     }
     HMACSHA1 hmac = new HMACSHA1(key);
     hmac.Initialize();
     return hmac.ComputeHash(ins0);
 }
开发者ID:s7loves,项目名称:pesta,代码行数:19,代码来源:Crypto.cs

示例13: Base64EncodeHash

        private static string Base64EncodeHash(string url)
        {
            byte[] result;
            HMACSHA1 shaM = new HMACSHA1();

            byte[] ms = new byte[url.Length];

            for (int i = 0; i < url.Length; i++)
            {
                byte b = Convert.ToByte(url[i]);
                ms[i] = (b);
            }

            shaM.Initialize();

            result = shaM.ComputeHash(ms, 0, ms.Length);

            return System.Convert.ToBase64String(result); ;
        }
开发者ID:windrobin,项目名称:3D-Repository,代码行数:19,代码来源:_3DRAPI_Imp.cs

示例14: Prepare

        protected void Prepare(string endpoint)
        {
            if (endpoint == null || endpoint.Length == 0)
            {
                throw new System.Exception ("No endpoint specified");
            }

            // setup connection to endpoint
            request = WebRequest.Create(baseUrl + endpoint);

            // compute HMAC
            var enc = Encoding.ASCII;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(privateKey));
            hmac.Initialize();

            var timestamp = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
            var signatureString = publicKey + timestamp + salt;
            byte[] buffer = enc.GetBytes(signatureString);
            var hash = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();

            request.Headers ["X-Coursio-apikey"] = publicKey;
            request.Headers ["X-Coursio-time"] = timestamp;
            request.Headers ["X-Coursio-random"] = salt;
            request.Headers ["X-Coursio-hmac"] = hash;
        }
开发者ID:Coursio,项目名称:Api,代码行数:25,代码来源:CoursioApi.cs

示例15: CreateAuthToken

        /// <summary>
        /// Helper method to create the appropriate Authentication Hash that Distimo expects
        /// </summary>
        /// <returns>Distimo Authentication Hash</returns>
        private DistimoAuthToken CreateAuthToken(string queryString)
        {
            var time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
            string data = String.Concat(queryString, time);

            HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes(DistimoAuthService.DistimoPrivateKey));
            hmac.Initialize();
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            string hash = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();

            string user = String.Concat(DistimoAuthService.DistimoUserName, ":", DistimoAuthService.DistimoPassword);
            string base64Login = Convert.ToBase64String(Encoding.Default.GetBytes(user));

            return new DistimoAuthToken(hash, base64Login, time);
        }
开发者ID:hunt3ri,项目名称:StatIt,代码行数:19,代码来源:DistimoService.cs


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