/** * Initialize a new SAM message-based session. * * @param dest Base64-encoded destination and private keys (same format as PrivateKeyFile) * @param props Properties to setup the I2P session * @throws IOException * @throws DataFormatException * @throws I2PSessionException */ protected SAMMessageSession(String dest, Properties props) throws IOException, DataFormatException, I2PSessionException { this(new ByteArrayInputStream(Base64.decode(dest)), props); }
/** * Create a new SAM STREAM session. * * Caller MUST call start(). * * @param dest Base64-encoded destination and private keys (same format as PrivateKeyFile) * @param dir Session direction ("RECEIVE", "CREATE" or "BOTH") or "__v3__" if extended by SAMv3StreamSession * @param props Properties to setup the I2P session * @param recv Object that will receive incoming data * @throws IOException * @throws DataFormatException * @throws SAMException */ public SAMStreamSession(String dest, String dir, Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException { this(new ByteArrayInputStream(Base64.decode(dest)), dir, props, recv); }
private static Hash getHash(String filename, String prefix, String suffix) { try { String key = filename.substring(prefix.length()); key = key.substring(0, key.length() - suffix.length()); //Hash h = new Hash(); //h.fromBase64(key); byte[] b = Base64.decode(key); if (b == null) return null; Hash h = Hash.create(b); return h; } catch (RuntimeException e) { // static //_log.warn("Unable to fetch the key from [" + filename + "]", e); return null; } }
/** * 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
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 Hash getHash(String name) { String key = name.substring(PREFIX.length()); key = key.substring(0, 44); //Hash h = new Hash(); try { //h.fromBase64(key); byte[] b = Base64.decode(key); if (b == null) return null; Hash h = Hash.create(b); return h; } catch (RuntimeException dfe) { _log.warn("Invalid base64 [" + key + "]", dfe); return null; } }
/** * Decodes data from Base64 notation and * returns it as a string. * Equivlaent to calling * <code>new String( decode( s ) )</code> * * As of 0.9.14, decodes as UTF-8. Prior to that, it used the platform's encoding. * For best results, decoded data should be 7 bit. * * As of 0.9.14, does not require trailing '=' if remaining bits are zero. * Prior to that, trailing 1, 2, or 3 chars were ignored. * * As of 0.9.14, trailing garbage after an '=' will cause an error. * Prior to that, it was ignored. * * As of 0.9.14, whitespace will cause an error. * Prior to that, it was ignored. * * @param s the string to decode * @return The data as a string, or null on error * @since 1.4 */ public static String decodeToString(String s) { byte[] b = decode(s); if (b == null) return null; return DataHelper.getUTF8(b); } // end decodeToString
String b64 = k.substring(PROP_META_MAGNET_PREFIX.length()); b64 = b64.replace('$', '='); byte[] ih = Base64.decode(b64);
/** * Always throws an exception if something fails. * We do NOT validate the received data here - that is done in PersistentDataStore * * @param peer The Base64 hash, may include % encoding. It is decoded and validated here. * @return true on success, false if skipped */ private boolean fetchSeed(String seedURL, String peer) throws IOException, URISyntaxException { // Use URI to do % decoding of the B64 hash (some servers escape ~ and =) // Also do basic hash validation. This prevents stuff like // .. or / in the file name URI uri = new URI(peer); String b64 = uri.getPath(); if (b64 == null) throw new IOException("bad hash " + peer); byte[] hash = Base64.decode(b64); if (hash == null || hash.length != Hash.HASH_LENGTH) throw new IOException("bad hash " + peer); Hash ourHash = _context.routerHash(); if (ourHash != null && DataHelper.eq(hash, ourHash.getData())) return false; URI url = new URI(seedURL + (seedURL.endsWith("/") ? "" : "/") + ROUTERINFO_PREFIX + peer + ROUTERINFO_SUFFIX); byte data[] = readURL(url); if (data == null || data.length <= 0) throw new IOException("Failed fetch of " + url); return writeSeed(b64, data); }
public void fromBase64(String data) throws DataFormatException { if (data == null) throw new DataFormatException("Null data passed in"); byte bytes[] = Base64.decode(data); if (bytes == null) throw new DataFormatException("Bad Base64 \"" + data + '"'); fromByteArray(bytes); }
/** * 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); }
if (_nonceCleanCounter.incrementAndGet() % 16 == 0) cleanNonces(); byte[] n = Base64.decode(b64); if (n == null || n.length != NONCE_BYTES) return AuthResult.AUTH_BAD;
/** * Non-blocking * * @param privData Base64-encoded private key data, * format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile} * @throws IllegalArgumentException if the I2CP configuration is b0rked so * badly that we cant create a socketManager */ public I2PTunnelServer(InetAddress host, int port, String privData, Logging l, EventDispatcher notifyThis, I2PTunnel tunnel) { super("Server at " + host + ':' + port, notifyThis, tunnel); _log = tunnel.getContext().logManager().getLog(getClass()); ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(privData)); this.l = l; this.remoteHost = host; this.remotePort = port; _usePool = getUsePool(); buildSocketMap(tunnel.getClientOptions()); sockMgr = createManager(bais); }
/** * Create from persistent storage string. * Format: NID:Hash:Destination:port * First 3 in base 64; Destination may be empty string * @throws IllegalArgumentException */ public NodeInfo(String s) throws DataFormatException { super(); String[] parts = DataHelper.split(s, ":", 4); if (parts.length != 4) throw new DataFormatException("Bad format"); byte[] nid = Base64.decode(parts[0]); if (nid == null || nid.length != NID.HASH_LENGTH) throw new DataFormatException("Bad NID"); nID = new NID(nid); byte[] h = Base64.decode(parts[1]); if (h == null || h.length != Hash.HASH_LENGTH) throw new DataFormatException("Bad hash"); //hash = new Hash(h); hash = Hash.create(h); if (parts[2].length() > 0) dest = new Destination(parts[2]); try { port = Integer.parseInt(parts[3]); } catch (NumberFormatException nfe) { throw new DataFormatException("Bad port", nfe); } initialize(); }
/** * Get the saved bitfield for a torrent from the config file. * Convert "." to a full bitfield. * A Snark.CompleteListener method. */ public BitField getSavedTorrentBitField(Snark snark) { MetaInfo metainfo = snark.getMetaInfo(); if (metainfo == null) return null; Properties config = getConfig(snark); String bf = config.getProperty(PROP_META_BITFIELD); if (bf == null) return null; int len = metainfo.getPieces(); if (bf.equals(".")) { BitField bitfield = new BitField(len); for (int i = 0; i < len; i++) bitfield.set(i); return bitfield; } byte[] bitfield = Base64.decode(bf); if (bitfield == null) return null; if (bitfield.length * 8 < len) return null; return new BitField(bitfield, len); }
/** * Check whether a base64-encoded {dest,privkey,signingprivkey} is valid * * @param dest The base64-encoded destination and keys to be checked (same format as PrivateKeyFile) * @return true if valid */ public static boolean checkPrivateDestination(String dest) { byte[] b = Base64.decode(dest); if (b == null || b.length < 663) return false; ByteArrayInputStream destKeyStream = new ByteArrayInputStream(b); try { Destination d = Destination.create(destKeyStream); new PrivateKey().readBytes(destKeyStream); SigningPrivateKey spk = new SigningPrivateKey(d.getSigningPublicKey().getType()); spk.readBytes(destKeyStream); } catch (DataFormatException e) { return false; } catch (IOException e) { return false; } return destKeyStream.available() == 0; }
/** * Sets the data. * @throws DataFormatException if decoded data is not the legal number of bytes or on decoding error * @throws RuntimeException if data already set. */ @Override public void fromBase64(String data) throws DataFormatException { if (data == null) throw new DataFormatException("Null data passed in"); byte[] d = Base64.decode(data); if (d == null) throw new DataFormatException("Bad Base64 encoded data"); if (d.length != length()) throw new DataFormatException("Bad decoded data length, expected " + length() + " got " + d.length); // call setData() instead of _data = data in case overridden setData(d); }
@Test public void testBase64(){ String orig = "you smell"; String encoded = Base64.encode(DataHelper.getASCII(orig)); byte decoded[] = Base64.decode(encoded); String transformed = new String(decoded); assertTrue(orig.equals(transformed)); byte all[] = new byte[256]; for (int i = 0; i < all.length; i++) all[i] = (byte) (0xFF & i); encoded = Base64.encode(all); decoded = Base64.decode(encoded); assertTrue(DataHelper.eq(decoded, all)); } }
if (s == null) throw new IllegalArgumentException("no NTCP2 IV"); _bobIV = Base64.decode(s); if (_bobIV == null || _bobIV.length != IV_SIZE || DataHelper.eq(_bobIV, 0, ZEROKEY, 0, IV_SIZE))
public void testVerifyCompatability(){ PublicKey pub = new PublicKey(); PrivateKey priv = new PrivateKey(); try{ pub.fromBase64(PUBLIC_KEY); priv.fromBase64(PRIVATE_KEY); }catch(DataFormatException dfe){ dfe.printStackTrace(); fail(); } for (int i = 0; i < ENCRYPTED.length; i++) { byte enc[] = Base64.decode(ENCRYPTED[i]); byte decrypted[] = _context.elGamalEngine().decrypt(enc, priv); assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i]))); } }