X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FKey.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FKey.java;h=f21e2f60891d0de75eaef8dc46d340f4abc13525;hb=fdffe49b10613e1d9caacdf62ab99bca06edf3e1;hp=0000000000000000000000000000000000000000;hpb=f67d0cbb1bc328c0072fe5f795ffb31cf1b987bb;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/freenet/Key.java b/src/main/java/net/pterodactylus/sone/freenet/Key.java new file mode 100644 index 0000000..f21e2f6 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/Key.java @@ -0,0 +1,67 @@ +package net.pterodactylus.sone.freenet; + +import static freenet.support.Base64.encode; +import static java.lang.String.format; + +import freenet.keys.FreenetURI; + +import com.google.common.annotations.VisibleForTesting; + +/** + * Encapsulates the parts of a {@link FreenetURI} that do not change while + * being converted from SSK to USK and/or back. + * + * @author David ‘Bombe’ Roden + */ +public class Key { + + private final byte[] routingKey; + private final byte[] cryptoKey; + private final byte[] extra; + + private Key(byte[] routingKey, byte[] cryptoKey, byte[] extra) { + this.routingKey = routingKey; + this.cryptoKey = cryptoKey; + this.extra = extra; + } + + @VisibleForTesting + public String getRoutingKey() { + return encode(routingKey); + } + + @VisibleForTesting + public String getCryptoKey() { + return encode(cryptoKey); + } + + @VisibleForTesting + public String getExtra() { + return encode(extra); + } + + public FreenetURI toUsk(String docName, long edition, String... paths) { + return new FreenetURI("USK", docName, paths, routingKey, cryptoKey, + extra, edition); + } + + public FreenetURI toSsk(String docName, String... paths) { + return new FreenetURI("SSK", docName, paths, routingKey, cryptoKey, + extra); + } + + public FreenetURI toSsk(String docName, long edition, String... paths) { + return new FreenetURI("SSK", format("%s-%d", docName, edition), paths, + routingKey, cryptoKey, extra, edition); + } + + public static Key from(FreenetURI freenetURI) { + return new Key(freenetURI.getRoutingKey(), freenetURI.getCryptoKey(), + freenetURI.getExtra()); + } + + public static String routingKey(FreenetURI freenetURI) { + return from(freenetURI).getRoutingKey(); + } + +}