B4J Question For the Encryptation gurus out there.

Pedro Caldeira

Active Member
Licensed User
Longtime User
I have a bash script in linux that works correclty, but i wantes to port the code to BJ4.

Is it possible to achieve the same in B4J ??
I was thinking of the encrypt and string utils for the base64 encode !?

B4X:
curl --user username:password -H "Content-Type:text/xml" -i -X POST http://192.168.0.1:8080/RPC3 -o response.xml -d @tblStruct.xml
nonce=$(sed -nE 's/.*nonce="([^"]+)"/\1/p' response.xml)
hashkey=$(echo -n $nonce|base64 -d|openssl dgst -sha1 -mac HMAC -macopt hexkey:"$(echo -n u7crun6Qk4g4z0qxJRAWGA==|base64 -d|xxd -p)" -binary|base64)
curl --user username:password -H "Content-Type:text/xml" -H "X-ServiceAuthorization: $hashkey" -X POST http://192.168.0.1:8080/RPC3 -o response.xml -d @tblStruct.xml
 

OliverA

Expert
Licensed User
Longtime User
Adapted from @somed3v3loper (https://www.b4x.com/android/forum/threads/help-me-with-twitter-api-1-1.33947/). Nonce and hexkey taken from your post (https://www.b4x.com/android/forum/threads/hash-on-nonce.84448/#post-535626). Please note that the output does not match what you posted there, but it matches running the openssl command under Ubuntu.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
   #CommandLineArgs:
   #MergeLibraries: True
#End Region

Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)
   Dim pieKey As String = "u7crun6Qk4g4z0qxJRAWGA=="
   Dim nonce As String = "QizYxctgAj5TNxMQPLeaQg=="
   Log(CreateHmacSHA1DigestBase64(pieKey, nonce))
End Sub


' Note: All inputs and outputs are Base64 encoded
' Adapted from: https://www.b4x.com/android/forum/threads/help-me-with-twitter-api-1-1.33947/
Sub CreateHmacSHA1DigestBase64(key As String, data As String) As String
   Dim su As StringUtils
   Dim m As Mac
   Dim k As KeyGenerator
   k.Initialize("HmacSHA1")
   k.KeyFromBytes(su.DecodeBase64(key))
   m.Initialise("HmacSHA1", k.Key)
   m.Update(su.DecodeBase64(data))
   Dim b() As Byte
   b = m.Sign
   Return(su.EncodeBase64(b))
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub
Output:
Waiting for debugger to connect...
Program started.
IZjHLWmtD8luMn+lPQmc01wbzig=
Program terminated (StartMessageLoop was not called).
Ubuntu:
xx@xx:~$ echo -n "QizYxctgAj5TNxMQPLeaQg==" | base64 -d | openssl dgst -sha1 -mac HMAC -macopt hexkey:"$(echo -n u7crun6Qk4g4z0qxJRAWGA==|base64 -d|xxd -p)" -binary|base64
IZjHLWmtD8luMn+lPQmc01wbzig=
xx@xx:~$

Note: Uses Encryption library (https://www.b4x.com/android/forum/threads/base64-and-encryption-library.6839/#content)
 
Last edited:
Upvote 0
Top