Android Question How can I to write this code into B4A?

interlnk

Member
Licensed User
How can I to write this code into B4A?

B4X:
Dim Asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set Asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")

Thanks
 
Last edited:

interlnk

Member
Licensed User
I've seen the thread and I've written this code:

B4X:
Dim m As Mac
Dim k As KeyGenerator
Dim stUt As StringUtils
Dim st As String
k.Initialize("HMACSHA1")
k.KeyFromBytes("Secreto de firma de URL".GetBytes("UTF8"))
m.Initialise("HMACSHA1", k.Key)
m.Update("URL".GetBytes("UTF8"))
Dim b() As Byte
b = m.Sign
st = stUt.EncodeBase64(b)
Log(st)

But I don't get exactly the signature I need. I get:
2nzGLUE2HDdbH7/33iE4nAoPDLU=

And I should get:
yFkqfJN-NQm_FCjr91Sq027bSbE=

What's wrong?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've run your code and I get:
f0yJ2HHs9P49sZ2VqbJxXWJhUJ8=

I guess that you changed the password.

Checking it with this online tool: https://www.freeformatter.com/hmac-generator.html#ad-output
I get: 7f4c89d871ecf4fe3db19d95a9b2715d6261509f

This is a hex string.
Converting it to base64:
B4X:
Dim bc As ByteConverter
Dim b() As Byte = bc.HexToBytes("7f4c89d871ecf4fe3db19d95a9b2715d6261509f")
Dim su As StringUtils
Log(su.EncodeBase64(b))

The output is: f0yJ2HHs9P49sZ2VqbJxXWJhUJ8=

So it seems that the code correctly calculates HMACSHA1.
 
Upvote 0

interlnk

Member
Licensed User
In my post the key and URL data were not the real ones for confidentiality

In Google Static Maps API console there is a tool to sign URL's. It returns the signature:
yFkqfJN-NQm_FCjr91Sq027bSbE=

My code, however returns the signature:
2nzGLUE2HDdbH7/33iE4nAoPDLU=
 
Last edited:
Upvote 0
Top