package com.weaver.seconddev.chapanda.test;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class HmacSha1Example {

    public static String hmacSha1(String key, String message) {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            mac.init(secretKeySpec);
            byte[] digest = mac.doFinal(message.getBytes());
            return Base64.getEncoder().encodeToString(digest);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String key = "8460bf7be9fb4db29a263681fc0e4583&7aeaaf8891d740108858765f345d1261";

        String date1 = "POST";
        String date2 = "http://api.beisenapp.com/Assess/410033/activity/testee/invite";
        System.out.println(URLEncoder.encode(date2, "UTF-8"));
        String data22 = URLEncoder.encode(date2, "UTF-8");
        System.out.println("data22:"+data22);

        String data3 = "activity_id=995912&beisen_account=";

        String message = "POST&http%3A%2F%2Fapi.beisenapp.com%2FAssess%2F410033%2Factivity%2Ftestee%2Finvite&activity_id%3D995912%26beisen_account%3Djingzhijiang410033%2540beisen.com%26end_time%3D2023%252F12%252F6%252014%253A08%253A58%26format%3Djson%26oauth_consumer_key%3Dab42a74de31246e98d844344084c708a%26oauth_nonce%3Dd164a36d-6956-4501-a21d-8cb713404118%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1701841455%26oauth_token%3D5e84276db22d41f9bedb0ba0d7b0bc18%26oauth_version%3D1.0%26start_time%3D2023%252F12%252F6%252013%253A38%253A58%26testee_list%3D%255B%257B%2522name%2522%253A%2522%25E4%25B6%25AE%25EF%25BF%25BD%25EF%25BF%25BD%25C2%25B7%25EF%25BF%25BD%25EF%25BF%25BD%25EF%25BF%25BD%25EF%25BF%25BD%2522%252C%2522mobile%2522%253Anull%252C%2522email%2522%253A%2522111%2540111.com%2522%252C%2522moreinfo%2522%253Anull%257D%255D";

        String hmac = hmacSha1(key, message);
        System.out.println("HMAC-SHA1: " + hmac);

        String encodedStr = URLEncoder.encode(hmac, "UTF-8");
        System.out.println("Encoded String: " + encodedStr);


    }
}
