org.xipki.util.Base64类的使用及代码示例

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

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

Base64介绍

[英]A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.

On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes) compared to sun.misc.Encoder()/Decoder().

On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and about 50% faster for decoding large arrays. This implementation is about twice as fast on very small arrays (less than 30 bytes). If source/destination is a String this version is about three times as fast due to the fact that the Commons Codec result has to be recoded to a String from byte[], which is very expensive.

This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only allocates the resulting array. This produces less garbage and it is possible to handle arrays twice as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown whether Sun's sun.misc.Encoder()/Decoder() produce temporary arrays but since performance is quite low it probably does.

The encoder produces the same output as the Sun one except that the Sun's encoder appends a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the length and is probably a side effect. Both are in conformance with RFC 2045 though.
Commons codec seem to always att a trailing line separator.

Note! The encode/decode method pairs (types) come in three versions with the exact same algorithm and thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different format types. The methods not used can simply be commented out.

There is also a "fast" version of all decode methods that works the same way as the normal ones, but har a few demands on the decoded input. Normally though, these fast verions should be used if the source if the input is known and it hasn't bee tampered with.

If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom.com.

Licence (BSD):

Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com) All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the MiG InfoCom AB nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[中]这是一个非常快速且内存高效的类,用于完全按照RFC 2045在BASE64之间进行编码和解码。
在1.4.2_04及更高版本的Windows XP sp1上;),这种编码器和解码器在小型阵列(10-1000字节)上的速度大约是sun.misc.Encoder()/Decoder()的10倍,在大型阵列(10000-1000000字节)上的速度大约是[$0$]的2-3倍。
在字节数组上,编码器的编码速度比Jakarta Commons Base64编解码器快约20%,解码大型数组时快约50%。在非常小的阵列(小于30字节)上,此实现的速度大约是原来的两倍。如果源/目的地为String,则此版本的速度约为byte[]的三倍,因为Commons编解码器结果必须从byte[]重新编码为String,这非常昂贵。
这种编码/解码算法不像许多其他编解码器那样创建任何临时数组,它只分配生成的数组。这会产生更少的垃圾,并且可以处理比创建临时数组的算法大两倍的数组。(例如,雅加达公共编解码器)。尚不清楚Sun的sun.misc.Encoder()/Decoder()是否会生成临时阵列,但由于性能相当低,它可能会生成临时阵列。
编码器产生与Sun one相同的输出,除了Sun的编码器在最后一个字符不是pad时附加一个尾随行分隔符。不清楚原因,但这只会增加长度,可能是一种副作用。但两者都符合RFC 2045。
Commons编解码器似乎总是使用尾随行分隔符。
笔记编码/解码方法对(类型)有三种版本,使用完全相同的算法,因此存在大量代码冗余。这是为了不创建任何临时数组,以便在不同格式类型之间进行代码转换。没有使用的方法可以简单地注释掉。
还有一种“快速”版本的所有解码方法,其工作方式与普通方法相同,但对解码输入有一些要求。但通常情况下,如果输入的来源已知且未被篡改,则应使用这些快速版本。
如果您觉得代码有用或发现bug,请发送一封邮件至base64@miginfocom。通用域名格式。
牌照(BSD):
版权所有(c)2004,Mikael Grev,MiG InfoCom AB.(base64@miginfocom.com)保留所有权利。
在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用,无论是否进行修改:源代码的重新分发必须保留上述版权声明、此条件列表和以下免责声明。以二进制形式重新分发时,必须在分发时提供的文档和/或其他材料中复制上述版权声明、本条件列表和以下免责声明。未经事先书面许可,不得使用MiG InfoCom AB的名称或其贡献者的名称来认可或推广源自本软件的产品。
本软件由版权所有者和贡献者“按原样”提供,不承担任何明示或暗示的担保,包括但不限于对适销性和特定用途适用性的暗示担保。在任何情况下,版权所有人或贡献者均不对任何直接、间接、偶然、特殊、惩戒性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)负责,无论是在合同中还是在任何责任理论下,严格责任,或因使用本软件而产生的侵权行为(包括疏忽或其他),即使告知可能发生此类损害。

代码示例

代码示例来源:origin: org.xipki/util

