9c97d8b7319e059cd66c5aef638ed2d25d1bf477.svn-base 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package io.agora.media;
  2. import java.io.ByteArrayOutputStream;
  3. public class DynamicKey4 {
  4. private static final String PUBLIC_SHARING_SERVICE = "APSS";
  5. private static final String RECORDING_SERVICE = "ARS";
  6. private static final String MEDIA_CHANNEL_SERVICE = "ACS";
  7. /**
  8. * Generate Dynamic Key for Public Sharing Service
  9. * @param appID App IDassigned by Agora
  10. * @param appCertificate App Certificate assigned by Agora
  11. * @param channelName name of channel to join, limited to 64 bytes and should be printable ASCII characters
  12. * @param unixTs unix timestamp in seconds when generating the Dynamic Key
  13. * @param randomInt salt for generating dynamic key
  14. * @param uid user id, range from 0 - max uint32
  15. * @param expiredTs should be 0
  16. * @return String representation of dynamic key
  17. * @throws Exception if any error occurs
  18. */
  19. public static String generatePublicSharingKey(String appID, String appCertificate, String channelName, int unixTs, int randomInt, long uid, int expiredTs) throws Exception {
  20. return doGenerate(appID, appCertificate, channelName, unixTs, randomInt, uid, expiredTs, PUBLIC_SHARING_SERVICE);
  21. }
  22. /**
  23. * Generate Dynamic Key for recording service
  24. * @param appID Vendor key assigned by Agora
  25. * @param appCertificate Sign key assigned by Agora
  26. * @param channelName name of channel to join, limited to 64 bytes and should be printable ASCII characters
  27. * @param unixTs unix timestamp in seconds when generating the Dynamic Key
  28. * @param randomInt salt for generating dynamic key
  29. * @param uid user id, range from 0 - max uint32
  30. * @param expiredTs should be 0
  31. * @return String representation of dynamic key
  32. * @throws Exception if any error occurs
  33. */
  34. public static String generateRecordingKey(String appID, String appCertificate, String channelName, int unixTs, int randomInt, long uid, int expiredTs) throws Exception {
  35. return doGenerate(appID, appCertificate, channelName, unixTs, randomInt, uid, expiredTs, RECORDING_SERVICE);
  36. }
  37. /**
  38. * Generate Dynamic Key for media channel service
  39. * @param appID Vendor key assigned by Agora
  40. * @param appCertificate Sign key assigned by Agora
  41. * @param channelName name of channel to join, limited to 64 bytes and should be printable ASCII characters
  42. * @param unixTs unix timestamp in seconds when generating the Dynamic Key
  43. * @param randomInt salt for generating dynamic key
  44. * @param uid user id, range from 0 - max uint32
  45. * @param expiredTs service expiring timestamp. After this timestamp, user will not be able to stay in the channel.
  46. * @return String representation of dynamic key
  47. * @throws Exception if any error occurs
  48. */
  49. public static String generateMediaChannelKey(String appID, String appCertificate, String channelName, int unixTs, int randomInt, long uid, int expiredTs) throws Exception {
  50. return doGenerate(appID, appCertificate, channelName, unixTs, randomInt, uid, expiredTs, MEDIA_CHANNEL_SERVICE);
  51. }
  52. private static String doGenerate(String appID, String appCertificate, String channelName, int unixTs, int randomInt, long uid, int expiredTs, String serviceType) throws Exception {
  53. String version = "004";
  54. String unixTsStr = ("0000000000" + Integer.toString(unixTs)).substring(Integer.toString(unixTs).length());
  55. String randomIntStr = ("00000000" + Integer.toHexString(randomInt)).substring(Integer.toHexString(randomInt).length());
  56. uid = uid & 0xFFFFFFFFL;
  57. String uidStr = ("0000000000" + Long.toString(uid)).substring(Long.toString(uid).length());
  58. String expiredTsStr = ("0000000000" + Integer.toString(expiredTs)).substring(Integer.toString(expiredTs).length());
  59. String signature = generateSignature4(appID, appCertificate, channelName, unixTsStr, randomIntStr, uidStr, expiredTsStr, serviceType);
  60. return String.format("%s%s%s%s%s%s", version, signature, appID, unixTsStr, randomIntStr, expiredTsStr);
  61. }
  62. private static String generateSignature4(String appID, String appCertificate, String channelName, String unixTsStr, String randomIntStr, String uidStr, String expiredTsStr, String serviceType) throws Exception {
  63. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64. baos.write(serviceType.getBytes());
  65. baos.write(appID.getBytes());
  66. baos.write(unixTsStr.getBytes());
  67. baos.write(randomIntStr.getBytes());
  68. baos.write(channelName.getBytes());
  69. baos.write(uidStr.getBytes());
  70. baos.write(expiredTsStr.getBytes());
  71. byte[] sign = DynamicKeyUtil.encodeHMAC(appCertificate, baos.toByteArray());
  72. return DynamicKeyUtil.bytesToHex(sign);
  73. }
  74. }