package de.donmanfred;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DesignerProperties;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Pixel;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Property;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.objects.CustomViewWrapper;
import anywheresoftware.b4a.objects.LabelWrapper;
import anywheresoftware.b4a.objects.PanelWrapper;
import anywheresoftware.b4a.objects.ViewWrapper;
import anywheresoftware.b4a.objects.collections.Map;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@Version(1.10f)
@ShortName("HMAC")
@Author(value = "DonManfred (wrapper)")
//@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
//@Events(values={"onSigned(sign As Object)"})
//@DependsOn(values={"com.android.support:support-v4"})
/**
* com.android.support:support-v4
* com.android.support:appcompat-v7
* com.android.support:cardview-v7
* com.android.support:gridlayout-v7
* com.android.support:mediarouter-v7
* com.android.support:palette-v7
* com.android.support:recyclerview-v
* com.android.support:preference-v7
* com.android.support:support-v13
* com.android.support:design
* com.google.firebase:firebase-database
*/
public class HMACWrapper {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public void setAlgorithm(String Algorithm){
}
public String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
public String calculateMD5(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
String HMAC_MD5_ALGORITHM = "HmacMD5";
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_MD5_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_MD5_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
}