use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class SU3File method main.
/**
* Parses command line arguments when this class is used from the command
* line.
* Exits 1 on failure so this can be used in scripts.
*
* @param args Command line parameters.
*/
public static void main(String[] args) {
boolean ok = false;
try {
// defaults
String stype = null;
String ctype = null;
String ftype = null;
String kfile = null;
String crlfile = null;
String kspass = KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD;
boolean error = false;
boolean shouldVerify = true;
Getopt g = new Getopt("SU3File", args, "t:c:f:k:xp:r:");
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
case 't':
stype = g.getOptarg();
break;
case 'c':
ctype = g.getOptarg();
break;
case 'f':
ftype = g.getOptarg();
break;
case 'k':
kfile = g.getOptarg();
break;
case 'r':
crlfile = g.getOptarg();
break;
case 'x':
shouldVerify = false;
break;
case 'p':
kspass = g.getOptarg();
break;
case '?':
case ':':
default:
error = true;
}
}
int idx = g.getOptind();
String cmd = args[idx];
List<String> a = new ArrayList<String>(Arrays.asList(args).subList(idx + 1, args.length));
if (error) {
showUsageCLI();
} else if ("showversion".equals(cmd)) {
ok = showVersionCLI(a.get(0));
} else if ("sign".equals(cmd)) {
// speed things up by specifying a small PRNG buffer size
Properties props = new Properties();
props.setProperty("prng.bufferSize", "16384");
new I2PAppContext(props);
ok = signCLI(stype, ctype, ftype, a.get(0), a.get(1), a.get(2), a.get(3), a.get(4), "", kspass);
} else if ("bulksign".equals(cmd)) {
Properties props = new Properties();
props.setProperty("prng.bufferSize", "16384");
new I2PAppContext(props);
ok = bulkSignCLI(stype, ctype, a.get(0), a.get(1), a.get(2), a.get(3), kspass);
} else if ("verifysig".equals(cmd)) {
ok = verifySigCLI(a.get(0), kfile);
} else if ("keygen".equals(cmd)) {
Properties props = new Properties();
props.setProperty("prng.bufferSize", "16384");
new I2PAppContext(props);
ok = genKeysCLI(stype, a.get(0), a.get(1), crlfile, a.get(2), kspass);
} else if ("extract".equals(cmd)) {
ok = extractCLI(a.get(0), a.get(1), shouldVerify, kfile);
} else {
showUsageCLI();
}
} catch (NoSuchElementException nsee) {
showUsageCLI();
} catch (IndexOutOfBoundsException ioobe) {
showUsageCLI();
}
if (!ok)
System.exit(1);
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class DatabaseEntry method getRoutingKey.
/**
* Get the routing key for the structure using the current modifier in the RoutingKeyGenerator.
* This only calculates a new one when necessary though (if the generator's key modifier changes)
*
* @throws IllegalStateException if not in RouterContext
*/
public Hash getRoutingKey() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
if (!ctx.isRouterContext())
throw new IllegalStateException("Not in router context");
RoutingKeyGenerator gen = ctx.routingKeyGenerator();
long mod = gen.getLastChanged();
if (mod != _routingKeyGenMod) {
_currentRoutingKey = gen.getRoutingKey(getHash());
_routingKeyGenMod = mod;
}
return _currentRoutingKey;
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class DatabaseEntry method validateRoutingKey.
/**
* @throws IllegalStateException if not in RouterContext
*/
public boolean validateRoutingKey() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
if (!ctx.isRouterContext())
throw new IllegalStateException("Not in router context");
RoutingKeyGenerator gen = ctx.routingKeyGenerator();
Hash destKey = getHash();
Hash rk = gen.getRoutingKey(destKey);
return rk.equals(getRoutingKey());
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class KeyStoreUtil method randomString.
/**
* 48 char b32 string (30 bytes of entropy)
*/
public static String randomString() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
// make a random 48 character password (30 * 8 / 5)
byte[] rand = new byte[30];
ctx.random().nextBytes(rand);
return Base32.encode(rand);
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class WebMail method processComposeButtons.
/**
* process buttons of compose message dialog
* This must be called BEFORE processStateChangeButtons so we can add the attachment before SEND
*
* @param sessionObject
* @param request
* @return new state, or null if unknown
*/
private static State processComposeButtons(SessionObject sessionObject, RequestWrapper request) {
State state = null;
String filename = request.getFilename(NEW_FILENAME);
// We handle an attachment whether sending or uploading
if (filename != null && (buttonPressed(request, NEW_UPLOAD) || buttonPressed(request, SEND))) {
int i = filename.lastIndexOf('/');
if (i != -1)
filename = filename.substring(i + 1);
i = filename.lastIndexOf('\\');
if (i != -1)
filename = filename.substring(i + 1);
if (filename != null && filename.length() > 0) {
InputStream in = null;
OutputStream out = null;
I2PAppContext ctx = I2PAppContext.getGlobalContext();
File f = new File(ctx.getTempDir(), "susimail-attachment-" + ctx.random().nextLong());
try {
in = request.getInputStream(NEW_FILENAME);
if (in == null)
throw new IOException("no stream");
out = new SecureFileOutputStream(f);
DataHelper.copy(in, out);
String contentType = request.getContentType(NEW_FILENAME);
String encodeTo;
String ctlc = contentType.toLowerCase(Locale.US);
if (ctlc.startsWith("text/")) {
encodeTo = "quoted-printable";
// interpret it as ISO-8859-1
if (!ctlc.contains("charset="))
contentType += "; charset=\"utf-8\"";
} else {
encodeTo = "base64";
}
Encoding encoding = EncodingFactory.getEncoding(encodeTo);
if (encoding != null) {
if (sessionObject.attachments == null)
sessionObject.attachments = new ArrayList<Attachment>();
sessionObject.attachments.add(new Attachment(filename, contentType, encodeTo, f));
} else {
sessionObject.error += _t("No Encoding found for {0}", encodeTo) + '\n';
}
} catch (IOException e) {
sessionObject.error += _t("Error reading uploaded file: {0}", e.getMessage()) + '\n';
f.delete();
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
state = State.NEW;
} else if (sessionObject.attachments != null && buttonPressed(request, DELETE_ATTACHMENT)) {
for (String item : getCheckedItems(request)) {
try {
int n = Integer.parseInt(item);
for (int i = 0; i < sessionObject.attachments.size(); i++) {
Attachment attachment = sessionObject.attachments.get(i);
if (attachment.hashCode() == n) {
sessionObject.attachments.remove(i);
break;
}
}
} catch (NumberFormatException nfe) {
}
}
state = State.NEW;
}
return state;
}
Aggregations