Package org.jinstagram.utils

Source Code of org.jinstagram.utils.EnforceSignedHeaderUtils

package org.jinstagram.utils;

import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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

import org.apache.commons.codec.binary.Hex;
import org.jinstagram.exceptions.InstagramException;

public class EnforceSignedHeaderUtils {

    public static final String ENFORCE_SIGNED_HEADER = "X-Insta-Forwarded-For";
   
    private static final String HMAC_SHA256 = "HmacSHA256";

    public static String signature(String clientSecret, String message) throws InstagramException {
        SecretKeySpec keySpec = new SecretKeySpec(clientSecret.getBytes(Charset.forName("UTF-8")), HMAC_SHA256);

        try {
            Mac mac = Mac.getInstance(HMAC_SHA256);
            mac.init(keySpec);
            byte[] result = mac.doFinal(message.getBytes(Charset.forName("UTF-8")));
            String encodedResult = Hex.encodeHexString(result);
            return encodedResult;
        } catch (NoSuchAlgorithmException e) {
            throw new InstagramException("Invalid algorithm name!", e);
        } catch (InvalidKeyException e) {
            throw new InstagramException("Invalid key: " + clientSecret, e);
        }
    }

}
TOP

Related Classes of org.jinstagram.utils.EnforceSignedHeaderUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.