Author: dbkr
Date: 2006-06-23 11:16:15 +0000 (Fri, 23 Jun 2006)
New Revision: 9357
Added:
trunk/apps/Freemail/src/freemail/FreenetURI.java
trunk/apps/Freemail/src/org/
trunk/apps/Freemail/src/org/archive/
trunk/apps/Freemail/src/org/archive/util/
trunk/apps/Freemail/src/org/archive/util/Base32.java
Modified:
trunk/apps/Freemail/src/freemail/AccountManager.java
trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
Log:
Change addresses to be base 32 encoded instead of base 16. Makes them a bit
shorter.
Modified: trunk/apps/Freemail/src/freemail/AccountManager.java
===================================================================
--- trunk/apps/Freemail/src/freemail/AccountManager.java 2006-06-23
03:24:18 UTC (rev 9356)
+++ trunk/apps/Freemail/src/freemail/AccountManager.java 2006-06-23
11:16:15 UTC (rev 9357)
@@ -18,6 +18,8 @@
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.util.encoders.Hex;
+import org.archive.util.Base32;
+
import freemail.fcp.HighLevelFCPClient;
import freemail.fcp.SSKKeyPair;
import freemail.utils.PropsFile;
@@ -155,6 +157,12 @@
}
System.out.println("Mailsite keys generated.");
+
+ FreenetURI puburi = new FreenetURI(keypair.pubkey);
+
+ String base32body =
Base32.encode(puburi.getKeyBody().getBytes());
+
+ System.out.println("Your Freemail address is:
<anything>@"+base32body+".freemail");
} catch (IOException ioe) {
System.out.println("Couldn't create mailsite key file!
"+ioe.getMessage());
}
Added: trunk/apps/Freemail/src/freemail/FreenetURI.java
===================================================================
--- trunk/apps/Freemail/src/freemail/FreenetURI.java 2006-06-23 03:24:18 UTC
(rev 9356)
+++ trunk/apps/Freemail/src/freemail/FreenetURI.java 2006-06-23 11:16:15 UTC
(rev 9357)
@@ -0,0 +1,73 @@
+package freemail;
+
+import java.net.MalformedURLException;
+
+/*
+ * Represents a Freenet URI
+ * If this gets complicated, look at using Freenet's own class of the same
name (has dependancies though)
+ */
+public class FreenetURI {
+ private String keytype;
+ private String keybody;
+ private String suffix; // this includes USK versions etc for now
+
+ public FreenetURI(String uri) throws MalformedURLException {
+ String[] parts = uri.split(":", 2);
+
+ if (parts.length == 2 && !parts[0].equals("freenet")) {
+ throw new MalformedURLException("Invalid scheme - not a
freenet address");
+ } else if (parts.length == 2) {
+ uri = parts[1];
+ }
+
+ // now split on the '@'
+ parts = uri.split("@", 2);
+
+ if (parts.length < 2) {
+ this.keytype = "KSK";
+ } else {
+ this.keytype = parts[0];
+ uri = parts[1];
+ }
+
+ // finally, separate the body from the metastrings
+ parts = uri.split("/", 2);
+
+ if (parts.length < 2) {
+ this.keybody = uri;
+ this.suffix = null;
+ } else {
+ this.keybody = parts[0];
+ this.suffix = parts[1];
+ }
+ }
+
+ public String getKeyType() {
+ return this.keytype;
+ }
+
+ public String getKeyBody() {
+ return this.keybody;
+ }
+
+ public String getSuffix() {
+ return this.suffix;
+ }
+
+ /*
+ * Read a freenet URI from args and print out in parts to test
+ */
+ public static void main(String args[]) {
+ FreenetURI uri;
+ try {
+ uri = new FreenetURI(args[0]);
+ } catch (MalformedURLException mue) {
+ mue.printStackTrace();
+ return;
+ }
+
+ System.out.println("Key type: "+uri.getKeyType());
+ System.out.println("Key body: "+uri.getKeyBody());
+ System.out.println("Suffix: "+uri.getSuffix());
+ }
+}
Modified: trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
===================================================================
--- trunk/apps/Freemail/src/freemail/utils/EmailAddress.java 2006-06-23
03:24:18 UTC (rev 9356)
+++ trunk/apps/Freemail/src/freemail/utils/EmailAddress.java 2006-06-23
11:16:15 UTC (rev 9357)
@@ -2,6 +2,8 @@
import org.bouncycastle.util.encoders.Hex;
+import org.archive.util.Base32;
+
public class EmailAddress {
public String realname;
public String user;
@@ -59,13 +61,7 @@
if (domparts.length < 2) return null;
- try {
- return new String (Hex.decode(domparts[0].getBytes()));
- } catch (ArrayIndexOutOfBoundsException aiobe) {
- // the Hex decoder just generates this exception if the
input is not hex
- // (since it looks up a non-hex charecter in the
decoding table)
- return null;
- }
+ return new String (Base32.decode(domparts[0]));
}
public String toString() {
Added: trunk/apps/Freemail/src/org/archive/util/Base32.java
===================================================================
--- trunk/apps/Freemail/src/org/archive/util/Base32.java 2006-06-23
03:24:18 UTC (rev 9356)
+++ trunk/apps/Freemail/src/org/archive/util/Base32.java 2006-06-23
11:16:15 UTC (rev 9357)
@@ -0,0 +1,165 @@
+/* Base32
+*
+* $Id: Base32.java,v 1.4 2004/04/15 19:04:01 stack-sf Exp $
+*
+* Created on Jan 21, 2004
+*
+* Copyright (C) 2004 Internet Archive.
+*
+* This file is part of the Heritrix web crawler (crawler.archive.org).
+*
+* Heritrix is free software; you can redistribute it and/or modify
+* it under the terms of the GNU Lesser Public License as published by
+* the Free Software Foundation; either version 2.1 of the License, or
+* any later version.
+*
+* Heritrix is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Lesser Public License for more details.
+*
+* You should have received a copy of the GNU Lesser Public License
+* along with Heritrix; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+package org.archive.util;
+
+/**
+ * Base32 - encodes and decodes RFC3548 Base32
+ * (see http://www.faqs.org/rfcs/rfc3548.html )
+ *
+ * Imported public-domain code of Bitzi.
+ *
+ * @author Robert Kaye
+ * @author Gordon Mohr
+ */
+public class Base32 {
+ private static final String base32Chars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+ private static final int[] base32Lookup =
+ { 0xFF,0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, // '0', '1', '2', '3', '4',
'5', '6', '7'
+ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // '8', '9', ':', ';', '<',
'=', '>', '?'
+ 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '@', 'A', 'B', 'C', 'D',
'E', 'F', 'G'
+ 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O'
+ 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W'
+ 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, // 'X', 'Y', 'Z', '[', '\',
']', '^', '_'
+ 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '`', 'a', 'b', 'c', 'd',
'e', 'f', 'g'
+ 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o'
+ 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'p', 'q', 'r', 's', 't',
'u', 'v', 'w'
+ 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF // 'x', 'y', 'z', '{', '|',
'}', '~', 'DEL'
+ };
+
+ /**
+ * Encodes byte array to Base32 String.
+ *
+ * @param bytes Bytes to encode.
+ * @return Encoded byte array <code>bytes</code> as a String.
+ *
+ */
+ static public String encode(final byte[] bytes) {
+ int i = 0, index = 0, digit = 0;
+ int currByte, nextByte;
+ StringBuffer base32 = new StringBuffer((bytes.length + 7) * 8 / 5);
+
+ while (i < bytes.length) {
+ currByte = (bytes[i] >= 0) ? bytes[i] : (bytes[i] + 256); // unsign
+
+ /* Is the current digit going to span a byte boundary? */
+ if (index > 3) {
+ if ((i + 1) < bytes.length) {
+ nextByte =
+ (bytes[i + 1] >= 0) ? bytes[i + 1] : (bytes[i + 1] +
256);
+ } else {
+ nextByte = 0;
+ }
+
+ digit = currByte & (0xFF >> index);
+ index = (index + 5) % 8;
+ digit <<= index;
+ digit |= nextByte >> (8 - index);
+ i++;
+ } else {
+ digit = (currByte >> (8 - (index + 5))) & 0x1F;
+ index = (index + 5) % 8;
+ if (index == 0)
+ i++;
+ }
+ base32.append(base32Chars.charAt(digit));
+ }
+
+ return base32.toString();
+ }
+
+ /**
+ * Decodes the given Base32 String to a raw byte array.
+ *
+ * @param base32
+ * @return Decoded <code>base32</code> String as a raw byte array.
+ */
+ static public byte[] decode(final String base32) {
+ int i, index, lookup, offset, digit;
+ byte[] bytes = new byte[base32.length() * 5 / 8];
+
+ for (i = 0, index = 0, offset = 0; i < base32.length(); i++) {
+ lookup = base32.charAt(i) - '0';
+
+ /* Skip chars outside the lookup table */
+ if (lookup < 0 || lookup >= base32Lookup.length) {
+ continue;
+ }
+
+ digit = base32Lookup[lookup];
+
+ /* If this digit is not in the table, ignore it */
+ if (digit == 0xFF) {
+ continue;
+ }
+
+ if (index <= 3) {
+ index = (index + 5) % 8;
+ if (index == 0) {
+ bytes[offset] |= digit;
+ offset++;
+ if (offset >= bytes.length)
+ break;
+ } else {
+ bytes[offset] |= digit << (8 - index);
+ }
+ } else {
+ index = (index + 5) % 8;
+ bytes[offset] |= (digit >>> index);
+ offset++;
+
+ if (offset >= bytes.length) {
+ break;
+ }
+ bytes[offset] |= digit << (8 - index);
+ }
+ }
+ return bytes;
+ }
+
+ /** For testing, take a command-line argument in Base32, decode, print in
hex,
+ * encode, print
+ *
+ * @param args
+ */
+ static public void main(String[] args) {
+ if (args.length == 0) {
+ System.out.println("Supply a Base32-encoded argument.");
+ return;
+ }
+ System.out.println(" Original: " + args[0]);
+ byte[] decoded = Base32.decode(args[0]);
+ System.out.print(" Hex: ");
+ for (int i = 0; i < decoded.length; i++) {
+ int b = decoded[i];
+ if (b < 0) {
+ b += 256;
+ }
+ System.out.print((Integer.toHexString(b + 256)).substring(1));
+ }
+ System.out.println();
+ System.out.println("Reencoded: " + Base32.encode(decoded));
+ }
+}