Skip to content

Commit

Permalink
feat(Utilities): Generate UUID from String seed
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed May 4, 2023
1 parent 7eb5d14 commit fec41e1
Showing 1 changed file with 25 additions and 0 deletions.
@@ -1,5 +1,6 @@
package com.georgev22.skinoverlay.utilities;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -13,13 +14,37 @@
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.UUID;

public class Utilities {

/**
* Generates a deterministic UUID from a given seed using the SHA-256 hash function.
*
* @param seed the input seed used to generate the UUID
* @return a UUID generated from the seed
* @throws NoSuchAlgorithmException if SHA-256 is not a supported message digest algorithm
*/
@Contract("_ -> new")
public static @NotNull UUID generateUUID(@NotNull String seed) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(seed.getBytes(StandardCharsets.UTF_8));
byte[] hash = md.digest();
long msb = 0;
long lsb = 0;
for (int i = 0; i < 8; i++)
msb = (msb << 8) | (hash[i] & 0xff);
for (int i = 8; i < 16; i++)
lsb = (lsb << 8) | (hash[i] & 0xff);
return new UUID(msb, lsb);
}

/**
* Decodes the specified Base64-encoded string into an object.
*
Expand Down

0 comments on commit fec41e1

Please sign in to comment.