4c3dd2f383ac9b3f715c8333f493ba870d54aede.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package io.agora.media;
  2. import org.apache.commons.codec.binary.Base64;
  3. import javax.crypto.Mac;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.io.ByteArrayOutputStream;
  6. import java.security.InvalidKeyException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.security.SecureRandom;
  9. import java.util.Date;
  10. import java.util.zip.CRC32;
  11. import java.util.zip.Deflater;
  12. import java.util.zip.Inflater;
  13. public class Utils {
  14. public static final long HMAC_SHA256_LENGTH = 32;
  15. public static final int VERSION_LENGTH = 3;
  16. public static final int APP_ID_LENGTH = 32;
  17. public static byte[] hmacSign(String keyString, byte[] msg) throws InvalidKeyException, NoSuchAlgorithmException {
  18. SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), "HmacSHA256");
  19. Mac mac = Mac.getInstance("HmacSHA256");
  20. mac.init(keySpec);
  21. return mac.doFinal(msg);
  22. }
  23. public static byte[] pack(PackableEx packableEx) {
  24. ByteBuf buffer = new ByteBuf();
  25. packableEx.marshal(buffer);
  26. return buffer.asBytes();
  27. }
  28. public static void unpack(byte[] data, PackableEx packableEx) {
  29. ByteBuf buffer = new ByteBuf(data);
  30. packableEx.unmarshal(buffer);
  31. }
  32. public static String base64Encode(byte[] data) {
  33. byte[] encodedBytes = Base64.encodeBase64(data);
  34. return new String(encodedBytes);
  35. }
  36. public static byte[] base64Decode(String data) {
  37. return Base64.decodeBase64(data.getBytes());
  38. }
  39. public static int crc32(String data) {
  40. // get bytes from string
  41. byte[] bytes = data.getBytes();
  42. return crc32(bytes);
  43. }
  44. public static int crc32(byte[] bytes) {
  45. CRC32 checksum = new CRC32();
  46. checksum.update(bytes);
  47. return (int)checksum.getValue();
  48. }
  49. public static int getTimestamp() {
  50. return (int)((new Date().getTime())/1000);
  51. }
  52. public static int randomInt() {
  53. return new SecureRandom().nextInt();
  54. }
  55. public static boolean isUUID(String uuid) {
  56. if (uuid.length() != 32) {
  57. return false;
  58. }
  59. return uuid.matches("\\p{XDigit}+");
  60. }
  61. public static byte[] compress(byte[] data) {
  62. byte[] output;
  63. Deflater deflater = new Deflater();
  64. ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
  65. try {
  66. deflater.reset();
  67. deflater.setInput(data);
  68. deflater.finish();
  69. byte[] buf = new byte[data.length];
  70. while (!deflater.finished()) {
  71. int i = deflater.deflate(buf);
  72. bos.write(buf, 0, i);
  73. }
  74. output = bos.toByteArray();
  75. } catch (Exception e) {
  76. output = data;
  77. e.printStackTrace();
  78. } finally {
  79. deflater.end();
  80. }
  81. return output;
  82. }
  83. public static byte[] decompress(byte[] data) {
  84. Inflater inflater = new Inflater();
  85. ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
  86. try {
  87. inflater.setInput(data);
  88. byte[] buf = new byte[data.length];
  89. while (!inflater.finished()) {
  90. int i = inflater.inflate(buf);
  91. bos.write(buf, 0, i);
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. } finally {
  96. inflater.end();
  97. }
  98. return bos.toByteArray();
  99. }
  100. }