get Signature string with HmacSHA1 - Android java.lang

Android examples for java.lang:String Hash

Description

get Signature string with HmacSHA1

Demo Code


//package com.java2s;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

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

public class Main {
    public static String getSignature(String signatureBaseString,
            String keyString) {//from   ww  w  .  j  a va  2  s .  com
        String signature = null;
        String algorithm = "HmacSHA1";
        try {
            Mac mac = Mac.getInstance(algorithm);
            Key key = new SecretKeySpec(keyString.getBytes(), algorithm);

            mac.init(key);
            byte[] digest = mac.doFinal(signatureBaseString.getBytes());
            signature = Base64.encodeToString(digest, Base64.NO_WRAP);
        } catch (NoSuchAlgorithmException e) {
            // FIXME
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // FIXME
            e.printStackTrace();
        }
        return signature;
    }
}

Related Tutorials