I'm working on a program that authorizes a user of a server to access it with a generated key, and I'm currently working on the generation part. The owner of the server can specify how many keys, the server ID, and the duration of the keys. I need to store and check if the key matches the serverID so users can use the keys to gain access.
The only thing I could think of was a complicated HashMap combination with another HashMap inside, with an List inside of that HashMap. The first String is the server ID which is unique to each server, the List is for the batch of generated keys, and the final String value is for the duration of those keys.
static HashMap<List<String>, String> keyTimeBundle = new HashMap<>();
static List<String> generatedKeys = new ArrayList<>();
static HashMap<String, HashMap<List<String>, String>> clientGeneratedKeys = new HashMap<>();
The problem is that I don't have any idea where to start when checking if the key goes with the matching server ID because the key is in another HashMap inside a List. I'm sure there's another easier way to implement this but I haven't thought of it yet so feel free to not use my method to explain how to store and check the values.
Very simple different approach would be use HashMap<String,String>
only.
You can take key as
<serverId><delimiter(such as '#')><generatedKey>
and value as the duration of that key in that particular server.
While searching you just concatenate serverId and key and just check the duration.