Пример #1
0
        private static string GetContent(byte[] secret, HttpListenerRequest request)
        {
            var ms = new System.IO.MemoryStream();

            request.InputStream.CopyTo(ms);
            byte[] bytes = ms.ToArray();
            (request.InputStream as IDisposable).Dispose();

            var signature = request.Headers.Get("X-Signature");

            if (secret != null)
            {
                using (var hmac = new System.Security.Cryptography.HMACSHA1(secret)) {
                    hmac.Initialize();
                    string result = BitConverter.ToString(
                        hmac.ComputeHash(bytes, 0, bytes.Length)
                        ).Replace("-", "");
                    if (!string.Equals(
                            signature,
                            $"sha1={result}",
                            StringComparison.OrdinalIgnoreCase
                            ))
                    {
                        return(null);
                    }
                }
            }
            return(request.ContentEncoding.GetString(bytes));
        }
Пример #2
0
        public static string HMACSHA1(string text, string passKey)
        {
            var provider = new System.Security.Cryptography.HMACSHA1(Encoding.ASCII.GetBytes(passKey));

            provider.Initialize();
            var binary = provider.ComputeHash(Encoding.ASCII.GetBytes(text));

            return(Convert.ToBase64String(binary));
        }
Пример #3
0
        private string CreateHubSignature(string bodyMessage)
        {
            var hashResult = string.Empty;

            using (var hmac = new System.Security.Cryptography.HMACSHA1(Encoding.UTF8.GetBytes(_appSecret)))
            {
                hmac.Initialize();
                var hashArray = hmac.ComputeHash(Encoding.UTF8.GetBytes(bodyMessage));
                var hash      = $"SHA1={BitConverter.ToString(hashArray).Replace("-", string.Empty)}";

                hashResult = hash;
            }

            return(hashResult);
        }
Пример #4
0
 public byte[] ComputeHash(byte[] data, int offset, int length)
 {
     _algorithm.Initialize();
     return(_algorithm.ComputeHash(data, offset, length));
 }
Пример #5
0
 public byte[] Calc(byte[] data)
 {
     _algorithm.Initialize();
     return(_algorithm.ComputeHash(data));
 }