SHA256 with a key

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

SHA256 with a key

Postby antonio » Thu Dec 14, 2023 9:29 pm

I'm trying to do a SHA256 hash of a string using the HMAC method, using my secret key as the hash key.
scopes.svyCrypto.getSHA256() doesn't take a secret key as an argument.
scopes.svyCrypto.getHash() also doesn't take a secret key as an argument
scopes.svyCrypto.createOptions() allows a key and algorithm to be set, but I get an error Unsupported Algorithm: SHA-256

I've also looked at it2be cryptor plugin.

Any suggestions?
Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia

Re: SHA256 with a key

Postby Joas » Wed Jan 03, 2024 5:50 pm

I've done this before using inline java:
Code: Select all
function encode(key, data) {
   var sha256_HMAC = javax.crypto.Mac.getInstance("HmacSHA256");
   var secret_key = new javax.crypto.spec.SecretKeySpec(getStringBytes(key), "HmacSHA256");
   sha256_HMAC.init(secret_key);
   var encodedVal = org.apache.commons.codec.binary.Hex.encodeHexString(sha256_HMAC.doFinal(getStringBytes(data)));
   return encodedVal;
}
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: SHA256 with a key

Postby antonio » Thu Jan 04, 2024 12:04 am

Nice tip, thanks Joas!
Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 21 guests

cron