/**
 *
 * The same as encodeToString(byte[], false).
 *
 * @param sArr
 *          The bytes to convert. If <code>null</code> or length 0 an empty array will be
 *          returned.
 * @return A BASE64 encoded array without line separator. Never <code>null</code>.
 */
public static final String encodeToString(byte[] sArr) {
 return encodeToString(sArr, false);
}

代码示例来源:origin: org.xipki/ca-server

IssuerEntry(int id, String b64Cert) {
 this.id = id;
 this.cert = Base64.decode(b64Cert);
}

代码示例来源:origin: org.xipki/ca-mgmt-client

private String getBase64HashValue(ResultSet rs) throws SQLException {
  if (dbControl == DbControl.XIPKI_OCSP_v4) {
   return rs.getString("HASH");
  } else { // if (dbControl == DbControl.XIPKI_CA_v4) {
   if (certhashAlgo == HashAlgo.SHA1) {
    return rs.getString("SHA1");
   } else {
    String b64Cert = rs.getString("CERT");
    byte[] encodedCert = Base64.decodeFast(b64Cert);
    return certhashAlgo.base64Hash(encodedCert);
   }
  }
 }
}

代码示例来源:origin: org.xipki/security

return Base64.decode(base64Bytes);
if (Base64.containsOnlyBase64Chars(bytes, 0, 10)) {
 return Base64.decode(bytes);

代码示例来源:origin: org.xipki.shell/ca-mgmt-shell

keystoreBytes = IoUtil.read(keystoreFile);
} else if (StringUtil.startsWithIgnoreCase(keystoreConf, "base64:")) {
 keystoreBytes = Base64.decode(keystoreConf.substring("base64:".length()));
} else {
 return signerConf;
  keyLabel, password, null);
pairs.putPair("keystore", "base64:" + Base64.encodeToString(keystoreBytes));
return pairs.getEncoded();

代码示例来源:origin: org.xipki/ca-server

