Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import java.security.GeneralSecurityException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static String computeSignature(String baseString, String keyString)
            throws GeneralSecurityException, UnsupportedEncodingException {
        Mac mac = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256");
        mac.init(secret);
        byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8"));

        BigInteger hash = new BigInteger(1, byteData);
        String hmac = hash.toString(16);

        if (hmac.length() % 2 != 0) {
            hmac = "0" + hmac;
        }

        return hmac;
    }
}