/** * Same as orig.getBytes("UTF-8") but throws an unchecked RuntimeException * instead of an UnsupportedEncodingException if no UTF-8, for ease of use. * * @return null if orig is null * @throws RuntimeException * @deprecated unused */ public static byte[] getUTF8(StringBuffer orig) { if (orig == null) return null; return getUTF8(orig.toString()); }
public String getHost() { int addressType = this.header[3]; if (addressType != 3) return null; int namelen = (this.header[4] & 0xff); byte[] nameBytes = new byte[namelen]; System.arraycopy(this.header, 5, nameBytes, 0, namelen); return DataHelper.getUTF8(nameBytes); }
public void run() { while (true) { try { Thread.sleep(127*1000); synchronized(_out) { _out.write(DataHelper.getUTF8("PING " + System.currentTimeMillis() + '\n')); _out.flush(); } } catch (InterruptedException ie) { break; } catch (IOException ioe) { break; } } } }
/** * Returns lower case. * Does not add trailing '='. * * @param source if null will return "" */ public static String encode(String source) { return (source != null ? encode(DataHelper.getUTF8(source)) : ""); }
/** * Output will be a multiple of 4 chars, including 0-2 trailing '=' * As of 0.9.14, encodes the UTF-8 encoding of source. Prior to that, used the platform's encoding. * * @param source if null will return "" */ public static String encode(String source) { return (source != null ? encode(DataHelper.getUTF8(source)) : ""); }
/** * Unsynchronized, use with caution * @return success */ public static boolean writeString(String str, SocketChannel out) { try { writeBytes(ByteBuffer.wrap(DataHelper.getUTF8(str)), out); } catch (IOException e) { //_log.debug("Caught IOException", e); return false; } return true ; }
/** * Decodes data from Base64 notation. * As of 0.9.14, this uses the I2P alphabet, so it is not "standard". * * @param s the string to decode * @return the decoded data, null on error * @since 1.4 */ private static byte[] standardDecode(String s) { // We use getUTF8() instead of getASCII() so we may verify // there's no UTF-8 in there. byte[] bytes = DataHelper.getUTF8(s); if (bytes.length != s.length()) return null; return decode(bytes, 0, bytes.length); } // end decode
/** * Decodes data from Base32 notation and * returns it as a string. * Case-insensitive. * Does not allow trailing '='. * * @param s the string to decode, if null returns null * @return The data as a string or null on failure */ public static String decodeToString(String s) { byte[] b = decode(s); if (b == null) return null; return DataHelper.getUTF8(b); }
private static void decode(InputStream in, OutputStream out) throws IOException { byte decoded[] = decode(DataHelper.getUTF8(read(in))); if (decoded == null) throw new IOException("Invalid base 64 string"); out.write(decoded); }
private static void decode(InputStream in, OutputStream out) throws IOException { byte decoded[] = decode(DataHelper.getUTF8(read(in))); if (decoded == null) { System.out.println("FAIL"); return; } out.write(decoded); }
/** * @param realm e.g. i2cp, routerconsole, etc. * @param user null or "" for no user, already trimmed * @param pw plain text, already trimmed * @return if pw verified */ public boolean checkB64(String realm, String user, String pw) { String pfx = realm; if (user != null && user.length() > 0) pfx += '.' + user; String b64 = _context.getProperty(pfx + PROP_B64); if (b64 == null) return false; return b64.equals(Base64.encode(DataHelper.getUTF8(pw))); }
public String getAuthHeader(String method, String uri) throws IOException { switch (authMode) { case BASIC: authSent = true; // use standard alphabet return "Basic " + Base64.encode(DataHelper.getUTF8(username + ':' + password), true); case DIGEST: if (authChallenge == null) throw new IOException("Bad proxy auth response"); if (args == null) args = parseAuthArgs(authChallenge); Map<String, String> outArgs = generateAuthArgs(method, uri); if (outArgs == null) throw new IOException("Bad proxy auth response"); StringBuilder buf = new StringBuilder(256); buf.append("Digest"); for (Map.Entry<String, String> e : outArgs.entrySet()) { buf.append(' ').append(e.getKey()).append('=').append(e.getValue()); } authSent = true; return buf.toString(); default: throw new IOException("Unknown proxy auth type " + authChallenge); } }
/** * UTF-8 */ private static void toHex(char c, StringBuilder buf) { if (c > 0x7f) { byte[] b = DataHelper.getUTF8(Character.toString(c)); for (int i = 0; i < b.length; i++) { toHex(b[i], buf); } } else { toHex((byte) c, buf); } } }
/** * Actually write the specified entries * */ private synchronized void writeEntries() { File f = new File(_historyFile); if (!f.isAbsolute()) f = new File(_context.getLogDir(), _historyFile); FileOutputStream fos = null; try { fos = new SecureFileOutputStream(f, true); String entry; while ((entry = _unwrittenEntries.poll()) != null) { fos.write(DataHelper.getUTF8(entry)); fos.write(NL); } } catch (IOException ioe) { _log.error("Error writing trace entries", ioe); } finally { if (fos != null) try { fos.close(); } catch (IOException ioe) {} } }
/** * This will fail if * if user contains '#' or '=' or starts with '!' * * @param realm e.g. i2cp, routerconsole, etc. * @param user null or "" for no user, already trimmed * @param pw plain text, already trimmed * @return success */ public boolean saveB64(String realm, String user, String pw) { String pfx = realm; if (user != null && user.length() > 0) pfx += '.' + user; String b64 = Base64.encode(DataHelper.getUTF8(pw)); Map<String, String> toAdd = Collections.singletonMap(pfx + PROP_B64, b64); List<String> toDel = new ArrayList<String>(4); toDel.add(pfx + PROP_PW); toDel.add(pfx + PROP_MD5); toDel.add(pfx + PROP_CRYPT); toDel.add(pfx + PROP_SHASH); return _context.router().saveConfig(toAdd, toDel); }
/** * Straight MD5, no salt * Will return the MD5 sum of the data, compatible with Jetty * and RFC 2617. * * Updated in 0.9.26 to use UTF-8, as implied in RFC 7616/7617 * See also http://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication * * @param fullpw non-null, plain text, already trimmed * @return lower-case hex with leading zeros, 32 chars, or null on error */ public static String md5Hex(String fullpw) { byte[] data = DataHelper.getUTF8(fullpw); byte[] sum = md5Sum(data); if (sum != null) // adds leading zeros if necessary return DataHelper.toString(sum); return null; }
/** * Creates a copy of this MetaInfo that shares everything except the * announce URL. * Drops any announce-list. * Preserves infohash and info map, including any non-standard fields. * @param announce may be null */ public MetaInfo reannounce(String announce) throws InvalidBEncodingException { Map<String, BEValue> m = new HashMap<String, BEValue>(); if (announce != null) m.put("announce", new BEValue(DataHelper.getUTF8(announce))); Map<String, BEValue> info = createInfoMap(); m.put("info", new BEValue(info)); return new MetaInfo(m); }
/** * Check pw against b64 salt+hash, as generated by createHash() * * @param shash b64 string * @param pw plain text non-null, already trimmed * @return if pw verified * @since 0.9.24 */ public boolean checkHash(String shash, String pw) { byte[] shashBytes = Base64.decode(shash); if (shashBytes == null || shashBytes.length != SHASH_LENGTH) return false; byte[] salt = new byte[SALT_LENGTH]; byte[] hash = new byte[SessionKey.KEYSIZE_BYTES]; System.arraycopy(shashBytes, 0, salt, 0, SALT_LENGTH); System.arraycopy(shashBytes, SALT_LENGTH, hash, 0, SessionKey.KEYSIZE_BYTES); byte[] pwHash = _context.keyGenerator().generateSessionKey(salt, DataHelper.getUTF8(pw)).getData(); return DataHelper.eqCT(hash, 0, pwHash, 0, SessionKey.KEYSIZE_BYTES); }
/** * Create a salt+hash, to be saved and verified later by verifyHash(). * * @param pw plain text non-null, already trimmed * @return salted+hash b64 string * @since 0.9.24 */ public String createHash(String pw) { byte[] salt = new byte[SALT_LENGTH]; _context.random().nextBytes(salt); byte[] pwHash = _context.keyGenerator().generateSessionKey(salt, DataHelper.getUTF8(pw)).getData(); byte[] shashBytes = new byte[SHASH_LENGTH]; System.arraycopy(salt, 0, shashBytes, 0, SALT_LENGTH); System.arraycopy(pwHash, 0, shashBytes, SALT_LENGTH, SessionKey.KEYSIZE_BYTES); return Base64.encode(shashBytes); }
@Test public void testRate() throws Exception{ Rate rate = new Rate(5000); for (int i = 0; i < 50; i++) { Thread.sleep(20); rate.addData(i * 100, 20); } rate.coalesce(); StringBuilder buf = new StringBuilder(1024); rate.store("rate.test", buf); byte data[] = DataHelper.getUTF8(buf.toString()); Properties props = new Properties(); props.load(new ByteArrayInputStream(data)); Rate r = new Rate(props, "rate.test", true); assertEquals(r, rate); } }