fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.security.InvalidKeyException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.Base64;
  7.  
  8. import javax.crypto.Mac;
  9. import javax.crypto.spec.SecretKeySpec;
  10.  
  11. public class Main {
  12. public static void main(String[] args) throws Exception {
  13. String algo = "HMacSHA1";
  14. String keySource = "qwertyuiopasdfghjklz";
  15. String str = "hogehoge";
  16.  
  17. final SecretKeySpec keySpec = new SecretKeySpec(keySource.getBytes(), algo);
  18. final Mac mac = Mac.getInstance(algo);
  19. mac.init(keySpec);
  20. final byte[] signBytes = mac.doFinal(str.getBytes());
  21. String encoded = Base64.getEncoder().encodeToString(signBytes);
  22. System.out.print(encoded);
  23. }
  24. }
Success #stdin #stdout 0.17s 40124KB
stdin
Standard input is empty
stdout
YKJZs5xPTHmotKAw4/tzwciAyis=