[Free] Just hmac256 HASH

This function is available in a few extensions, and can also be found as a javascript function for the webviewer, but another personal use single function extension for you.

Developers should provide for protecting their users data wherever possible especially their passwords used for signing up and signing in. This extension provides for that by helping to store their data with a one way hash.

  • Latest version : 1.0
  • Tested : tested on App Inventor 2 (n199), in companion (2.73u) and compiled on Android 14.
  • Released : 2024-11-21T00:00:00Z
  • Last updated : 2024-11-21T00:00:00Z
  • Built : using the RUSH Extension Builder by @shreyash

BLOCK

image

Example Usage:

BLOCKS

(Note: key can be any text, as can data)

AIX
uk.co.metricrat.justhmac256hashV1.aix (4.5 KB)

SOURCE
package uk.co.metricrat.justhmac256hash;

//Credits: https://stackoverflow.com/a/40878298/2030549

import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import org.apache.commons.codec.binary.*;

import javax.crypto.*;
import javax.crypto.spec.*;

public class JustHmac256Hash extends AndroidNonvisibleComponent {

  public JustHmac256Hash(ComponentContainer container) {
    super(container.$form());
  }

  @SimpleFunction(description = "Returns hash.")
  public static String encode(String key, String data) {
    try {
      Mac hmac = Mac.getInstance("HmacSHA256");
      SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
      hmac.init(secret_key);
      return new String(Hex.encodeHex(hmac.doFinal(data.getBytes("UTF-8"))));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}
7 Likes

Hey Friend.
I'm having some trouble with this, perhaps I don't have it imported correctly or something?

I cannot seem to find the particular box you have in that example.
The only component i've been able to find was the one in the "all" category, which looks very different.
i'm attaching a screenshot. perhaps you could help me understand better? :slight_smile:

In the project properties make sure to set toolkit to default, then you should be able to find the regular blocks of the extension

Also why is the method starting with lowercase encode? It should be upper case

Screenshot_20250328_153937_DuckDuckGo
Taifun

That did the trick! :smiley: thank you so much! i was starting to get pretty frustrated with that over the last couple days XD