HMACSHA1 hash generation

PHB2

Member
Licensed User
Longtime User
I am trying to write the same encoding routine between a c# app and Basic4Android.

As hard as I try, I can't get the b4a routine to generate the same hash as the one for c#.

Can anyone help me write the b4a equivalent of (this is c# code btw):

B4X:
HMACSHA1 hash1 = new HMACSHA1();
hash1.Key = HexToByte(validationKey);
string encodedPassword = Convert.ToBase64String(hash1.ComputeHash(Encoding.Unicode.GetBytes(stringToHash)));

The validation key I am using is the same on both platforms.
 

PHB2

Member
Licensed User
Longtime User
Ah, I was using the Mac encryption method.

My code is a bit all over the place as I was trying all sorts of things, but this is it as it currently stands:

B4X:
Dim key(0) As Byte
   Dim data(0) As Byte
   Dim keydata(0) As Byte
   Dim sigdata(0) As Byte
   
   key = Bconv.HexToBytes( "ABCDABCDABCDABCD847FC70F12728DA2D1883F67020D8C5FF53CA0E309ECE70019D038B07DB3E0896ECFA34FB77F35C985D5124C040F4795ABCDABCDABCDABCD" )
   
   Dim mac1 As Mac
   Dim kg As KeyGenerator
   kg.Initialize("DESEDE")   
   kg.KeyFromBytes( key )
   
   text = "please encode me"
   data = Bconv.StringToBytes(text, "UNICODE")
   
   mac1.Initialise("HMACSHA1", kg.key)
   mac1.Update(data)
   
   sigdata = mac1.Sign
   
   Dim B64 As Base64
   b64str = B64.EncodeBtoS( sigdata, 0, sigdata.Length )
   
   Msgbox( b64str, "Encoded" )

I am not sure how the MessageDigest method would work as I don't see how I can pass my key in to it.
 
Upvote 0

greydog

Member
Licensed User
Longtime User
Hi PHB2, I am working on a very similar thing, trying to HMAC SHA256 hash some text. I am trying to get what is shown here Example REST Requests - Product Advertising API to work. I have taken your code above, changed the method to HMACSHA256 and cannot get it to match the encoded url in the example, it is driving me spare as I thought this would be the easy part of my app.
If I can get mine to work I will put how I did it in here, otherwise I am watching this thread like a hawk.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code returns the same value as in this example:
HMAC - Wikipedia, the free encyclopedia
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim m As Mac
   Dim k As KeyGenerator
   k.Initialize("HMACSHA256")
   k.KeyFromBytes("key".GetBytes("UTF8"))
   m.Initialise("HMACSHA256", k.Key)
   m.Update("The quick brown fox jumps over the lazy dog".GetBytes("UTF8"))
   Dim b() As Byte
   b = m.Sign
   Dim bc As ByteConverter
   Log(bc.HexFromBytes(b))
End Sub
 
Upvote 0

PHB2

Member
Licensed User
Longtime User
Loving your work Erel. That worked a treat for me.

In case anyone wanted to know what my c# code looks like to generate the same hash as in b4a it is here:

B4X:
             string message;
             string key;

             key = "key";
             message = "The quick brown fox jumps over the lazy dog";

             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

             byte[] keyByte = encoding.GetBytes(key);

             HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);

             byte[] messageBytes = encoding.GetBytes(message);

             byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);

             StringBuilder sb = new StringBuilder();
             for (int i = 0; i < hashmessage.Length; i++)
                 sb.Append(string.Format("{0:X2}", hashmessage[i]));

             MessageBox.Show(sb.ToString());
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
The following code returns the same value as in this example:
HMAC - Wikipedia, the free encyclopedia
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim m As Mac
   Dim k As KeyGenerator
   k.Initialize("HMACSHA256")
   k.KeyFromBytes("key".GetBytes("UTF8"))
   m.Initialise("HMACSHA256", k.Key)
   m.Update("The quick brown fox jumps over the lazy dog".GetBytes("UTF8"))
   Dim b() As Byte
   b = m.Sign
   Dim bc As ByteConverter
   Log(bc.HexFromBytes(b))
End Sub

Erel,

This looks fairly straight-forward, and I also examined the Encryption sample code, but I can't quite nail down what I am looking for. I need to create an SHA1-HMAC signature using a string and secret key. Could you modify your code above such that with these input values, I will get the required result?

String = "0080686001409"
Secret Key = "Vf31K4l9e7Ap5Tl1"

Required Result = "Y2wM3r+zPxnxatL/yyxVhXAk0AM="

Thanks so much!!
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Have you tried to replace HMACSHA256 with HMACSHA1 or HMACSHA?
I did replace HMACSHA256 with HMACSHA1 but the result was apparently in hex. I think the problem is in the area of:

Dim bc AsByteConverter
Log(bc.HexFromBytes(b))
 
Upvote 0
Top