net.i2p.data.Base64.decode()方法的使用及代码示例

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

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

Base64.decode介绍

[英]Decodes data from Base64 notation using the I2P alphabet. 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.
[中]使用I2P字母表解码Base64符号中的数据。从0.9.14开始,如果剩余位为零,则不需要尾随“=”。在此之前,后面的1、2或3个字符被忽略。从0.9.14开始,在“=”后面尾随垃圾将导致错误。在此之前,它被忽略了。从0.9.14开始,空格将导致错误。在此之前,它被忽略了。

代码示例

代码示例来源: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

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

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

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

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

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

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

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

String b64 = k.substring(PROP_META_MAGNET_PREFIX.length());
b64 = b64.replace('$', '=');
byte[] ih = Base64.decode(b64);

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

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

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

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

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

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

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

if (_nonceCleanCounter.incrementAndGet() % 16 == 0)
  cleanNonces();
byte[] n = Base64.decode(b64);
if (n == null || n.length != NONCE_BYTES)
  return AuthResult.AUTH_BAD;

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

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

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

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

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

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

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

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

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

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

代码示例来源: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

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

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

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

相关文章

微信公众号

最新文章

更多