1 package net.pterodactylus.sone.freenet;
3 import static freenet.support.Base64.encode;
4 import static java.lang.String.format;
6 import freenet.keys.FreenetURI;
8 import com.google.common.annotations.VisibleForTesting;
11 * Encapsulates the parts of a {@link FreenetURI} that do not change while
12 * being converted from SSK to USK and/or back.
14 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
18 private final byte[] routingKey;
19 private final byte[] cryptoKey;
20 private final byte[] extra;
22 private Key(byte[] routingKey, byte[] cryptoKey, byte[] extra) {
23 this.routingKey = routingKey;
24 this.cryptoKey = cryptoKey;
29 public String getRoutingKey() {
30 return encode(routingKey);
34 public String getCryptoKey() {
35 return encode(cryptoKey);
39 public String getExtra() {
43 public FreenetURI toUsk(String docName, long edition, String... paths) {
44 return new FreenetURI("USK", docName, paths, routingKey, cryptoKey,
48 public FreenetURI toSsk(String docName, String... paths) {
49 return new FreenetURI("SSK", docName, paths, routingKey, cryptoKey,
53 public FreenetURI toSsk(String docName, long edition, String... paths) {
54 return new FreenetURI("SSK", format("%s-%d", docName, edition), paths,
55 routingKey, cryptoKey, extra, edition);
58 public static Key from(FreenetURI freenetURI) {
59 return new Key(freenetURI.getRoutingKey(), freenetURI.getCryptoKey(),
60 freenetURI.getExtra());
63 public static String routingKey(FreenetURI freenetURI) {
64 return from(freenetURI).getRoutingKey();