/** * Output will be a multiple of 4 chars, including 0-2 trailing '=' * @param source if null will return "" */ public static String encode(byte[] source, int off, int len) { return (source != null ? encode(source, off, len, false) : ""); }
/** * 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); }
int converted = 0; while (i + 3 < end) { converted = decode4to3(source, i, outBuff, outBuffPosn); if (converted < 0) return null; b4[2] = EQUALS_SIGN; b4[3] = EQUALS_SIGN; converted = decode4to3(b4, 0, outBuff, outBuffPosn); if (converted < 0) return null;
@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)); } }
/** * @param realm e.g. i2cp, routerconsole, etc. * @param user null or "" for no user, already trimmed * @return the decoded pw or null */ public String getB64(String realm, String user) { String pfx = realm; if (user != null && user.length() > 0) pfx += '.' + user; String b64 = _context.getProperty(pfx + PROP_B64); if (b64 == null) return null; return Base64.decodeToString(b64); }
@Override public String toBase64() { if (_data == null) return null; return Base64.encode(_data); }
/** * 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); }
s = ctx.getProperty(PROP_NTCP2_SP); if (s != null) { priv = Base64.decode(s); s = ctx.getProperty(PROP_NTCP2_IV); if (s != null) { iv = Base64.decode(s); b64IV = s; String b64Priv = Base64.encode(_ntcp2StaticPrivkey); b64IV = Base64.encode(iv); changes.put(PROP_NTCP2_SP, b64Priv); changes.put(PROP_NTCP2_IV, b64IV); _b64Ntcp2StaticPubkey = Base64.encode(_ntcp2StaticPubkey); _b64Ntcp2StaticIV = b64IV; } else {
/** * Output will be a multiple of 4 chars, including 0-2 trailing '=' * @param source if null will return "" * @param useStandardAlphabet Warning, must be false for I2P compatibility */ public static String encode(byte[] source, boolean useStandardAlphabet) { return (source != null ? encode(source, 0, source.length, useStandardAlphabet) : ""); }
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; } }
/** * Output will be a multiple of 4 chars, including 0-2 trailing '=' * @param source if null will return "" */ public static String encode(byte[] source) { return (source != null ? encode(source, 0, source.length) : ""); }
/** * 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
public final String toBase64() { return Base64.encode(_data, _offset, _valid); } }
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; } }
public void toRawString(StringBuilder buf) { if (_message != null) buf.append(Base64.encode(_message, _payloadBeginOffset, _payloadLength)); }
/** * 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