Generate md5 hash for request headers

0 Answers 283 Views
Fiddler Classic
Adam
Top achievements
Rank 1
Iron
Adam asked on 14 Jul 2021, 12:03 PM | edited on 07 Aug 2021, 04:14 PM

Hello,

I am using fiddler classic. I want to test a request. The api provider asks me to include 2 headers(request id and signature)

I was able to replace request id in header by placing the below code in onbefore request

if (oSession.oRequest)     
        {  
            {
                oSession.oRequest["Request-Id"] = "New Request id";

                oSession.oRequest["Signature:"] = "New Signature";           

          }    

     }

 

But I am not able to auto generate md5 hash. How can the auto-generation of md5 hash for request header be achieved?

Thank you.

 

Regards,

Adam

Lini
Telerik team
commented on 16 Jul 2021, 06:39 AM

You can generate the md5 hash when you set the header with .NET as well:

 

 

using System;
using System.Security.Cryptography;
using System.Text;

...
oSession.oRequest["Api-Signature"] = CalculateMD5("Your request id..." + "Your API key");
...
        public static string CalculateMD5(string input)
        {
            using (var md5 = MD5.Create())
            {
                var arrBytes = Encoding.ASCII.GetBytes(input);
                var arrBytesHash = md5.ComputeHash(arrBytes);
                var sb = new StringBuilder();
                for (int i = 0; i < arrBytesHash.Length; i++)
                {
                    sb.AppendFormat("{0:x2}", arrBytesHash[i]);
                }
                return sb.ToString();
            }
        }

 

I also see that you have a colon (:) in your header name that sets the Api-Signature header. It should not be a part of the header name:

 oSession.oRequest["Api-Signature"] = "New Signature";         

Adam
Top achievements
Rank 1
Iron
commented on 16 Jul 2021, 08:37 PM | edited

Hello Lini,

Thanks a lot for replying :)

Btw, I need to generate a Request-Id on before request that must be MD5-hash and be unique. Need to then append that request id to the code provided you to generate Signature. Can both be auto generated onbeforerequest in .NET?

Also, I tried the code provided by you it says "Syntax error. Write 'var identifier : Type' rather than 'Type identifier' to declare a typed variable".

i don't have much knowledge in coding. Thank you for all the help.

Regards,
Adam
Lini
Telerik team
commented on 22 Jul 2021, 01:37 PM

Hi Adam,

Unfortunately I cannot understand exactly what you want to calculate and set for the session headers. However, the sample code I gave in my message should be enough for you to write the logic. Copy the CalculateMD5 method in your code and call it with the string you want to get a hash for. If you want the string to be unique each time, you can use something like DateTime.Now.ToFileTime().ToString() to have a unique number each time you execute the code.

As for the sytax error - it seems to be a JScript error and the sample code I am giving you is in C#. If you are using JScript, I am afraid that I cannot offer help and you should search on the internet about how to calculate a MD5 hash and create unique values.

Adam
Top achievements
Rank 1
Iron
commented on 05 Aug 2021, 05:53 PM | edited

Hello Lini,

I am getting error. Where should I add the code provided by you in FIddlerScript?

Btw, I am using FIddler Classic.

using System;
using System.Security.Cryptography;
using System.Text;

...
oSession.oRequest["Api-Signature"] = CalculateMD5("Your request id..." + "Your API key");
...
        public static string CalculateMD5(string input)
        {
            using (var md5 = MD5.Create())
            {
                var arrBytes = Encoding.ASCII.GetBytes(input);
                var arrBytesHash = md5.ComputeHash(arrBytes);
                var sb = new StringBuilder();
                for (int i = 0; i < arrBytesHash.Length; i++)
                {
                    sb.AppendFormat("{0:x2}", arrBytesHash[i]);
                }
                return sb.ToString();
            }
        }
Nick Iliev
Telerik team
commented on 09 Aug 2021, 07:53 AM

Hey Adam,

 

The code snippet @Stoyan shared is written in C# (as is it can be used within FIddlerCore) so you need to re-write it in FIddlerScript.

gaelic
Top achievements
Rank 1
commented on 27 Aug 2021, 10:42 AM

The Content-MD5 header is used by servers to provide a message-integrity check for the message body. Only an origin server or requesting client should!

find a word

No answers yet. Maybe you can help?

Tags
Fiddler Classic
Asked by
Adam
Top achievements
Rank 1
Iron
Share this question
or