Skip to content

Commit

Permalink
feat: started client
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Jun 4, 2023
1 parent d356bd7 commit cd8bc8e
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.rethinkdb/rethinkdb-driver -->
<dependency>
Expand All @@ -60,6 +65,15 @@
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/me/vinceh121/mobilitymock/client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package me.vinceh121.mobilitymock.client;

import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.cli.CLI;
import io.vertx.core.cli.CommandLine;
import io.vertx.core.cli.Option;
import io.vertx.ext.web.client.WebClient;

public class Client {
private static final Pattern PAT_UUID
= Pattern.compile("[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}");
private final Vertx vertx;
private final WebClient client;
private byte[] salt;

public Client() {
this.vertx = Vertx.vertx();
this.client = WebClient.create(vertx);

this.salt = new byte[32];
new SecureRandom().nextBytes(salt);
}

public void start(List<String> args) {
CLI cli = CLI.create("client")
.addOption(new Option().setLongName("fetch").setFlag(true))
.addOption(new Option().setLongName("anonymize").setFlag(true))
.addOption(new Option().setLongName("help").setShortName("h").setHelp(true));
CommandLine cl = cli.parse(args);

if (!cl.isValid() || cl.isAskingForHelp()) {
StringBuilder sb = new StringBuilder();
cli.usage(sb);
System.out.println(sb);
return;
}

@SuppressWarnings("rawtypes") // god damnit vertx!
List<Future> futures = new ArrayList<>(2);

if (cl.isFlagEnabled("fetch")) {
futures.add(this.fetch());
}
if (cl.isFlagEnabled("anonymize")) {
futures.add(this.anonymize());
}

CompositeFuture.all(futures).onSuccess(f -> System.out.println("Done!")).onFailure(System.out::println);
}

private Future<Void> fetch() {
return Future.future(p -> {

});
}

private Future<Void> anonymize() {
return Future.future(p -> {

});
}

public byte[] getSalt() {
return salt;
}

public void setSalt(byte[] salt) {
this.salt = salt;
}

public Vertx getVertx() {
return vertx;
}

public WebClient getClient() {
return client;
}

public static String anonymizeUuids(String id, byte[] salt) {
StringBuilder sb = new StringBuilder(id);
Matcher m = PAT_UUID.matcher(id);
while (m.find()) {
sb.replace(m.start(), m.end(), anonymizeUuid(id, salt));
}
return sb.toString();
}

public static String anonymizeUuid(String id, byte[] salt) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(salt);
LongBuffer buf = ByteBuffer.wrap(digest.digest(id.getBytes())).asLongBuffer();
UUID hashedUuid = new UUID(buf.get(), buf.get());
return hashedUuid.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
31 changes: 31 additions & 0 deletions src/test/java/me/vinceh121/mobilitymock/TestAnon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.vinceh121.mobilitymock;

import static org.junit.jupiter.api.Assertions.*;

import java.security.SecureRandom;

import org.junit.jupiter.api.Test;

import me.vinceh121.mobilitymock.client.Client;

class TestAnon {

@Test
void testUuid() {
byte[] salt = new byte[32];
new SecureRandom(new byte[0]).nextBytes(salt);

assertTrue(Client.anonymizeUuids("SKO-E-7bac9e0c-d05d-404c-8199-eb492d535d29", salt).startsWith("SKO-E-"));
assertTrue(Client.anonymizeUuids("TCTU-SKO-E-80da98b3-0955-4140-b8fc-2cb50db62dbb", salt)
.startsWith("TCTU-SKO-E-"));
assertTrue(Client.anonymizeUuids("PSKO-P-80da98b3-0955-4140-b8fc-2cb50db62dbb", salt).startsWith("PSKO-P-"));

String uuid = Client.anonymizeUuids(
"SKO-E-d46c3d62-2581-4097-b561-8833ec8394e8-47135-author-ASKO-P-ecbc4435-cd86-45aa-92b5-d07f5b8d4c6c",
salt);
assertTrue(uuid.startsWith("SKO-E-") && uuid.contains("-author-ASKO-P-"));
assertTrue(Client.anonymizeUuids(
"SKO-E-d46c3d62-2581-4097-b561-8833ec8394e8-47135-author-ASKO-P-ecbc4435-cd86-45aa-92b5-d07f5b8d4c6c",
salt).startsWith("SKO-E-"));
}
}

0 comments on commit cd8bc8e

Please sign in to comment.