/** * Base64编码, URL安全.(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548). */ public static String encodeBase64UrlSafe(byte[] input) { return BaseEncoding.base64Url().encode(input); }
/** * Base64解码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548). * * 如果字符不合法,抛出IllegalArgumentException */ public static byte[] decodeBase64UrlSafe(CharSequence input) { return BaseEncoding.base64Url().decode(input); } }
/** * Generate a session ID. * * @return A unique session ID. */ default String generateID() { byte[] bytes = new byte[30]; rnd.nextBytes(bytes); return BaseEncoding.base64Url().encode(bytes); } }
public static UUID decodeJobId(String encodedJobId) { Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedJobId)); return UUID.fromString(new String(BaseEncoding.base64Url().decode(encodedJobId), Charsets.UTF_8)); }
public static String encodeJobId(UUID jobId) { Preconditions.checkNotNull(jobId); return BaseEncoding.base64Url().encode(jobId.toString().getBytes(Charsets.UTF_8)); }
/** Returns the cursor in an encoded form that can be used as part of a URL. */ public String toUrlSafe() { return BaseEncoding.base64Url().encode(byteString.toByteArray()); }
@Override protected String getEncodedValue(String value, boolean omitPadding) { BaseEncoding encoding = BaseEncoding.base64Url(); encoding = omitPadding ? encoding.omitPadding() : encoding; return new String(encoding.decode(value), StandardCharsets.UTF_8); }
@Override protected String getEncodedValue(String value, boolean omitPadding) { BaseEncoding encoding = BaseEncoding.base64Url(); encoding = omitPadding ? encoding.omitPadding() : encoding; return encoding.encode(value.getBytes(StandardCharsets.UTF_8)); }
protected String registryAuth(AuthConfig authConfig) { try { return BaseEncoding.base64Url().encode(new ObjectMapper().writeValueAsString(authConfig).getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
protected String registryAuth(@Nonnull AuthConfig authConfig) { try { return BaseEncoding.base64Url().encode(new ObjectMapper().writeValueAsString(authConfig).getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
/** Create a {@code Cursor} given its URL safe encoded form. */ public static Cursor fromUrlSafe(String urlSafe) { try { return Cursor.copyFrom(BaseEncoding.base64Url().decode(urlSafe)); } catch (IllegalArgumentException e) { throw new IllegalStateException("Unexpected decoding exception", e); } }
@Override public String encrypt(String data) { try { Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] salt = new byte[8]; SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.nextBytes(salt); cipher.update(salt); byte[] encrypted = cipher.doFinal(data.getBytes(Charsets.UTF_8)); return BaseEncoding.base64Url().encode(encrypted); } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) { monitor.severe(() -> format("Exception encrypting data, length: %s", data.length()), e); throw new RuntimeException(e); } } }
@Override public String decrypt(String encrypted) { try { byte[] decoded = BaseEncoding.base64Url().decode(encrypted); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decrypted = cipher.doFinal(decoded); if (decrypted == null || decrypted.length <= 8) { throw new RuntimeException("incorrect decrypted text."); } byte[] data = new byte[decrypted.length - 8]; System.arraycopy(decrypted, 8, data, 0, data.length); return new String(data, Charsets.UTF_8); } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) { monitor.severe(() -> format("Error decrypting data, length: %s", encrypted.length()), e); throw new RuntimeException(e); } } }
@Nonnull protected String registryConfigs(@Nonnull AuthConfigurations authConfigs) { try { final String json; final ObjectMapper objectMapper = new ObjectMapper(); final RemoteApiVersion apiVersion = dockerClientConfig.getApiVersion(); if (apiVersion.equals(UNKNOWN_VERSION)) { ObjectNode rootNode = objectMapper.valueToTree(authConfigs.getConfigs()); // all registries final ObjectNode authNodes = objectMapper.valueToTree(authConfigs); // wrapped in "configs":{} rootNode.setAll(authNodes); // merge 2 variants json = rootNode.toString(); } else if (apiVersion.isGreaterOrEqual(VERSION_1_19)) { json = objectMapper.writeValueAsString(authConfigs.getConfigs()); } else { json = objectMapper.writeValueAsString(authConfigs); } return BaseEncoding.base64Url().encode(json.getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
protected String registryConfigs(AuthConfigurations authConfigs) { try { final String json; final ObjectMapper objectMapper = new ObjectMapper(); final RemoteApiVersion apiVersion = dockerClientConfig.getApiVersion(); if (apiVersion.equals(UNKNOWN_VERSION)) { ObjectNode rootNode = objectMapper.valueToTree(authConfigs.getConfigs()); // all registries final ObjectNode authNodes = objectMapper.valueToTree(authConfigs); // wrapped in "configs":{} rootNode.setAll(authNodes); // merge 2 variants json = rootNode.toString(); } else if (apiVersion.isGreaterOrEqual(VERSION_1_19)) { json = objectMapper.writeValueAsString(authConfigs.getConfigs()); } else { json = objectMapper.writeValueAsString(authConfigs); } return BaseEncoding.base64Url().encode(json.getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
/** * In most cases we want to use identity equality for Trips. * However, in some cases we want a way to consistently identify trips across versions of a GTFS feed, when the * feed publisher cannot ensure stable trip IDs. Therefore we define some additional hash functions. * Hash collisions are theoretically possible, so these identifiers should only be used to detect when two * trips are the same with a high degree of probability. * An example application is avoiding double-booking of a particular bus trip for school field trips. * Using Murmur hash function. see http://programmers.stackexchange.com/a/145633 for comparison. * * @param trip a trip object within this pattern, or null to hash the pattern itself independent any specific trip. * @return the semantic hash of a Trip in this pattern as a printable String. * * TODO deal with frequency-based trips */ public String semanticHashString(Trip trip) { HashFunction murmur = Hashing.murmur3_32(); BaseEncoding encoder = BaseEncoding.base64Url().omitPadding(); StringBuilder sb = new StringBuilder(50); sb.append(encoder.encode(stopPattern.semanticHash(murmur).asBytes())); if (trip != null) { TripTimes tripTimes = scheduledTimetable.getTripTimes(trip); if (tripTimes == null) return null; sb.append(':'); sb.append(encoder.encode(tripTimes.semanticHash(murmur).asBytes())); } return sb.toString(); }
@Override public AuthFlowConfiguration generateConfiguration(String callbackBaseUrl, String id) { String encodedJobId = BaseEncoding.base64Url().encode(id.getBytes(Charsets.UTF_8)); URIBuilder builder = new URIBuilder() .setPath(config.getAuthUrl()) .setParameter("response_type", "code") .setParameter("client_id", clientId) .setParameter("redirect_uri", callbackBaseUrl) .setParameter("scope", String.join(" ", scopes)) .setParameter("state", encodedJobId); if (config.getAdditionalAuthUrlParameters() != null) { for (Entry<String, String> entry : config.getAdditionalAuthUrlParameters().entrySet()) { builder.setParameter(entry.getKey(), entry.getValue()); } } try { String url = builder.build().toString(); return new AuthFlowConfiguration(url, OAUTH_2, getTokenUrl()); } catch (URISyntaxException e) { throw new IllegalStateException("Could not produce url.", e); } }
symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(encodedSessionKey));
String encodedSessionKey = BaseEncoding.base64Url().encode(sessionKey.getEncoded());
private MimeMessage convertFromMessageToMimeMessage(Message message) throws MessagingException { byte[] emailBytes = BaseEncoding.base64Url().decode(message.getRaw()); // While we are not actually sending or receiving an email, a session is required so there will be strict parsing // of address headers when we create a MimeMessage. We are also passing in empty properties where we are expected to // supply some values because we are not actually sending or receiving any email. Session session = Session.getInstance(new Properties()); return new MimeMessage(session, new ByteArrayInputStream(emailBytes)); }