Пример #1
0
        public void Test_Sha256_Base64()
        {
            var signature = SHA256HashingProvider.Signature("image", OutType.Base64);

            Output.WriteLine(signature);
            Assert.Equal("YQXWzHavQAMl6U1YjOURvlv9u3O0N9xR7KQ5F9ekPj0=", signature);
        }
Пример #2
0
        /// <summary>
        /// To SHA256
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isUpper"></param>
        /// <param name="isIncludeHyphen"></param>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public static string ToSHA256(this byte[] data, bool isUpper = true, bool isIncludeHyphen = false)
        {
            var hashBytes = SHA256HashingProvider.Signature(data);
            var str       = BitConverter.ToString(hashBytes);

            return(str.ToFixUpperCase(isUpper).ToFixHyphenChar(isIncludeHyphen));
        }
Пример #3
0
        /// <summary>
        /// 构造 X-WSSE 参数值
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public static string BuildWSSEHeader(string appKey, string appSecret)
        {
            var now   = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"); //Created
            var nonce = Guid.NewGuid().ToString().Replace("-", "");    //Nonce

            var material  = Encoding.UTF8.GetBytes(nonce + now + appSecret);
            var hashed    = SHA256HashingProvider.Signature(material);
            var hexDigest = BitConverter.ToString(hashed).Replace("-", "");
            var base64    = Convert.ToBase64String(Encoding.UTF8.GetBytes(hexDigest)); //PasswordDigest

            return($"UsernameToken Username=\"{appKey}\",PasswordDigest=\"{base64}\",Nonce=\"{nonce}\",Created=\"{now}\"");
        }
Пример #4
0
        public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            if (parameter.Value is BceObjectWrapper wrapper)
            {
                var date             = DateTime.UtcNow;
                var canonicalRequest = $"POST\n/{WebUtility.UrlEncode(wrapper.UrlSegment)}\n\nhost:{WebUtility.UrlEncode(wrapper.ApiServerUrl)}";
                //var canonicalRequest = $"POST\n/{(wrapper.UrlSegment)}\n\nhost:{(wrapper.ApiServerUrl)}";
                var content = wrapper.Message.ToSendObject(wrapper.Config).ToJson();

                context.RequestMessage.Headers.TryAddWithoutValidation("x-bce-date", date.ToString("YYYY-MM-DD"));
                context.RequestMessage.Headers.TryAddWithoutValidation("Authorization", wrapper.BceObject.GetAuthString(date, canonicalRequest, "host"));
                context.RequestMessage.Headers.TryAddWithoutValidation("x-bce-content-sha256", SHA256HashingProvider.Signature(content, encoding: Encoding.UTF8));
            }

            return(Task.CompletedTask);
        }
Пример #5
0
        public void Test_Sha256_Hex()
        {
            var signature = SHA256HashingProvider.Signature("image");

            Assert.Equal("6105D6CC76AF400325E94D588CE511BE5BFDBB73B437DC51ECA43917D7A43E3D", signature);
        }
Пример #6
0
 /// <summary>
 /// To SHA256
 /// </summary>
 /// <param name="data"></param>
 /// <param name="isUpper"></param>
 /// <param name="isIncludeHyphen"></param>
 /// <param name="encoding"></param>
 /// <returns></returns>
 // ReSharper disable once InconsistentNaming
 public static string ToSHA256(this string data, bool isUpper = true, bool isIncludeHyphen = false, Encoding encoding = null)
 {
     return(SHA256HashingProvider.Signature(data, isUpper, isIncludeHyphen, encoding));
 }