K kostefar Active Member Licensed User Longtime User Jul 5, 2024 #1 I am having some issues getting myself logged in to a service, which I believe is due to something I´m not doing right with the signing. See the result that I´m getting with the below: B4X: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" SHA2-256(stdin)= 0e804c9b90d393e8b907bf7ed7cfa6ca53db6fc7c2e0235418e697a7edb25716 The following is an attempt to do the same, but the result is not the same. Any idea what I´m doing wrong? B4X: Dim bc As ByteConverter Dim m As Mac Dim k As KeyGenerator Dim Secret As String = "72d4b1c24f2344f29cab98607d41c2a5" Dim input As String = "test" Dim m As Mac Dim k As KeyGenerator Dim signature As String k.Initialize("HMACSHA256") k.KeyFromBytes(Secret.GetBytes("UTF8")) m.Initialise("HMACSHA256", k.Key) m.Update(input.GetBytes("UTF8")) Dim b() As Byte b = m.Sign Dim bc As ByteConverter signature=bc.HexFromBytes(b) signature = signature.ToLowerCase Log (signature) Result: 2d869c0f5bac1f3cecb35c0a1fb85fec4b6d43b27a36d7837d173b09b3916ac2 Last edited: Jul 5, 2024
I am having some issues getting myself logged in to a service, which I believe is due to something I´m not doing right with the signing. See the result that I´m getting with the below: B4X: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" SHA2-256(stdin)= 0e804c9b90d393e8b907bf7ed7cfa6ca53db6fc7c2e0235418e697a7edb25716 The following is an attempt to do the same, but the result is not the same. Any idea what I´m doing wrong? B4X: Dim bc As ByteConverter Dim m As Mac Dim k As KeyGenerator Dim Secret As String = "72d4b1c24f2344f29cab98607d41c2a5" Dim input As String = "test" Dim m As Mac Dim k As KeyGenerator Dim signature As String k.Initialize("HMACSHA256") k.KeyFromBytes(Secret.GetBytes("UTF8")) m.Initialise("HMACSHA256", k.Key) m.Update(input.GetBytes("UTF8")) Dim b() As Byte b = m.Sign Dim bc As ByteConverter signature=bc.HexFromBytes(b) signature = signature.ToLowerCase Log (signature) Result: 2d869c0f5bac1f3cecb35c0a1fb85fec4b6d43b27a36d7837d173b09b3916ac2
Solution K K kostefar Jul 7, 2024 teddybear said: I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... Weird, here the -n does not matter. No clue why the results are different, but I found out that despite the difference between what I get from the bash and b4j, it actually works how it should. It was just my mistake to think that this part was the culprit, but thanks!
teddybear said: I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... Weird, here the -n does not matter. No clue why the results are different, but I found out that despite the difference between what I get from the bash and b4j, it actually works how it should. It was just my mistake to think that this part was the culprit, but thanks!
aeric Expert Licensed User Longtime User Jul 5, 2024 #2 kostefar said: Dim Mexcnonce As String Click to expand... I think you didn't assign value and use this variable. kostefar said: m.Update(input.GetBytes("UTF8")) Click to expand... After you assign a value then update the Mac with the nonce, not input. B4X: m.Update(Mexcnonce.GetBytes("UTF8")) edit: Unless, Mexcnonce = input & "" Last edited: Jul 5, 2024 Upvote 0
kostefar said: Dim Mexcnonce As String Click to expand... I think you didn't assign value and use this variable. kostefar said: m.Update(input.GetBytes("UTF8")) Click to expand... After you assign a value then update the Mac with the nonce, not input. B4X: m.Update(Mexcnonce.GetBytes("UTF8")) edit: Unless, Mexcnonce = input & ""
K kostefar Active Member Licensed User Longtime User Jul 5, 2024 #3 aeric said: I think you didn't assign value and use this variable. After you assign a value then update the Mac with the nonce, not input. B4X: m.Update(Mexcnonce.GetBytes("UTF8")) edit: Unless, Mexcnonce = input & "" Click to expand... Thanks aeric, it was in fact a mistake that I left the nonce line there and it has now been removed. As far as I understand, I do not need this to create an identical key to the one generated with "openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5", or am I wrong? I should add that nonce is not mentioned in the API that I´m trying to access - that part was left over from old code I had from another API where nonce was required, so I just renamed it. Timestamp is required though, but that´s not so important at this stage, where all I´m trying to do is getting the same signature, since I have the idea that the reason why I cannot log in is due to this, so I took out all the code regarding what needs to be sent, making a simple test, just to show that the signature looks different. Last edited: Jul 5, 2024 Upvote 0
aeric said: I think you didn't assign value and use this variable. After you assign a value then update the Mac with the nonce, not input. B4X: m.Update(Mexcnonce.GetBytes("UTF8")) edit: Unless, Mexcnonce = input & "" Click to expand... Thanks aeric, it was in fact a mistake that I left the nonce line there and it has now been removed. As far as I understand, I do not need this to create an identical key to the one generated with "openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5", or am I wrong? I should add that nonce is not mentioned in the API that I´m trying to access - that part was left over from old code I had from another API where nonce was required, so I just renamed it. Timestamp is required though, but that´s not so important at this stage, where all I´m trying to do is getting the same signature, since I have the idea that the reason why I cannot log in is due to this, so I took out all the code regarding what needs to be sent, making a simple test, just to show that the signature looks different.
aeric Expert Licensed User Longtime User Jul 5, 2024 #4 kostefar said: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... I am not sure the correct way to call this in bash command. If you have extra space after the "echo" keyword, it seems make a difference. The result may be differ if you have different version of openssl. Upvote 0
kostefar said: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... I am not sure the correct way to call this in bash command. If you have extra space after the "echo" keyword, it seems make a difference. The result may be differ if you have different version of openssl.
teddybear Well-Known Member Licensed User Jul 5, 2024 #5 kostefar said: B4X: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" SHA2-256(stdin)= 0e804c9b90d393e8b907bf7ed7cfa6ca53db6fc7c2e0235418e697a7edb25716 Click to expand... Install openssl 1.1.1f or 1.1.1d. Upvote 0
kostefar said: B4X: # echo "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" SHA2-256(stdin)= 0e804c9b90d393e8b907bf7ed7cfa6ca53db6fc7c2e0235418e697a7edb25716 Click to expand... Install openssl 1.1.1f or 1.1.1d.
K kostefar Active Member Licensed User Longtime User Jul 5, 2024 #6 aeric said: I am not sure the correct way to call this in bash command. If you have extra space after the "echo" keyword, it seems make a difference. The result may be differ if you have different version of openssl. Click to expand... Thanks aeric. The thing is that in the API manual, there is an example with a secret and a string, where also the expected signature is showing. This signature is identical to what I get with the bash command, but the result with b4j is different. Upvote 0
aeric said: I am not sure the correct way to call this in bash command. If you have extra space after the "echo" keyword, it seems make a difference. The result may be differ if you have different version of openssl. Click to expand... Thanks aeric. The thing is that in the API manual, there is an example with a secret and a string, where also the expected signature is showing. This signature is identical to what I get with the bash command, but the result with b4j is different.
K kostefar Active Member Licensed User Longtime User Jul 5, 2024 #7 teddybear said: Install openssl 1.1.1f or 1.1.1d. View attachment 155201 Click to expand... Though it does not matter, since my aim is to have the same result as with the bash (openssl), this is my version of openssl (from my raspberry pi): B4X: # openssl version OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023) But it´s interesting that you get a different hash than me, so a lot must have changed over the years, since your version is rather old. Upvote 0
teddybear said: Install openssl 1.1.1f or 1.1.1d. View attachment 155201 Click to expand... Though it does not matter, since my aim is to have the same result as with the bash (openssl), this is my version of openssl (from my raspberry pi): B4X: # openssl version OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023) But it´s interesting that you get a different hash than me, so a lot must have changed over the years, since your version is rather old.
teddybear Well-Known Member Licensed User Jul 5, 2024 #8 kostefar said: But it´s interesting that you get a different hash than me, so a lot must have changed over the years, since your version is rather old. Click to expand... I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Upvote 0
kostefar said: But it´s interesting that you get a different hash than me, so a lot must have changed over the years, since your version is rather old. Click to expand... I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5"
TILogistic Expert Licensed User Longtime User Jul 5, 2024 #9 ??? What is the difference between "echo" and "echo -n"? The manual page on Terminal for echo -n is the following: -n Do not print the trailing newline character. This may also be achieved by appending `\c' to the end of the string, as is do... stackoverflow.com Upvote 0
??? What is the difference between "echo" and "echo -n"? The manual page on Terminal for echo -n is the following: -n Do not print the trailing newline character. This may also be achieved by appending `\c' to the end of the string, as is do... stackoverflow.com
K kostefar Active Member Licensed User Longtime User Jul 7, 2024 #10 teddybear said: I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... Weird, here the -n does not matter. No clue why the results are different, but I found out that despite the difference between what I get from the bash and b4j, it actually works how it should. It was just my mistake to think that this part was the culprit, but thanks! Upvote 0 Solution
teddybear said: I didn't carefully read the first post, I just tested it on my machine and the result #5 was correct, same as you posted b4j code. actually the version is not problem,just the cmdline need to add -n . B4X: echo -n "test" | openssl dgst -sha256 -hmac "72d4b1c24f2344f29cab98607d41c2a5" Click to expand... Weird, here the -n does not matter. No clue why the results are different, but I found out that despite the difference between what I get from the bash and b4j, it actually works how it should. It was just my mistake to think that this part was the culprit, but thanks!