package io.agora.media; import org.apache.commons.codec.binary.Base64; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.ByteArrayOutputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Date; import java.util.zip.CRC32; import java.util.zip.Deflater; import java.util.zip.Inflater; public class Utils { public static final long HMAC_SHA256_LENGTH = 32; public static final int VERSION_LENGTH = 3; public static final int APP_ID_LENGTH = 32; public static byte[] hmacSign(String keyString, byte[] msg) throws InvalidKeyException, NoSuchAlgorithmException { SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); return mac.doFinal(msg); } public static byte[] pack(PackableEx packableEx) { ByteBuf buffer = new ByteBuf(); packableEx.marshal(buffer); return buffer.asBytes(); } public static void unpack(byte[] data, PackableEx packableEx) { ByteBuf buffer = new ByteBuf(data); packableEx.unmarshal(buffer); } public static String base64Encode(byte[] data) { byte[] encodedBytes = Base64.encodeBase64(data); return new String(encodedBytes); } public static byte[] base64Decode(String data) { return Base64.decodeBase64(data.getBytes()); } public static int crc32(String data) { // get bytes from string byte[] bytes = data.getBytes(); return crc32(bytes); } public static int crc32(byte[] bytes) { CRC32 checksum = new CRC32(); checksum.update(bytes); return (int)checksum.getValue(); } public static int getTimestamp() { return (int)((new Date().getTime())/1000); } public static int randomInt() { return new SecureRandom().nextInt(); } public static boolean isUUID(String uuid) { if (uuid.length() != 32) { return false; } return uuid.matches("\\p{XDigit}+"); } public static byte[] compress(byte[] data) { byte[] output; Deflater deflater = new Deflater(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); try { deflater.reset(); deflater.setInput(data); deflater.finish(); byte[] buf = new byte[data.length]; while (!deflater.finished()) { int i = deflater.deflate(buf); bos.write(buf, 0, i); } output = bos.toByteArray(); } catch (Exception e) { output = data; e.printStackTrace(); } finally { deflater.end(); } return output; } public static byte[] decompress(byte[] data) { Inflater inflater = new Inflater(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); try { inflater.setInput(data); byte[] buf = new byte[data.length]; while (!inflater.finished()) { int i = inflater.inflate(buf); bos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { inflater.end(); } return bos.toByteArray(); } }