net.i2p.data.Base64类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中net.i2p.data.Base64类的一些代码示例,展示了Base64类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Base64类的具体详情如下:
包路径:net.i2p.data.Base64
类名称:Base64

Base64介绍

[英]Encodes and decodes to and from Base64 notation.

Change Log:

  • v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.
  • v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream where last buffer being read, if not completely full, was not returned.
  • v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.
  • v1.3.3 - Fixed I/O streams which were totally messed up.

I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/xmlizable periodically to check for updates or to contribute improvements.
Modified by jrandom for i2p, using safeEncode / safeDecode to create filesystem and URL safe base64 values (replacing / with ~, and + with -)
[中]对Base64符号进行编码和解码。
更改日志:
*v1。3.6-固定输出流。刷新(),以便重置“位置”。
*v1。3.5-添加了用于打开和关闭换行符的标志。修复了输入流中最后一个被读取的缓冲区(若未完全填满)未返回的错误。
*v1。3.4-修复了在错误时间抛出“未正确填充的流”错误。
*v1。3.3-修复了完全混乱的I/O流。
我将此代码置于公共域中。你想怎么做就怎么做。这个软件没有任何保证或保证,但却有很多美好的愿望!请定期访问http://iharder.net/xmlizable,检查更新或作出改进。
由jrandom针对i2p进行修改,使用safeEncode/safeDecode创建文件系统和URL safe base64值(替换为~,替换为-)

代码示例

代码示例来源:origin: i2p/i2p.i2p

/**
 *  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) : "");
}

代码示例来源:origin: i2p/i2p.i2p

/**
 * 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);
}

代码示例来源:origin: i2p/i2p.i2p

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;

代码示例来源:origin: i2p/i2p.i2p

private static void encode(InputStream in, OutputStream out) throws IOException {
  String encoded = encode(read(in));
  for (int i = 0; i < encoded.length(); i++)
    out.write((byte)(encoded.charAt(i) & 0xFF));
}

代码示例来源:origin: i2p/i2p.i2p

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);
}

代码示例来源:origin: i2p/i2p.i2p

if ("encodestring".equals(cmd)) {
  if (args.length != 2)
    help();
  System.out.println(encode(DataHelper.getUTF8(args[1])));
  return;
    help();
  byte[] dec = decode(args[1]);
  if (dec != null) {
    try {
    encode(in, out);
  } else {
    decode(in, out);

代码示例来源:origin: i2p/i2p.i2p

@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));
  }
}

代码示例来源:origin: i2p/i2p.i2p

/**
 *  @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);
}

代码示例来源:origin: i2p/i2p.i2p

runlock();
  out.println("OK " + net.i2p.data.Base64.encode(prikey.toByteArray()));
} else {
  out.println("ERROR no public key has been set");
      try {
        prikey = new ByteArrayOutputStream();
        prikey.write(net.i2p.data.Base64.decode(Arg));
        d = new Destination();
        d.fromBase64(Arg);

代码示例来源:origin: i2p/i2p.i2p

@Override
public String toBase64() {
  if (_data == null)
    return null;
  return Base64.encode(_data);
}

代码示例来源:origin: i2p/i2p.i2p

/**
 * 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);
}

代码示例来源:origin: i2p/i2p.i2p

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 {

代码示例来源:origin: i2p/i2p.i2p

/**
 *  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) : "");
}

代码示例来源:origin: i2p/i2p.i2p

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;
  }
}

代码示例来源:origin: i2p/i2p.i2p

/**
 *  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) : "");
}

代码示例来源:origin: i2p/i2p.i2p

/**
 * 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

代码示例来源:origin: i2p/i2p.i2p

public final String toBase64() {
    return Base64.encode(_data, _offset, _valid);
  }
}

代码示例来源:origin: i2p/i2p.i2p

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;
  }
}

代码示例来源:origin: i2p/i2p.i2p

public void toRawString(StringBuilder buf) {
  if (_message != null)
    buf.append(Base64.encode(_message, _payloadBeginOffset, _payloadLength));
}

代码示例来源:origin: i2p/i2p.i2p

/**
 * 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

相关文章

微信公众号

最新文章

更多