Bouncy Castle - 在从 TimeStampResponse 签名之前获取哈希
Bouncy Castle - Get Hash before sign from TimeStampResponse
我正在使用 Bouncy Castle
从 TSA 获取签名哈希,就像这样-
TimeStampResponse GetSignedHashFromTsa(byte[] hash)
{
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1,
hash,
BigInteger.ValueOf(100)
);
byte[] reqData = request.GetEncoded();
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.cryptopro.ru/tsp/tsp.srf");
httpReq.Method = "POST";
httpReq.ContentType = "application/timestamp-query";
httpReq.ContentLength = reqData.Length;
// Write the request content
Stream reqStream = httpReq.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
reqStream.Close();
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
// Read the response
Stream respStream = new BufferedStream(httpResp.GetResponseStream());
TimeStampResponse response = new TimeStampResponse(respStream);
respStream.Close();
return response;
}
通过这个函数,我可以从 byte[]
.
得到 TimeStampResponse
object (same in Java and C#)
我喜欢从另一个 class 中的 TimeStampResponse
对象获取 byte[]
。有什么办法吗?
在此先感谢您的帮助。
重新
为了更好的理解Sai Ye Yan Naing Aye,我是这样调用函数的-
byte[] hashToSign = ....;
TimeStampResponse response = GetSignedHashFromTsa(hashToSign);
byte[] signedByteToSaveInFile = response.GetEncoded();
然后我将 signedByteToSaveInFile
保存在一个文件中。后来我试图找到 byte[]
签名的内容。说,我在做这个-
byte[] signedByteToSaveInFile = ....; //Read byte array from file
TimeStampResponse previouslyTsaSignedDataResponse = new TimeStampResponse(signedByteToSaveInFile);
现在我想获取在从 previouslyTsaSignedDataResponse
对象签名之前发送到 TSA 服务器的字节数组。所以,我想获取 byte[] hash
发送到 TSA 服务器进行签名的内容。换句话说,我喜欢在签名之前获取主要内容。
想想,现在问题更清楚了
我自己是这样解决的-
bool ValidateTimestamp(TimeStampResponse tr, byte[] hash)
{
try
{
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1,
hash,
BigInteger.ValueOf(100)
);
tr.Validate(request);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return tr.GetFailInfo() == null;
}
我正在使用 Bouncy Castle
从 TSA 获取签名哈希,就像这样-
TimeStampResponse GetSignedHashFromTsa(byte[] hash)
{
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1,
hash,
BigInteger.ValueOf(100)
);
byte[] reqData = request.GetEncoded();
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.cryptopro.ru/tsp/tsp.srf");
httpReq.Method = "POST";
httpReq.ContentType = "application/timestamp-query";
httpReq.ContentLength = reqData.Length;
// Write the request content
Stream reqStream = httpReq.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
reqStream.Close();
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
// Read the response
Stream respStream = new BufferedStream(httpResp.GetResponseStream());
TimeStampResponse response = new TimeStampResponse(respStream);
respStream.Close();
return response;
}
通过这个函数,我可以从 byte[]
.
TimeStampResponse
object (same in Java and C#)
我喜欢从另一个 class 中的 TimeStampResponse
对象获取 byte[]
。有什么办法吗?
在此先感谢您的帮助。
重新
为了更好的理解Sai Ye Yan Naing Aye,我是这样调用函数的-
byte[] hashToSign = ....;
TimeStampResponse response = GetSignedHashFromTsa(hashToSign);
byte[] signedByteToSaveInFile = response.GetEncoded();
然后我将 signedByteToSaveInFile
保存在一个文件中。后来我试图找到 byte[]
签名的内容。说,我在做这个-
byte[] signedByteToSaveInFile = ....; //Read byte array from file
TimeStampResponse previouslyTsaSignedDataResponse = new TimeStampResponse(signedByteToSaveInFile);
现在我想获取在从 previouslyTsaSignedDataResponse
对象签名之前发送到 TSA 服务器的字节数组。所以,我想获取 byte[] hash
发送到 TSA 服务器进行签名的内容。换句话说,我喜欢在签名之前获取主要内容。
想想,现在问题更清楚了
我自己是这样解决的-
bool ValidateTimestamp(TimeStampResponse tr, byte[] hash)
{
try
{
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1,
hash,
BigInteger.ValueOf(100)
);
tr.Validate(request);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return tr.GetFailInfo() == null;
}