I am getting signed hash from TSA by using Bouncy Castle
like this-
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;
}
From this function, I can get a TimeStampResponse
object (same in Java and C#) from a byte[]
.
I like to get the byte[]
from the TimeStampResponse
object in another class. Is there any way?
Thanks in advance for helping.
For a better understanding of Sai Ye Yan Naing Aye, I am calling the function like this-
byte[] hashToSign = ....;
TimeStampResponse response = GetSignedHashFromTsa(hashToSign);
byte[] signedByteToSaveInFile = response.GetEncoded();
Then I am saving signedByteToSaveInFile
in a file. Later I am trying to find the byte[]
what is signed. Say, I am doing this-
byte[] signedByteToSaveInFile = ....; //Read byte array from file
TimeStampResponse previouslyTsaSignedDataResponse = new TimeStampResponse(signedByteToSaveInFile);
Now I like to get the byte array what was sent to TSA server before sign from previouslyTsaSignedDataResponse
object. So, I like to get byte[] hash
what was sent to TSA server to sign. In another word, I like to get the main content before sign.
Think, now the question is more clear.
I have solved it myself like this-
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;
}