@@ -50,7 +50,9 @@ class Utils {
50
50
@ NonNull
51
51
public static String createRandomFid () {
52
52
// 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 );
54
56
return encodeFidBase64UrlSafe (uuidBytes );
55
57
}
56
58
@@ -75,21 +77,10 @@ private static String encodeFidBase64UrlSafe(byte[] rawValue) {
75
77
.substring (0 , FID_LENGTH );
76
78
}
77
79
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 );
90
82
bb .putLong (uuid .getMostSignificantBits ());
91
83
bb .putLong (uuid .getLeastSignificantBits ());
92
-
93
84
return bb .array ();
94
85
}
95
86
}
0 commit comments