Skip to content

Commit

Permalink
feat: id gen comparison test
Browse files Browse the repository at this point in the history
  • Loading branch information
querwurzel committed Dec 15, 2024
1 parent 2805fb6 commit 5671ed5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
public final class IdGenerator {

public static String randomAlphaNumericalId() {
return randomAlphaNumericalId(UUID.randomUUID().toString().getBytes());
}

public static String randomAlphaNumericalId(byte[] bytes) {
try {
final MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.update(UUID.randomUUID().toString().getBytes());
crypt.update(bytes);
return new BigInteger(1, crypt.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.binpastes.util;

import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

class IdGeneratorTest {

@Test
void sha1Hex() {
var uuidv4 = UUID.fromString("9b1482c6-5310-4eef-b91d-75220eb2bef7");
var id = IdGenerator.randomAlphaNumericalId(uuidv4.toString().getBytes());

assertThat(id).isEqualTo("7870235b55ee013737d015c756c02f5efa6ba903");
}
}

0 comments on commit 5671ed5

Please sign in to comment.