/** Read the binary form of a key, preceded by a short for its length. */ public static FreenetURI readFullBinaryKeyWithLength(DataInputStream dis) throws IOException { int len = dis.readShort(); byte[] buf = new byte[len]; dis.readFully(buf); Logger.minor(FreenetURI.class, "Read " + len + " bytes for key"); return fromFullBinaryKey(buf); }
/** * Get the FreenetURI as a string. * * @param prefix Whether to include the freenet: prefix. * @param pureAscii If true, encode any non-english characters. If false, only encode dangerous * characters (slashes e.g.). */ public String toString(boolean prefix, boolean pureAscii) { if (keyType == null) { // Not activated or something... Logger.minor(this, "Not activated?? in toString(" + prefix + "," + pureAscii + ")"); return null; } StringBuilder b; if (prefix) b = new StringBuilder("freenet:"); else b = new StringBuilder(); b.append(keyType).append('@'); if (!"KSK".equals(keyType)) { if (routingKey != null) b.append(Base64.encode(routingKey)); if (cryptoKey != null) b.append(',').append(Base64.encode(cryptoKey)); if (extra != null) b.append(',').append(Base64.encode(extra)); if (docName != null) b.append('/'); } if (docName != null) b.append(URLEncoder.encode(docName, "/", pureAscii)); if (keyType.equals("USK")) { b.append('/'); b.append(suggestedEdition); } if (metaStr != null) for (int i = 0; i < metaStr.length; i++) { b.append('/').append(URLEncoder.encode(metaStr[i], "/", pureAscii)); } return b.toString(); }
/** * Write a binary representation of this URI, with a short length, so it can be passed over if * necessary. * * @param dos The stream to write to. * @throws MalformedURLException If the key could not be written because of inconsistencies or * other problems in the key itself. * @throws IOException If an error occurred while writing the key. */ public void writeFullBinaryKeyWithLength(DataOutputStream dos) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream ndos = new DataOutputStream(baos); writeFullBinaryKey(ndos); ndos.close(); byte[] data = baos.toByteArray(); if (data.length > Short.MAX_VALUE) throw new MalformedURLException("Full key too long: " + data.length + " - " + this); dos.writeShort((short) data.length); Logger.minor(this, "Written " + data.length + " bytes"); dos.write(data); }
/** USK constructor from components. */ public FreenetURI( byte[] pubKeyHash, byte[] cryptoKey, byte[] extra, String siteName, long suggestedEdition2) { // this.uniqueHashCode = super.hashCode(); this.keyType = "USK"; this.routingKey = pubKeyHash; this.cryptoKey = cryptoKey; this.extra = extra; this.docName = siteName; this.suggestedEdition = suggestedEdition2; metaStr = null; Logger.minor(this, "Created from components (USK): " + toString(), new Exception("debug")); }
public FreenetURI( String keyType, String docName, String[] metaStr, byte[] routingKey, byte[] cryptoKey, byte[] extra2) { // this.uniqueHashCode = super.hashCode(); this.keyType = keyType.trim().toUpperCase().intern(); this.docName = docName; this.metaStr = metaStr; this.routingKey = routingKey; this.cryptoKey = cryptoKey; this.extra = extra2; this.suggestedEdition = -1; Logger.minor(this, "Created from components: " + toString(), new Exception("debug")); }
/** * Generate a suggested filename for the URI. This may be constructed from more than one part of * the URI e.g. SSK@blah,blah,blah/sitename/ might return sitename. */ public String getPreferredFilename() { Logger.minor(this, "Getting preferred filename for " + this); ArrayList<String> names = new ArrayList<String>(); if (keyType != null && (keyType.equals("KSK") || keyType.equals("SSK") || keyType.equals("USK"))) { Logger.minor(this, "Adding docName: " + docName); names.add(docName); if (keyType.equals("USK")) names.add(Long.toString(suggestedEdition)); } if (metaStr != null) for (int i = 0; i < metaStr.length; i++) { if (metaStr[i] == null || metaStr[i].equals("")) { Logger.minor(this, "metaString " + i + ": was null or empty"); continue; } Logger.minor(this, "Adding metaString " + i + ": " + metaStr[i]); names.add(metaStr[i]); } StringBuilder out = new StringBuilder(); for (int i = 0; i < names.size(); i++) { String s = names.get(i); Logger.minor(this, "name " + i + " = " + s); s = FileUtil.sanitize(s); Logger.minor(this, "Sanitized name " + i + " = " + s); if (s.length() > 0) { if (out.length() > 0) out.append('-'); out.append(s); } } Logger.minor(this, "out = " + out.toString()); if (out.length() == 0) { if (routingKey != null) { Logger.minor(this, "Returning base64 encoded routing key"); return Base64.encode(routingKey); } return "unknown"; } return out.toString(); }
public FreenetURI(FreenetURI uri) { // this.uniqueHashCode = super.hashCode(); keyType = uri.keyType; docName = uri.docName; if (uri.metaStr != null) { metaStr = new String[uri.metaStr.length]; System.arraycopy(uri.metaStr, 0, metaStr, 0, metaStr.length); } else metaStr = null; if (uri.routingKey != null) { routingKey = new byte[uri.routingKey.length]; System.arraycopy(uri.routingKey, 0, routingKey, 0, routingKey.length); } else routingKey = null; if (uri.cryptoKey != null) { cryptoKey = new byte[uri.cryptoKey.length]; System.arraycopy(uri.cryptoKey, 0, cryptoKey, 0, cryptoKey.length); } else cryptoKey = null; if (uri.extra != null) { extra = new byte[uri.extra.length]; System.arraycopy(uri.extra, 0, extra, 0, extra.length); } else extra = null; this.suggestedEdition = uri.suggestedEdition; Logger.minor(this, "Copied: " + toString() + " from " + uri.toString(), new Exception("debug")); }