Initial Python rewrite

This commit is contained in:
2026-02-19 17:50:14 +00:00
parent da95a64fb7
commit faa04a0d01
158 changed files with 5122 additions and 1144 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
* Your use of it is subject to your agreement with Google.
*/
package com.example.util;
import java.util.UUID;
import java.util.Base64;
/**
* A utility class for generating consistent and formatted session IDs.
* Centralizing ID generation ensures all parts of the application use the same
* logic and format.
*/
public final class SessionIdGenerator {
// Private constructor to prevent instantiation of the utility class.
private SessionIdGenerator() {}
/**
* Generates a standard, version 4 (random) UUID as a string.
* This is the most common and robust approach for general-purpose unique IDs.
* The UUID is a 36-character string with hyphens (e.g., "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
*
* @return a new, randomly generated UUID string.
*/
public static String generateStandardSessionId() {
return UUID.randomUUID().toString();
}
/**
* Generates a more compact session ID by removing the hyphens from a standard UUID.
* This is useful for contexts where a shorter or URL-friendly ID is needed.
*
* @return a 32-character UUID string without hyphens.
*/
public static String generateCompactSessionId() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* Generates a base64-encoded, URL-safe session ID from a UUID.
* This provides a very compact, yet robust, representation of the UUID.
* It's ideal for use in URLs, cookies, or other contexts where size matters.
*
* @return a new, base64-encoded UUID string.
*/
public static String generateUrlSafeSessionId() {
UUID uuid = UUID.randomUUID();
byte[] uuidBytes = toBytes(uuid);
return Base64.getUrlEncoder().withoutPadding().encodeToString(uuidBytes);
}
// Helper method to convert UUID to a byte array
private static byte[] toBytes(UUID uuid) {
long mostSignificantBits = uuid.getMostSignificantBits();
long leastSignificantBits = uuid.getLeastSignificantBits();
byte[] bytes = new byte[16];
for (int i = 0; i < 8; i++) {
bytes[i] = (byte) (mostSignificantBits >>> (8 * (7 - i)));
}
for (int i = 0; i < 8; i++) {
bytes[8 + i] = (byte) (leastSignificantBits >>> (8 * (7 - i)));
}
return bytes;
}
}