我是 JWS 概念的新手,并被要求在 C# 中为 JSON 签名创建一个片段。我们将有多个签名,因此每次对 JSON 有效负载进行签名时,都会将其添加到签名中。
我检查了 JWS JSON 序列化以及如何在多个签名的情况下使用它。
以下是用于签名和加密的代码:
// Checking if the request contains body, usually will be null wiht HTTP GET and DELETE
if (request.Content != null)
{
byte[] content = await request.Content.ReadAsByteArrayAsync();
MD5 md5 = MD5.Create();
// Hashing the request body, any change in request body will result in different hash, we'll ensure message integrity
byte[] requestContentHash = md5.ComputeHash(content);
requestContentBase64String = Convert.ToBase64String(requestContentHash);
}
// Creating the raw signature string
string signatureRawData = String.Format("{0}{1}{2}{3}{4}{5}", APPId, requestHttpMethod, requestUri, requestTimeStamp, nonce, requestContentBase64String);
var secretKeyByteArray = Convert.FromBase64String(APIKey);
byte[] signature = Encoding.UTF8.GetBytes(signatureRawData);
using (HMACSHA256 hmac = new HMACSHA256(secretKeyByteArray))
{
byte[] signatureBytes = hmac.ComputeHash(signature);
string requestSignatureBase64String = Convert.ToBase64String(signatureBytes);
// Setting the values in the Authorization header using custom scheme (amx)
request.Headers.Authorization = new AuthenticationHeaderValue("amx", string.Format("{0}:{1}:{2}:{3}", APPId, requestSignatureBase64String, nonce, requestTimeStamp));
}
response = await base.SendAsync(request, cancellationToken);
但是我们如何实现 JSON 签名呢?
我需要帮助实现我们如何使用 SignedXML 逻辑,以使用 x509 证书签署 XML 文档。