iOS Question use encryption lib with iencryption

tufanv

Expert
Licensed User
Longtime User
Hello

In b4j I can use encryption lib of b4a without any problem but with b4i when I add encryption library of b4a to use this :

B4X:
Sub HashHmac(data As String, secret As String) As Byte()
    Try
        Dim m As Mac                                         'm As Message Authentication Code
        Dim kg As KeyGenerator                               'kg As KeyGenerator
        kg.Initialize("HmacSHA512")                          'initialize kg using HmacSHA512 algorithm
        kg.KeyFromBytes(secret.GetBytes("UTF8"))             'encode string "secret" to an array of Bytes using UTF8
        m.Initialise("HmacSHA512", kg.Key)                   'initialize m using HmacSHA512 algorithm and the secret key
        m.Update(data.GetBytes("UTF8"))                      'encodes post data to an array of Bytes and loads it to be signed
        Return m.Sign
    Catch
        Log(LastException)
    End Try
    'sign the loaded data using the secret key, return the calc signature data
End Sub

my keyvaluestore module gives error.

B4X:
Error description: Missing parameter.
Error occurred on line: 56
Return ser.ConvertBytesToObject(cipher.Decrypt(b, Password))
Word: )

How can I fix this because it works perfect with b4j
 

tufanv

Expert
Licensed User
Longtime User
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You cannot use B4A libraries in B4i.

iEncryption library currently doesn't support hmac calculations. You can however use the code posted here: https://www.b4x.com/android/forum/t...es-v4-signature-calculator.81036/#post-513785

Go over the OBJC code and replace the two instances of SHA256 with SHA512.
Thanks ! What about the other lines of codes ? like:

B4X:
 m.Update(data.GetBytes("UTF8"))                      'encodes post data to an array of Bytes and loads it to be signed
Return m.Sign
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
The code from that post calculates the HMAC of the given input. It replaces your B4A code.
I am a bit confused now , this is the code using the hmac function

B4X:
    Dim API_Key  As String
        Dim API_Secret  As String
        Dim nonce As Int
        Dim Post_URL As String
        Dim sign As String
        Dim API_Signed As String
        Dim Byte_Conv As ByteConverter
        Dim Post_Data As String
        Dim uri As String

Post_URL = "https://bittrex.com/api/v1.1/account/getbalance" ' ------- bura
'    Post_Data = "apikey=" & API_Key  & "currency=BTC&nonce=" & nonce
 
 
        '    $apikey='xxx';
        API_Key    =apikey
        '    $apisecret='xxx';
        API_Secret = apisecret
        '    $nonce=time();
        nonce = DateTime.Now/1000
        '    $uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
        uri = Post_URL & "?apikey=" & API_Key &"&currency="& selectedchart& "&nonce=" & nonce   ' -------------------- Bura
        '    $sign=hash_hmac('sha512',$uri,$apisecret);
        sign = HMACSHA512(API_Secret, uri,False)
       
 
        API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
        API_Signed = API_Signed.ToLowerCase
 
        Dim job_GetInfo As HttpJob

        job_GetInfo.Initialize("job_GetInfo", Me)
        job_GetInfo.Download2(Post_URL, _
      Array As String("apikey", API_Key,"currency",selectedchart,"nonce", nonce))  ' ------------------------BURA
        job_GetInfo.GetRequest().SetHeader("apisign", API_Signed)
        job_GetInfo.GetRequest().SetHeader("apikey", API_Key)
        'Stuff that appears unnecessary but since others use it.....
        job_GetInfo.GetRequest().SetHeader("User-Agent","Mozilla/4.0 (compatible; Cryptsy API B4A client)")
        'job_GetInfo.GetRequest().SetContentType("application/x-www-form-urlencoded")

sign() is a byte and when i use the new code you provided i get string cant cast to byte error , If i change dim sign() as byte to dim sign as string
I get another error at

B4X:
    API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
        API_Signed = API_Signed.ToLowerCase

should i change the bytes to string or should I change other things , I am bit lost about it as I dont know the logic of the code here. I had found the code snippet somewhere in the forum.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Ok figured it out !
I needed to change to

dim sign as string

and cut these two lines :
B4X:
 API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
        API_Signed = API_Signed.ToLowerCase

and change this

B4X:
sign = HMACSHA512(API_Secret, uri,False)

to

B4X:
api_signed = HMACSHA512(API_Secret, uri,False)

Thanks Erel !
 
Upvote 0
Top