B4J Question For the Encryptation gurus out there.

Discussion in 'B4J Questions' started by Pedro Caldeira, Mar 22, 2018.

  1. Pedro Caldeira

    Pedro Caldeira Active Member Licensed 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 !?

    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    It is most probably possible. You can calculate hashes with the Encryption library.
     
  3. OliverA

    OliverA Expert Licensed 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.

    Code:
    '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 StringAs 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 StringAs Boolean
       
    Return True
    End Sub
    Output:
    Ubuntu:
    Note: Uses Encryption library (https://www.b4x.com/android/forum/threads/base64-and-encryption-library.6839/#content)
     
    Last edited: Mar 24, 2018
    Pedro Caldeira and Erel like this.
  4. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    You are a life saver @OliverA
    Thanks so much.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice