Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Evandro S. on March 21, 2025, 09:01:45 pm



Title: HMAC hash key
Post by: Evandro S. on March 21, 2025, 09:01:45 pm
Hello guys,

I'm in need of getting hmac hash keys to access AWS services via API.

In JS, i can get hmac hash using the following command:
crypto.createHmac('sha256', `mykey`).update("myadditionalvalue").digest('hex');

But i need it working on a 4gl program. In the documentation isn't that clear how to get the same cenario like the command above using xml.Crypto class.

Any help would be great.

Thanks


Title: Re: HMAC hash key
Post by: Daniele A. on March 27, 2025, 11:06:09 am
Hello Evandro,
i think this example can fits your needs:

function bdl_hmac_hash(prm_algo string, prm_data  string, prm_key string) returns (string)

define ckey        xml.CryptoKey,
       sig         xml.Signature,
       ialgo       smallint,
       signature   string

define ar_algo_supported dynamic array of record
                                             algcode string,
                                             link    string
                                          end record =
[
  ( algcode: 'HmacSHA1',  link: 'http://www.w3.org/2000/09/xmldsig#hmac-sha1' ),
  ( algcode: 'HmacSHA256',link: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'),
  ( algcode: 'HmacSHA384',link: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha384'),
  ( algcode: 'HmacSHA512',link: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha512')
]

let prm_algo = prm_algo.trim()
if prm_algo.getLength()=0 then
   let prm_algo = 'HmacSHA256'
end if

let ialgo = ar_algo_supported.search("algcode",prm_algo)
if ialgo>0 then
   let ckey = xml.CryptoKey.Create(ar_algo_supported[ialgo].link)
   try
      call ckey.setKey(prm_key)
      #display "Key url : ",ckey.getUrl()
      #display "Key size (in bits) : ",ckey.getSize()
      #display "Key type : ",ckey.getType() # displays HMAC
      #display "Key usage : ",ckey.getUsage() # displays SIGNATURE
   catch
      display "Unable to set key :",STATUS," - ",SQLCA.SQLERRM
   end try
   -- Create the signature
   let sig = xml.Signature.Create()
   call sig.setKey(ckey)
   let signature = xml.Signature.signString(ckey,prm_data)
   display iif(xml.Signature.verifyString(ckey,prm_data,signature), "success","failure")
end if

return signature
end function


Daniele