iOS Question Porting of B4A AMAZON WEB SERVICES V4 signature calculator

JackKirk

Well-Known Member
Licensed User
Longtime User
I need to port:

https://www.b4x.com/android/forum/threads/amazon-web-services-s3-v4-signature-calculator.81006/

to B4i for a project I am working on.

This relies on 2 subroutines:
B4X:
'************************************************************************************
'
'This procedure creates a HexSHA256Hash of a supplied string
'
'Input parameters are:
'
'       input = string to be mangled
'
'Returns:
'
'       HexSHA256Hash
'
'Notes on this procedure:
'
'       o HexSHA256Hash is lowercase hexadecimal string of SHA256 hash of input
'         string
'       o Has some distant parentage at:
'         https://www.b4x.com/android/forum/threads/amazon-s3-library.38699/
'
'************************************************************************************

'
'[Method] - lowercase hexadecimal string of SHA256 hash of input string
'
'input = string to be mangled
Public Sub HexSHA256Hash(input As String) As String
 
    Private wrk_bc As ByteConverter
    Private wrk_md As MessageDigest
    Private wrk_hash() As Byte
    Private wrk_str As String
 
    'Get SHA256 hash of input string
    wrk_hash = wrk_md.GetMessageDigest(input.GetBytes("UTF8"), "SHA-256")
 
    'Get hex of SHA256 hash of input string
    wrk_str = wrk_bc.HexFromBytes(wrk_hash)
 
    'Return lowercase hex of SHA256 hash of input string
    Return wrk_str.ToLowerCase

End Sub
and
B4X:
'************************************************************************************
'
'This procedure creates a HMACSHA256 of a supplied string for a given key
'
'Input parameters are:
'
'       key = key
'       input = string to be mangled
'       key_is_hex_string = flag to indicate if key is hex string (True) or
'                           character string (False)
'
'Returns:
'
'       HMACSHA256
'
'Notes on this procedure:
'
'       o Extracted from:
'         https://www.b4x.com/android/forum/threads/hmacsha1-hash-generation.18600/#post-106868
'
'************************************************************************************

'
'[Method] - lowercase hexadecimal string of HMAC-SHA256 of key and input string
'
'key = key
'input = string to be mangled
'key_is_hex_string = flag to indicate if key is hex string (True) or character string (False)
Public Sub HMACSHA256(key As String, input As String, key_is_hex_string As Boolean) As String

    Private wrk_mac As Mac
    Private wrk_key As KeyGenerator
    Private wrk_bc As ByteConverter
    Private wrk_byte() As Byte
 
    wrk_key.Initialize("HMACSHA256")
    If key_is_hex_string Then
        wrk_key.KeyFromBytes(wrk_bc.HexToBytes(key))
    Else
        wrk_key.KeyFromBytes(key.GetBytes("UTF8"))
    End If
    wrk_mac.Initialise("HMACSHA256", wrk_key.Key)
    wrk_mac.Update(input.GetBytes("UTF8"))
    wrk_byte = wrk_mac.Sign
 
    Return wrk_bc.HexFromBytes(wrk_byte).ToLowerCase
 
End Sub
both of which I (just) managed to pull together with a lot of fiddling - my substitute for any real deep understanding of encryption etc.

They rely on the MessageDigest, Mac and KeyGenerator objects of the B4A Encryption library - for which I can find no B4i equivalents.

Could anybody point me to a solution to this dilemma?

Thanks...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,
MessageDigest is available in iEncryption library: https://www.b4x.com/b4i/help/iencryption.html
OK, thanks.
ByteConverter is available in iRandomAccessFile library.
I wasn't worried about ByteConverter.
The signature part is missing. Please post the input and expected output and I'll show you how you can implement it.
I'm sorry, you have lost me, as I understand it I'm now only looking for B4i equivalents of B4A Mac and KeyGenerator objects.

Thanks...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel, I hope I am answering your question - if you look at the second bit of code in post #1 - it shows my total use of the Mac object.

What about the KeyGenerator object?

Thanks...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
If you call the following procedure in B4A with:

HMACSHA256("garbage", "gobbledegook", False)

the result should be:

36cc500b03eb80890522d955c40294540442852cfc8c9d8077c07f55e9f2a27d

Hope this helps...
B4X:
Public Sub HMACSHA256(key As String, input As String, key_is_hex_string As Boolean) As String


    Private wrk_mac As Mac
    Private wrk_key As KeyGenerator
    Private wrk_bc As ByteConverter
    Private wrk_byte() As Byte
  
    wrk_key.Initialize("HMACSHA256")
    If key_is_hex_string Then
        wrk_key.KeyFromBytes(wrk_bc.HexToBytes(key))
    Else
        wrk_key.KeyFromBytes(key.GetBytes("UTF8"))
    End If
    wrk_mac.Initialise("HMACSHA256", wrk_key.Key)
    wrk_mac.Update(input.GetBytes("UTF8"))
    wrk_byte = wrk_mac.Sign
  
    Return wrk_bc.HexFromBytes(wrk_byte).ToLowerCase
  
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Public Sub HMACSHA256(key As String, input As String, key_is_hex_string As Boolean) As String
   Dim no As NativeObject = Me
   Dim keyb() As Byte
   Dim bc As ByteConverter
   If key_is_hex_string Then
     keyb = bc.HexToBytes(key)
   Else
     keyb = key.GetBytes("UTF8")     
   End If
   Dim res As Object = no.RunMethod("hmacForKeyAndData::", Array(no.ArrayToNSData(keyb), no.ArrayToNSData(input.GetBytes("utf8"))))
   Dim resb() As Byte = no.NSDataToArray(res)
   Return bc.HexFromBytes(resb).ToLowerCase
End Sub

#if OBJC
#import <CommonCrypto/CommonHMAC.h>

- (NSData*) hmacForKeyAndData:(NSData*)cKey :(NSData*) cData
{
  unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
  CCHmac(kCCHmacAlgSHA256, [cKey bytes], [cKey length], [cData bytes], [cData length], cHMAC);
  return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
}
#End If

Usage:
B4X:
Dim res As String = HMACSHA256("garbage", "gobbledegook", False)
Log(res)
Log(res = "36cc500b03eb80890522d955c40294540442852cfc8c9d8077c07f55e9f2a27d")
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Brilliant - I will start porting ASAP.
 
Upvote 0
Top