Skip to content

Instantly share code, notes, and snippets.

@franreyes
Created May 16, 2022 12:31
Show Gist options
  • Save franreyes/6ffc86823ffc092a1ee7541157f3a145 to your computer and use it in GitHub Desktop.
Save franreyes/6ffc86823ffc092a1ee7541157f3a145 to your computer and use it in GitHub Desktop.
package resem.util.url;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ProppitIdCypher {
List<Integer> blockOrder = Arrays.asList(1, 3, 0, 4, 2);
public String cypher(String id) {
try {
List<String> cypheredId = Arrays.stream(id.split("-"))
.map(x -> Long.parseLong(x, 16))
.map(x -> x + 42)
.map(Long::toHexString)
.collect(Collectors.toList());
return blockOrder.stream().map(cypheredId::get).collect(Collectors.joining("-"));
} catch (NumberFormatException e) {
throw new ProppitIdMalformedException(e);
}
}
public String decypher(String id) {
try {
Long[] decypheredId = Arrays.stream(id.split("-"))
.map(x -> Long.parseLong(x, 16))
.map(x -> x - 42)
.toArray(Long[]::new);
List<Integer> blockReverseOrder = (IntStream.range(0, blockOrder.size())
.map(x -> blockOrder.indexOf(x))).boxed().collect(Collectors.toList());
return String.format("%08x-%04x-%04x-%04x-%012x", blockReverseOrder.stream()
.map(position -> decypheredId[position]).toArray(Long[]::new));
} catch (NumberFormatException e) {
throw new ProppitIdMalformedException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment