-
-
Save franreyes/6ffc86823ffc092a1ee7541157f3a145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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