int idx = 1;
if (transactionId != null) {
 ps.setString(idx++, Base64.encodeToString(transactionId));
 byte[] encodedCert = Base64.decodeFast(b64Cert);

代码示例来源:origin: org.xipki/ca-server

ksBytes = Base64.decode(keystoreConf.substring("base64:".length()));
} else {
 return signerConf;
 throw new CaMgmtException("PasswordResolverException: " + ex.getMessage(), ex);
pairs.putPair("keystore", "base64:" + Base64.encodeToString(ksBytes));
return pairs.getEncoded();

代码示例来源:origin: xipki/xipki

/**
 *
 * The same as encodeToString(byte[], false).
 *
 * @param sArr
 *          The bytes to convert. If <code>null</code> or length 0 an empty array will be
 *          returned.
 * @return A BASE64 encoded array without line separator. Never <code>null</code>.
 */
public static final String encodeToString(byte[] sArr) {
 return encodeToString(sArr, false);
}

代码示例来源:origin: org.xipki/ca-server

encodedCert = Base64.decode(rs.getString("CERT"));
try {
 subject = X509Util.getRfc4519Name(X509Util.parseBcCert(encodedCert).getSubject());
 base64Cert = Base64.encodeToString(encodedCert);
} catch (CertificateException ex) {
 throw new CaMgmtException("could not parse the certificate", ex);

代码示例来源:origin: org.xipki/ca-server

CertBasedIdentityEntry(int id, String subject, String b64Sha1Fp, String b64Cert) {
 this.id = id;
 this.subject = subject;
 this.sha1Fp = Base64.decode(Args.notBlank(b64Sha1Fp, "b64Sha1Fp"));
 this.cert = Base64.decode(Args.notBlank(b64Cert, "b64Cert"));
}

代码示例来源:origin: org.xipki/ca-dbtool

private String getBase64HashValue(ResultSet rs) throws SQLException {
  if (dbControl == DbControl.XIPKI_OCSP_v4) {
   return rs.getString("HASH");
  } else { // if (dbControl == DbControl.XIPKI_CA_v4) {
   if (certhashAlgo == HashAlgo.SHA1) {
    return rs.getString("SHA1");
   } else {
    String b64Cert = rs.getString("CERT");
    byte[] encodedCert = Base64.decodeFast(b64Cert);
    return certhashAlgo.base64Hash(encodedCert);
   }
  }
 }
}

代码示例来源:origin: xipki/xipki

public static String base64Encode(byte[] data, boolean withLineBreak) {
 return Base64.encodeToString(data, withLineBreak);
}

代码示例来源:origin: org.xipki/ca-dbtool

protected FileOrBinaryType buildFileOrBase64Binary(String base64Content, String fileName)
  throws IOException {
 if (base64Content == null) {
  return null;
 }
 return buildFileOrBinary(Base64.decode(base64Content), fileName);
}

代码示例来源:origin: org.xipki/ca-mgmt-client

private static Map<Integer, byte[]> getCas(DataSourceWrapper datasource, DbControl dbControl)
  throws DataAccessException {
 // get a list of available CAs in the target database
 String sql = "SELECT ID,CERT FROM ";
 if (dbControl == DbControl.XIPKI_CA_v4) {
  sql += "CA";
 } else if (dbControl == DbControl.XIPKI_OCSP_v4) {
  sql += "ISSUER";
 } else {
  throw new IllegalArgumentException("unknown dbControl " + dbControl);
 }
 Statement stmt = datasource.createStatement();
 Map<Integer, byte[]> caIdCertMap = new HashMap<>(5);
 ResultSet rs = null;
 try {
  rs = stmt.executeQuery(sql);
  while (rs.next()) {
   caIdCertMap.put(rs.getInt("ID"), Base64.decodeFast(rs.getString("CERT")));
  }
 } catch (SQLException ex) {
  throw datasource.translate(sql, ex);
 } finally {
  datasource.releaseResources(stmt, rs);
 }
 return caIdCertMap;
}

代码示例来源:origin: org.xipki/util

public static String base64Encode(byte[] data, boolean withLineBreak) {
 return Base64.encodeToString(data, withLineBreak);
}

代码示例来源:origin: org.xipki/ca-mgmt-client

protected FileOrBinary buildFileOrBase64Binary(String base64Content, String fileName)
  throws IOException {
 if (base64Content == null) {
  return null;
 }
 return buildFileOrBinary(Base64.decode(base64Content), fileName);
}

代码示例来源:origin: org.xipki/ca-dbtool

private static Map<Integer, byte[]> getCas(DataSourceWrapper datasource, DbControl dbControl)
  throws DataAccessException {
 // get a list of available CAs in the target database
 String sql = "SELECT ID,CERT FROM ";
 if (dbControl == DbControl.XIPKI_CA_v4) {
  sql += "CA";
 } else if (dbControl == DbControl.XIPKI_OCSP_v4) {
  sql += "ISSUER";
 } else {
  throw new IllegalArgumentException("unknown dbControl " + dbControl);
 }
 Connection conn = datasource.getConnection();
 Statement stmt = datasource.createStatement(conn);
 Map<Integer, byte[]> caIdCertMap = new HashMap<>(5);
 ResultSet rs = null;
 try {
  rs = stmt.executeQuery(sql);
  while (rs.next()) {
   caIdCertMap.put(rs.getInt("ID"), Base64.decodeFast(rs.getString("CERT")));
  }
 } catch (SQLException ex) {
  throw datasource.translate(sql, ex);
 } finally {
  datasource.releaseResources(stmt, rs);
 }
 return caIdCertMap;
}

代码示例来源:origin: org.xipki/ca-mgmt-api

private String getBase64Binary(FileOrBinaryType fileOrBinary, ZipFile zipFile)
  throws IOException {
 byte[] binary = getBinary(fileOrBinary, zipFile);
 return (binary == null) ? null : Base64.encodeToString(binary);
}

代码示例来源:origin: org.xipki/ca-server

private X509Certificate generateCert(String b64Cert) throws CaMgmtException {
 if (b64Cert == null) {
  return null;
 }
 byte[] encodedCert = Base64.decode(b64Cert);
 try {
  return X509Util.parseCert(encodedCert);
 } catch (CertificateException ex) {
  throw new CaMgmtException(ex);
 }
} // method generateCert

代码示例来源:origin: org.xipki/ca-mgmt-client

hash = rs.getString("SHA1");
} else {
 hash = certhashAlgo.base64Hash(Base64.decodeFast(rs.getString("CERT")));

相关文章