2648f02cb0fc9b3961902922251d92a6e2d94766.svn-base 1003 B

123456789101112131415161718192021222324252627282930313233
  1. package io.agora.media;
  2. import javax.crypto.Mac;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.security.InvalidKeyException;
  5. import java.security.NoSuchAlgorithmException;
  6. /**
  7. * Created by hefeng on 15/8/10.
  8. * Util to generate Agora media dynamic key.
  9. */
  10. public class DynamicKeyUtil {
  11. static byte[] encodeHMAC(String key, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
  12. return encodeHMAC(key.getBytes(), message);
  13. }
  14. static byte[] encodeHMAC(byte[] key, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
  15. SecretKeySpec keySpec = new SecretKeySpec(key, "HmacSHA1");
  16. Mac mac = Mac.getInstance("HmacSHA1");
  17. mac.init(keySpec);
  18. return mac.doFinal(message);
  19. }
  20. static String bytesToHex(byte[] in) {
  21. final StringBuilder builder = new StringBuilder();
  22. for (byte b : in) {
  23. builder.append(String.format("%02x", b));
  24. }
  25. return builder.toString();
  26. }
  27. }