Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am consuming a REST based service and I need to pass the signature to conume the service.My method takes 2 paramters.One is DateTime of type string and other is SubjectId of type string.I am signing the both these data using SignData method of RSACryptoServiceProvider as shown below.

How to sign both the input data one after the other to generate overall signature.

C#
public static string GenerateSignature(string dateTime, string subjectId)
      {
          SHA1Managed sha1 = new SHA1Managed();
          UnicodeEncoding encoding = new UnicodeEncoding();

          string signature = string.Empty;
          X509Certificate2 objGetCertificate = null;
          RSACryptoServiceProvider privateKey = null;

          byte[] dateTimeBytes = Encoding.UTF8.GetBytes(dateTime);
          byte[] dateTimeByteshash = sha1.ComputeHash(dateTimeBytes);

          byte[] subjectIdBytes = Encoding.UTF8.GetBytes(subjectId);
          byte[] subjectIdByteshash = sha1.ComputeHash(subjectIdBytes);



          try
          {
              objGetCertificate = GetCertificate();
              privateKey = (RSACryptoServiceProvider)objGetCertificate.PrivateKey;

              privateKey.SignHash(dateTimeByteshash, CryptoConfig.MapNameToOID("SHA1"));
              byte[] signaturebytes=  privateKey.SignHash(subjectIdByteshash, CryptoConfig.MapNameToOID("SHA1"));
              signature = Convert.ToBase64String(signaturebytes);


          }
          catch (Exception ex)
          {

          }
          finally
          {
              if (objGetCertificate != null)
                  objGetCertificate.Reset();
              if (privateKey != null)
                  privateKey.Dispose();

          }
          return signature;


What I have tried:

I have tried to concatenate the signaturebytes retrieved from both the inputs but the services is not returning any data.
Posted
Comments
Peter_in_2780 18-May-16 20:30pm    
Maybe you need to concatenate the inputs, then use the hash of that as the signature. The REST provider should tell you how to do it, what they are expecting.
Member 11698392 19-May-16 0:21am    
The service provider said that signature should be generated based on privatekey,datetime string,subjectid string.

Do we sign the each input individually by calling SingHash method on each input?
Peter_in_2780 19-May-16 3:53am    
I'd ask them just how they intend you to generate the signature. There are too many ways to combine those items for me to guess.
Member 11698392 18-May-16 22:51pm    
The service provider said that signature should be generated based on privatekey,datetime string,subjectid string.

Do we sign the each input individually by calling SingHash method on each input?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900