use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class ElGamalTest method testBasicAES.
public void testBasicAES() {
SessionKey sessionKey = KeyGenerator.getInstance().generateSessionKey();
Hash h = SHA256Generator.getInstance().calculateHash(sessionKey.getData());
byte[] iv = new byte[16];
System.arraycopy(h.getData(), 0, iv, 0, 16);
String msg = "Hello world01234012345678901234501234567890123450123456789012345";
h = SHA256Generator.getInstance().calculateHash(DataHelper.getASCII(msg));
byte[] aesEncr = new byte[DataHelper.getASCII(msg).length];
byte[] aesDecr = new byte[aesEncr.length];
_context.aes().encrypt(DataHelper.getASCII(msg), 0, aesEncr, 0, sessionKey, iv, aesEncr.length);
_context.aes().decrypt(aesEncr, 0, aesDecr, 0, sessionKey, iv, aesEncr.length);
h = SHA256Generator.getInstance().calculateHash(aesDecr);
assertEquals(msg, new String(aesDecr));
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class GeneralHelper method updateTunnelConfig.
protected static List<String> updateTunnelConfig(TunnelControllerGroup tcg, int tunnel, TunnelConfig config) {
// Get current tunnel controller
TunnelController cur = getController(tcg, tunnel);
Properties props = config.getConfig();
List<String> msgs = new ArrayList<String>();
String type = props.getProperty(TunnelController.PROP_TYPE);
if (TunnelController.TYPE_STD_CLIENT.equals(type) || TunnelController.TYPE_IRC_CLIENT.equals(type)) {
//
if (Boolean.parseBoolean(props.getProperty(OPT + I2PTunnelClientBase.PROP_USE_SSL))) {
// add the local interface and all targets to the cert
String intfc = props.getProperty(TunnelController.PROP_INTFC);
Set<String> altNames = new HashSet<String>(4);
if (intfc != null && !intfc.equals("0.0.0.0") && !intfc.equals("::") && !intfc.equals("0:0:0:0:0:0:0:0"))
altNames.add(intfc);
String tgts = props.getProperty(TunnelController.PROP_DEST);
if (tgts != null) {
altNames.add(intfc);
String[] hosts = DataHelper.split(tgts, "[ ,]");
for (String h : hosts) {
int colon = h.indexOf(':');
if (colon >= 0)
h = h.substring(0, colon);
altNames.add(h);
if (!h.endsWith(".b32.i2p")) {
Hash hash = ConvertToHash.getHash(h);
if (hash != null)
altNames.add(hash.toBase32());
}
}
}
try {
boolean created = SSLClientUtil.verifyKeyStore(props, OPT, altNames);
if (created) {
// config now contains new keystore props
String name = props.getProperty(TunnelController.PROP_NAME, "");
msgs.add("Created new self-signed certificate for tunnel " + name);
}
} catch (IOException ioe) {
msgs.add("Failed to create new self-signed certificate for tunnel " + getTunnelName(tcg, tunnel) + ", check logs: " + ioe);
}
}
}
if (cur == null) {
// creating new
cur = new TunnelController(props, "", true);
tcg.addController(cur);
if (cur.getStartOnLoad())
cur.startTunnelBackground();
} else {
cur.setConfig(props, "");
}
// if the current tunnel is shared, and of supported type
if (Boolean.parseBoolean(cur.getSharedClient()) && TunnelController.isClient(cur.getType())) {
// all clients use the same I2CP session, and as such, use the same I2CP options
List<TunnelController> controllers = tcg.getControllers();
for (int i = 0; i < controllers.size(); i++) {
TunnelController c = controllers.get(i);
// Current tunnel modified by user, skip
if (c == cur)
continue;
// if it belongs to a shared destination, and is of supported type
if (Boolean.parseBoolean(c.getSharedClient()) && TunnelController.isClient(c.getType())) {
Properties cOpt = c.getConfig("");
config.updateTunnelQuantities(cOpt);
cOpt.setProperty("option.inbound.nickname", TunnelConfig.SHARED_CLIENT_NICKNAME);
cOpt.setProperty("option.outbound.nickname", TunnelConfig.SHARED_CLIENT_NICKNAME);
c.setConfig(cOpt, "");
}
}
}
return msgs;
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class IdenticonServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getCharacterEncoding() == null)
request.setCharacterEncoding("UTF-8");
String codeParam = request.getParameter(PARAM_IDENTICON_CODE_SHORT);
boolean codeSpecified = codeParam != null && codeParam.length() > 0;
if (!codeSpecified) {
response.setStatus(403);
return;
}
String sizeParam = request.getParameter(PARAM_IDENTICON_SIZE_SHORT);
int size = 32;
if (sizeParam != null) {
try {
size = Integer.parseInt(sizeParam);
if (size < 16)
size = 16;
else if (size > 512)
size = 512;
} catch (NumberFormatException nfe) {
}
}
String identiconETag = IdenticonUtil.getIdenticonETag(codeParam.hashCode(), size, version);
String requestETag = request.getHeader("If-None-Match");
if (requestETag != null && requestETag.equals(identiconETag)) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else {
// we try to interpret the codeParam parameter as:
// 1) a number
// 2) a base32 or base64 hash, which we take the Java hashcode of
// 3) a string, which we take the Java hashcode of
int code;
try {
code = Integer.parseInt(codeParam);
} catch (NumberFormatException nfe) {
Hash h = ConvertToHash.getHash(codeParam);
if (h != null)
code = Arrays.hashCode(h.getData());
else
code = codeParam.hashCode();
}
byte[] imageBytes = null;
// retrieve image bytes from either cache or renderer
if (cache == null || (imageBytes = cache.get(identiconETag)) == null) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
RenderedImage image = renderer.render(code, size);
ImageIO.write(image, IDENTICON_IMAGE_FORMAT, byteOut);
imageBytes = byteOut.toByteArray();
if (cache != null)
cache.add(identiconETag, imageBytes);
} else {
response.setStatus(403);
return;
}
// set ETag and, if code was provided, Expires header
response.setHeader("ETag", identiconETag);
if (codeSpecified) {
long expires = System.currentTimeMillis() + identiconExpiresInMillis;
response.addDateHeader("Expires", expires);
}
// return image bytes to requester
response.setContentType(IDENTICON_IMAGE_MIMETYPE);
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Accept-Ranges", "none");
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
}
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class RandomArtServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getCharacterEncoding() == null)
request.setCharacterEncoding("UTF-8");
String codeParam = request.getParameter(PARAM_IDENTICON_CODE_SHORT);
boolean codeSpecified = codeParam != null && codeParam.length() > 0;
if (!codeSpecified) {
response.setStatus(403);
return;
}
String modeParam = request.getParameter(PARAM_IDENTICON_MODE_SHORT);
boolean html = modeParam == null || modeParam.startsWith("h");
String identiconETag = IdenticonUtil.getIdenticonETag(codeParam.hashCode(), 0, version);
String requestETag = request.getHeader("If-None-Match");
if (requestETag != null && requestETag.equals(identiconETag)) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else {
Hash h = ConvertToHash.getHash(codeParam);
if (h == null) {
response.setStatus(403);
} else {
StringBuilder buf = new StringBuilder(512);
if (html) {
response.setContentType("text/html");
buf.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>");
} else {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
}
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Accept-Ranges", "none");
buf.append(RandomArt.gnutls_key_fingerprint_randomart(h.getData(), "SHA", 256, "", true, html));
if (html)
buf.append("</body></html>");
// set ETag and, if code was provided, Expires header
response.setHeader("ETag", identiconETag);
if (codeSpecified) {
long expires = System.currentTimeMillis() + identiconExpiresInMillis;
response.addDateHeader("Expires", expires);
}
// return image bytes to requester
byte[] imageBytes = buf.toString().getBytes("UTF-8");
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
}
}
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class ConvertToHash method main.
/**
* @since 0.9.28
*/
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: converttohash [hostname|b32|destination]...");
return;
}
for (int i = 0; i < args.length; i++) {
Hash h = getHash(args[i]);
System.out.println(h != null ? h.toBase64() : "conversion failed");
}
}
Aggregations