Skip to content

Commit f4c44b0

Browse files
committed
Updating createRandomFid as per Rayo's comments.
1 parent 34848c6 commit f4c44b0

File tree

1 file changed

+5
-14
lines changed
  • firebase-installations/src/main/java/com/google/firebase/installations

1 file changed

+5
-14
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/Utils.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class Utils {
5050
@NonNull
5151
public static String createRandomFid() {
5252
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
53-
byte[] uuidBytes = getBytesFromUUID(UUID.randomUUID());
53+
byte[] uuidBytes = getBytesFromUUID(UUID.randomUUID(), new byte[17]);
54+
uuidBytes[16] = uuidBytes[0];
55+
uuidBytes[0] = (byte) ((REMOVE_PREFIX_MASK & uuidBytes[0]) | FID_4BIT_PREFIX);
5456
return encodeFidBase64UrlSafe(uuidBytes);
5557
}
5658

@@ -75,21 +77,10 @@ private static String encodeFidBase64UrlSafe(byte[] rawValue) {
7577
.substring(0, FID_LENGTH);
7678
}
7779

78-
private static byte[] getBytesFromUUID(UUID uuid) {
79-
ByteBuffer bb = ByteBuffer.wrap(new byte[17]);
80-
81-
// The first 4 bits with the constant FID header of 0x7 (0b0111) followed by the last 4 random
82-
// bits of the UUID.
83-
byte fidPrefixWithLast4bitsOfUUID =
84-
(byte)
85-
(FID_4BIT_PREFIX
86-
| (ByteBuffer.allocate(8).putLong(uuid.getLeastSignificantBits()).array()[7]
87-
& REMOVE_PREFIX_MASK));
88-
89-
bb.put(fidPrefixWithLast4bitsOfUUID);
80+
private static byte[] getBytesFromUUID(UUID uuid, byte[] output) {
81+
ByteBuffer bb = ByteBuffer.wrap(output);
9082
bb.putLong(uuid.getMostSignificantBits());
9183
bb.putLong(uuid.getLeastSignificantBits());
92-
9384
return bb.array();
9485
}
9586
}

0 commit comments

Comments
 (0)