fc5c9867c86fa0e31b49648dc74a25b44c7f16e6.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package io.agora.media;
  2. import org.apache.commons.codec.binary.Base64;
  3. import org.apache.commons.codec.binary.Hex;
  4. import java.util.TreeMap;
  5. /**
  6. * Created by Li on 10/1/2016.
  7. */
  8. public class DynamicKey5 {
  9. public final static String version = "005";
  10. public final static String noUpload = "0";
  11. public final static String audioVideoUpload = "3";
  12. // ServiceType
  13. public final static short MEDIA_CHANNEL_SERVICE = 1;
  14. public final static short RECORDING_SERVICE = 2;
  15. public final static short PUBLIC_SHARING_SERVICE = 3;
  16. public final static short IN_CHANNEL_PERMISSION = 4;
  17. // InChannelPermissionKey
  18. public final static short ALLOW_UPLOAD_IN_CHANNEL = 1;
  19. public DynamicKey5Content content;
  20. public boolean fromString(String key) {
  21. if (! key.substring(0, 3).equals(version)) {
  22. return false;
  23. }
  24. byte[] rawContent = new Base64().decode(key.substring(3));
  25. if (rawContent.length == 0) {
  26. return false;
  27. }
  28. content = new DynamicKey5Content();
  29. ByteBuf buffer = new ByteBuf(rawContent);
  30. content.unmarshall(buffer);
  31. return true;
  32. }
  33. public static String generateSignature(String appCertificate, short service, String appID, int unixTs, int salt, String channelName, long uid, int expiredTs, TreeMap<Short, String> extra) throws Exception {
  34. // decode hex to avoid case problem
  35. Hex hex = new Hex();
  36. byte[] rawAppID = hex.decode(appID.getBytes());
  37. byte[] rawAppCertificate = hex.decode(appCertificate.getBytes());
  38. Message m = new Message(service, rawAppID, unixTs, salt, channelName, (int)(uid & 0xFFFFFFFFL), expiredTs, extra);
  39. byte[] toSign = pack(m);
  40. return new String(Hex.encodeHex(DynamicKeyUtil.encodeHMAC(rawAppCertificate, toSign), false));
  41. }
  42. public static String generateDynamicKey(String appID, String appCertificate, String channel, int ts, int salt, long uid, int expiredTs, TreeMap<Short, String> extra, short service) throws Exception {
  43. String signature = generateSignature(appCertificate, service, appID, ts, salt, channel, uid, expiredTs, extra);
  44. DynamicKey5Content content = new DynamicKey5Content(service, signature, new Hex().decode(appID.getBytes()), ts, salt, expiredTs, extra);
  45. byte[] bytes = pack(content);
  46. byte[] encoded = new Base64().encode(bytes);
  47. String base64 = new String(encoded);
  48. return version + base64;
  49. }
  50. private static byte[] pack(Packable content) {
  51. ByteBuf buffer = new ByteBuf();
  52. content.marshal(buffer);
  53. return buffer.asBytes();
  54. }
  55. public static String generatePublicSharingKey(String appID, String appCertificate, String channel, int ts, int salt, long uid, int expiredTs) throws Exception {
  56. return generateDynamicKey(appID, appCertificate, channel, ts, salt, uid, expiredTs, new TreeMap<Short, String>(), PUBLIC_SHARING_SERVICE);
  57. }
  58. public static String generateRecordingKey(String appID, String appCertificate, String channel, int ts, int salt, long uid, int expiredTs) throws Exception {
  59. return generateDynamicKey(appID, appCertificate, channel, ts, salt, uid, expiredTs, new TreeMap<Short, String>(), RECORDING_SERVICE);
  60. }
  61. public static String generateMediaChannelKey(String appID, String appCertificate, String channel, int ts, int salt, long uid, int expiredTs) throws Exception {
  62. return generateDynamicKey(appID, appCertificate, channel, ts, salt, uid, expiredTs, new TreeMap<Short, String>(), MEDIA_CHANNEL_SERVICE);
  63. }
  64. public static String generateInChannelPermissionKey(String appID, String appCertificate, String channel, int ts, int salt, long uid, int expiredTs, String permission) throws Exception {
  65. TreeMap<Short, String> extra = new TreeMap<Short, String>();
  66. extra.put(ALLOW_UPLOAD_IN_CHANNEL, permission);
  67. return generateDynamicKey(appID, appCertificate, channel, ts, salt, uid, expiredTs, extra, IN_CHANNEL_PERMISSION);
  68. }
  69. static class Message implements Packable {
  70. public short serviceType;
  71. public byte[] appID;
  72. public int unixTs;
  73. public int salt;
  74. public String channelName;
  75. public int uid;
  76. public int expiredTs;
  77. public TreeMap<Short, String> extra;
  78. public Message(short serviceType, byte[] appID, int unixTs, int salt, String channelName, int uid, int expiredTs, TreeMap<Short, String> extra) {
  79. this.serviceType = serviceType;
  80. this.appID = appID;
  81. this.unixTs = unixTs;
  82. this.salt = salt;
  83. this.channelName = channelName;
  84. this.uid = uid;
  85. this.expiredTs = expiredTs;
  86. this.extra = extra;
  87. }
  88. public ByteBuf marshal(ByteBuf out) {
  89. return out.put(serviceType).put(appID).put(unixTs).put(salt).put(channelName).put(uid).put(expiredTs).put(extra);
  90. }
  91. }
  92. public static class DynamicKey5Content implements Packable {
  93. public short serviceType;
  94. public String signature;
  95. public byte[] appID;
  96. public int unixTs;
  97. public int salt;
  98. public int expiredTs;
  99. public TreeMap<Short, String> extra;
  100. public DynamicKey5Content() {
  101. }
  102. public DynamicKey5Content(short serviceType, String signature, byte[] appID, int unixTs, int salt, int expiredTs, TreeMap<Short, String> extra) {
  103. this.serviceType = serviceType;
  104. this.signature = signature;
  105. this.appID = appID;
  106. this.unixTs = unixTs;
  107. this.salt = salt;
  108. this.expiredTs = expiredTs;
  109. this.extra = extra;
  110. }
  111. public ByteBuf marshal(ByteBuf out) {
  112. return out.put(serviceType).put(signature).put(appID).put(unixTs).put(salt).put(expiredTs).put(extra);
  113. }
  114. public void unmarshall(ByteBuf in) {
  115. this.serviceType = in.readShort();
  116. this.signature = in.readString();
  117. this.appID = in.readBytes();
  118. this.unixTs = in.readInt();
  119. this.salt = in.readInt();
  120. this.expiredTs = in.readInt();
  121. this.extra = in.readMap();
  122. }
  123. }
  124. }