HmacSHA1 encode - Android java.security

Android examples for java.security:Sha

Description

HmacSHA1 encode

Demo Code

import android.util.Base64;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main{

    public static String sha1Base64_encode(String key, String datas) {
        String reString = "";

        try {//  www .j  av  a2  s  . c  o  m
            byte[] data = key.getBytes("UTF-8");
            SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKey);

            byte[] text = datas.getBytes("UTF-8");
            byte[] text1 = mac.doFinal(text);

            reString = Base64.encodeToString(text1, Base64.DEFAULT);

        } catch (Exception e) {
        }

        return reString;
    }

}

Related Tutorials