package com.ctrip.framework.apollo.core.signature;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author nisiyong
 */
public class HmacSha1Utils {

  private static final String ALGORITHM_NAME = "HmacSHA1";
  private static final String ENCODING = "UTF-8";

  public static String signString(String stringToSign, String accessKeySecret) {
    try {
      Mac mac = Mac.getInstance(ALGORITHM_NAME);
      mac.init(new SecretKeySpec(
          accessKeySecret.getBytes(ENCODING),
          ALGORITHM_NAME
      ));
      byte[] signData = mac.doFinal(stringToSign.getBytes(ENCODING));
      return DatatypeConverter.printBase64Binary(signData);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) {
      throw new IllegalArgumentException(e.toString());
    }
  }
}
@@ -56,105 +56,41 @@
 ort 
-javax.crypto.Mac;%0Aimport javax.crypto.spec.SecretKeySpec;%0Aimport javax.xml.bind.DatatypeConverter
+com.google.common.io.BaseEncoding
 ;%0Aim
@@ -220,16 +220,81 @@
 ception;
+%0Aimport javax.crypto.Mac;%0Aimport javax.crypto.spec.SecretKeySpec;
 %0A%0A/**%0A *
@@ -797,43 +797,36 @@
 urn 
-DatatypeConverter.printBase64Binary
+BaseEncoding.base64().encode
 (sig
@@ -996,8 +996,9 @@
  %7D%0A  %7D%0A%7D
+%0A

use guava base64 instead of javax.xml.bind.DatatypeConverter to support java 11