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.
16 private final byte[] routingKey;
17 private final byte[] cryptoKey;
18 private final byte[] extra;
20 private Key(byte[] routingKey, byte[] cryptoKey, byte[] extra) {
21 this.routingKey = routingKey;
22 this.cryptoKey = cryptoKey;
27 public String getRoutingKey() {
28 return encode(routingKey);
32 public String getCryptoKey() {
33 return encode(cryptoKey);
37 public String getExtra() {
41 public FreenetURI toUsk(String docName, long edition, String... paths) {
42 return new FreenetURI("USK", docName, paths, routingKey, cryptoKey,
46 public FreenetURI toSsk(String docName, String... paths) {
47 return new FreenetURI("SSK", docName, paths, routingKey, cryptoKey,
51 public FreenetURI toSsk(String docName, long edition, String... paths) {
52 return new FreenetURI("SSK", format("%s-%d", docName, edition), paths,
53 routingKey, cryptoKey, extra, edition);
56 public static Key from(FreenetURI freenetURI) {
57 return new Key(freenetURI.getRoutingKey(), freenetURI.getCryptoKey(),
58 freenetURI.getExtra());
61 public static String routingKey(FreenetURI freenetURI) {
62 return from(freenetURI).getRoutingKey();