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

x33g5p2x  于2022-01-16 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(151)

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

Base64介绍

[英]Utility class for converting Base64 data to bytes and back again.
[中]用于将Base64数据转换为字节并再次转换回字节的实用程序类。

代码示例

代码示例来源:origin: apache/nifi

private byte[] decode(String encoded) throws DecoderException {
  if (this.encoding.equalsIgnoreCase("HEX")) {
    return Hex.decodeHex(encoded.toCharArray());
  } else {
    return Base64.decode(encoded);
  }
}

代码示例来源:origin: apache/cloudstack

private static String encode(byte[] input) throws UnsupportedEncodingException {
  return new String(Base64.encode(input), "UTF-8");
}

代码示例来源:origin: apache/nifi

private String base64Encode(byte[] input) {
  return Base64.toBase64String(input).replaceAll("=", "");
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public static String toBase64String(
  byte[] data,
  int off,
  int length)
{
  byte[] encoded = encode(data, off, length);
  return Strings.fromByteArray(encoded);
}

代码示例来源:origin: org.pac4j/pac4j-saml

private static void writeEncodedCertificateToFile(final File file, final byte[] certificate) {
  if (file.exists()) {
    final boolean res = file.delete();
    LOGGER.debug("Deleted file [{}]:{}", file, res);
  }
  final Base64 encoder = new Base64();
  try (final FileOutputStream fos = new FileOutputStream(file)) {
    fos.write(X509Factory.BEGIN_CERT.getBytes(StandardCharsets.UTF_8));
    fos.write("\n".getBytes(StandardCharsets.UTF_8));
    encoder.encode(certificate, fos);
    fos.write(X509Factory.END_CERT.getBytes(StandardCharsets.UTF_8));
    fos.flush();
  } catch (final Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
}

代码示例来源:origin: redfish64/TinyTravelTracker

public static String toBase64String(
  byte[] data,
  int    off,
  int    length)
{
  byte[] encoded = encode(data, off, length);
  return Strings.fromByteArray(encoded);
}

代码示例来源:origin: floragunncom/search-guard

private static byte[] readPrivateKey(InputStream in) throws KeyException {
  String content;
  try {
    content = readContent(in);
  } catch (IOException e) {
    throw new KeyException("failed to read key input stream", e);
  }
  Matcher m = KEY_PATTERN.matcher(content);
  if (!m.find()) {
    throw new KeyException("could not find a PKCS #8 private key in input stream" +
        " (see http://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
  }
  return Base64.decode(m.group(1));
}

代码示例来源:origin: apache/cloudstack

public String encode(String password, byte[] salt) throws UnsupportedEncodingException, NoSuchAlgorithmException {
  byte[] passwordBytes = password.getBytes("UTF-8");
  byte[] hashSource = new byte[passwordBytes.length + salt.length];
  System.arraycopy(passwordBytes, 0, hashSource, 0, passwordBytes.length);
  System.arraycopy(salt, 0, hashSource, passwordBytes.length, salt.length);
  // 2. Hash the password with the salt
  MessageDigest md = MessageDigest.getInstance("SHA-256");
  md.update(hashSource);
  byte[] digest = md.digest();
  return new String(Base64.encode(digest));
}

代码示例来源:origin: apache/nifi

private String encode(byte[] rawBytes) {
  if (this.encoding.equalsIgnoreCase("HEX")) {
    return Hex.encodeHexString(rawBytes);
  } else {
    return Base64.toBase64String(rawBytes);
  }
}

代码示例来源:origin: cloudfoundry/uaa

public UaaAuthenticationDetails(HttpServletRequest request, String clientId) {
  WebAuthenticationDetails webAuthenticationDetails = new WebAuthenticationDetails(request);
  this.origin = webAuthenticationDetails.getRemoteAddress();
  this.sessionId = webAuthenticationDetails.getSessionId();
  if (clientId == null) {
    this.clientId = request.getParameter("client_id");
    if(!StringUtils.hasText(this.clientId)) {
      String authHeader = request.getHeader("Authorization");
      if(StringUtils.hasText(authHeader) && authHeader.startsWith("Basic ")) {
        String decodedCredentials = new String(Base64.decode(authHeader.substring("Basic ".length())));
        String[] split = decodedCredentials.split(":");
        if (split == null || split.length == 0)
          throw new BadCredentialsException("Invalid basic authentication token");
        this.clientId = split[0];
      }
    }
  } else {
    this.clientId = clientId;
  }
  this.addNew = Boolean.parseBoolean(request.getParameter(ADD_NEW));
  this.loginHint = UaaLoginHint.parseRequestParameter(request.getParameter("login_hint"));
  this.parameterMap = request.getParameterMap();
}

代码示例来源:origin: apache/cloudstack

private String generatePassword() throws ServerApiException {
  try {
    final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
    final byte bytes[] = new byte[20];
    randomGen.nextBytes(bytes);
    return new String(Base64.encode(bytes), "UTF-8");
  } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
    throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
  }
}

代码示例来源:origin: spotify/helios

public static String digest(final String user, final String password) {
 return Base64.toBase64String(Hash.sha1digest(
   String.format("%s:%s", user, password).getBytes()));
}

代码示例来源:origin: apache/nifi

byte[] iv = Base64.decode(IV_B64);
if (iv.length < IV_LENGTH) {
  throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
  byte[] cipherBytes = Base64.decode(CIPHERTEXT_B64);

代码示例来源:origin: apache/cloudstack

private String generatePassword() throws ServerApiException {
    try {
      final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
      final byte bytes[] = new byte[20];
      randomGen.nextBytes(bytes);
      return new String(Base64.encode(bytes), "UTF-8");
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
      throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
    }
  }
}

代码示例来源:origin: com.dracoon/dracoon-crypto-sdk

/**
 * Converts a byte array into a Base64 encoded string.
 *
 * @param bytes The byte array to convert.
 *
 * @return The Base64 encoded string.
 */
public static String byteArrayToString(byte[] bytes) {
  return Base64.toBase64String(bytes);
}

代码示例来源:origin: apache/cloudstack

private static byte[] decode(String input) throws UnsupportedEncodingException {
    return Base64.decode(input.getBytes("UTF-8"));
  }
}

代码示例来源:origin: apache/cloudstack

@Override
public String encode(String password) {
  // 1. Generate the salt
  SecureRandom randomGen;
  try {
    randomGen = SecureRandom.getInstance("SHA1PRNG");
    byte salt[] = new byte[s_saltlen];
    randomGen.nextBytes(salt);
    String saltString = new String(Base64.encode(salt));
    String hashString = encode(password, salt);
    // 3. concatenate the two and return
    return saltString + ":" + hashString;
  } catch (NoSuchAlgorithmException e) {
    throw new CloudRuntimeException("Unable to hash password", e);
  } catch (UnsupportedEncodingException e) {
    throw new CloudRuntimeException("Unable to hash password", e);
  }
}

代码示例来源:origin: radixdlt/radixdlt-java

@Override
  public String toString() {
    return Base64.toBase64String(hash);
  }
}

代码示例来源:origin: apache/cloudstack

/**
 * Decodes base64 encoded private key to PrivateKey
 * @param privateKey encoded private key string
 * @return returns PrivateKey
 */
public static PrivateKey decodePrivateKey(String privateKey) {
  byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(privateKey);
  PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec(sigBytes);
  KeyFactory keyFactory = CertUtils.getKeyFactory();
  if (keyFactory == null)
    return null;
  try {
    return keyFactory.generatePrivate(pkscs8KeySpec);
  } catch (InvalidKeySpecException e) {
    s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
  }
  return null;
}

代码示例来源:origin: apache/cloudstack

/**
 * Returns base64 encoded PublicKey
 * @param key PublicKey
 * @return public key encoded string
 */
public static String encodePublicKey(PublicKey key) {
  try {
    KeyFactory keyFactory = CertUtils.getKeyFactory();
    if (keyFactory == null) return null;
    X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
    return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
  } catch (InvalidKeySpecException e) {
    s_logger.error("Unable to get KeyFactory:" + e.getMessage());
  }
  return null;
}

相关文章

微信公众号

最新文章

更多