]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/GroupId.java
34e18e8e972150f4534abb66d41d9dc946543aec
[signal-cli] / src / main / java / org / asamk / signal / manager / GroupId.java
1 package org.asamk.signal.manager;
2
3 import org.whispersystems.util.Base64;
4
5 import java.util.Arrays;
6
7 public abstract class GroupId {
8
9 private final byte[] id;
10
11 public static GroupIdV1 v1(byte[] id) {
12 return new GroupIdV1(id);
13 }
14
15 public static GroupIdV2 v2(byte[] id) {
16 return new GroupIdV2(id);
17 }
18
19 public static GroupId unknownVersion(byte[] id) {
20 if (id.length == 16) {
21 return new GroupIdV1(id);
22 } else if (id.length == 32) {
23 return new GroupIdV2(id);
24 }
25
26 throw new AssertionError("Invalid group id of size " + id.length);
27 }
28
29 public static GroupId fromBase64(String id) throws GroupIdFormatException {
30 try {
31 return unknownVersion(java.util.Base64.getDecoder().decode(id));
32 } catch (Throwable e) {
33 throw new GroupIdFormatException(id, e);
34 }
35 }
36
37 public GroupId(final byte[] id) {
38 this.id = id;
39 }
40
41 public byte[] serialize() {
42 return id;
43 }
44
45 public String toBase64() {
46 return Base64.encodeBytes(id);
47 }
48
49 @Override
50 public boolean equals(final Object o) {
51 if (this == o) return true;
52 if (o == null || getClass() != o.getClass()) return false;
53
54 final GroupId groupId = (GroupId) o;
55
56 return Arrays.equals(id, groupId.id);
57 }
58
59 @Override
60 public int hashCode() {
61 return Arrays.hashCode(id);
62 }
63 }