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
Example Usage:
BLOCKS
(Note: key can be any text, as can data)
AIX
uk.co.metricrat.justhmac256hash.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);
}
}
}