Android Question Hmac md5 ?

tufanv

Expert
Licensed User
Longtime User
Hello

I need to convert a string using a secret key with md5 algoritm. I couldnt find any code snippet for this .
check here pls : http://www.freeformatter.com/hmac-generator.html

How can i get this converted code with b4a ?

is it possible with encryption lib ? ( when i search as hmac i found some results but couldnt be sure )

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim hmac As HMAC
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Log(hmac.calculateRFC2104HMAC("57457457","45u75478467"))
End Sub

LogCat connected to: 9885e6514556383552
--------- beginning of crash
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
ca2acd53d629dde007b549d8c86d04626c60e65a
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 

Attachments

  • HMACV1.0.zip
    1.8 KB · Views: 193
Upvote 0

tufanv

Expert
Licensed User
Longtime User
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim hmac As HMAC
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Log(hmac.calculateRFC2104HMAC("57457457","45u75478467"))
End Sub

Hello ,

I tried this Don,
The web convertors converts ths data and key you used to
b6d38641537a88478527ce0f98f70743
but yours give
ca2acd53d629dde007b549d8c86d04626c60e65a

the lengths are also different. is this hmac md5 ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I tried the above link you posted and compared the result i got in this lib. The were the same.
I used the same paramreters in your website and my example

hmac0184.png


ca2acd53d629dde007b549d8c86d04626c60e65a

** Activity (main) Create, isFirst = true **
ca2acd53d629dde007b549d8c86d04626c60e65a
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The java source used:

B4X:
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()));
    }

}
 
Upvote 0
Top