public void TestGetSignedHeader() { TeaRequest request = new TeaRequest(); request.Headers["key"] = "value"; request.Headers["host"] = "hostNull"; string signHeader = BaseUtils.GetSignedHeader(request); Assert.NotNull(signHeader); Assert.Contains("key", signHeader); Assert.DoesNotContain("host", signHeader); }
protected string _getSignature(TeaRequest request) { string signedHeader = BaseUtils.GetSignedHeader(request); string url = BaseUtils.BuildUrl(request); string date = DictUtils.GetDicValue(request.Headers, "date").ToSafeString(); string accept = DictUtils.GetDicValue(request.Headers, "accept").ToSafeString(); string contentType = DictUtils.GetDicValue(request.Headers, "content-type").ToSafeString(); string contentMd5 = DictUtils.GetDicValue(request.Headers, "content-md5").ToSafeString(); string signStr = request.Method + "\n" + accept + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + signedHeader + "\n" + url; byte[] signData; using (KeyedHashAlgorithm algorithm = CryptoConfig.CreateFromName("HMACSHA256") as KeyedHashAlgorithm) { algorithm.Key = Encoding.UTF8.GetBytes(_appSecret); signData = algorithm.ComputeHash(Encoding.UTF8.GetBytes(signStr.ToCharArray())); } string signedStr = Convert.ToBase64String(signData); return(signedStr); }