1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.rcs.simpleclient.protocol.msrp;
18 
19 import com.android.libraries.rcs.simpleclient.protocol.sip.SipUtils;
20 import java.security.SecureRandom;
21 
22 /** Collections of utility functions for MSRP */
23 public final class MsrpUtils {
24 
25     private static final SecureRandom random = new SecureRandom();
26 
MsrpUtils()27     private MsrpUtils() {
28     }
29 
30     /** Generate a path attribute defined in RFC 4975 for the given address, port. */
generatePath(String address, int port, boolean isSecure)31     public static String generatePath(String address, int port, boolean isSecure) {
32         StringBuilder builder = new StringBuilder();
33 
34         if (SipUtils.isIPv6Address(address)) {
35             address = "[" + address + "]";
36         }
37 
38         builder
39                 .append(isSecure ? "msrps" : "msrp")
40                 .append("://")
41                 .append(address)
42                 .append(":")
43                 .append(port)
44                 .append("/")
45                 .append(System.currentTimeMillis())
46                 .append(";tcp");
47 
48         return builder.toString();
49     }
50 
generateRandomId()51     public static String generateRandomId() {
52         byte[] randomBytes = new byte[8];
53         random.nextBytes(randomBytes);
54         String hex = "";
55         for (byte b : randomBytes) {
56             hex = hex + String.format("%02x", b);
57         }
58         return hex;
59     }
60 }
61