Tokenizer(final String string) { this.string = string; this.i = CodePointIterator.ofString(string); }
/** * Get a code point iterator for a string. * * @param string the string * @return the code point iterator */ public static CodePointIterator ofString(final String string) { return ofString(string, 0, string.length()); }
ParsingIterator(final String string) { this.string = string; cpi = CodePointIterator.ofString(string); b = new StringBuilder(); }
/** * Determine if the remaining contents of this iterator are identical to the given string. If the * contents are not equal, the iterator will be positioned at the location of the first difference (i.e. the code point * returned by {@link #next()} will be the differing code point. If the contents are equal, the iterator will be * positioned at the end of its contents. * * @param other the other string * @return {@code true} if the contents are equal, {@code false} otherwise */ public boolean contentEquals(String other) { return contentEquals(CodePointIterator.ofString(other)); }
private static byte[] getReplayNonce(HttpURLConnection connection) throws AcmeException { String nonce = connection.getHeaderField(REPLAY_NONCE); if (nonce == null) { return null; } return CodePointIterator.ofString(nonce).base64Decode(BASE64_URL, false).drain(); }
private static byte[] decodeColumn(String string, Encoding encoding) { switch (encoding) { case BASE64: return CodePointIterator.ofString(string).base64Decode(Base64Alphabet.STANDARD, false).drain(); case HEX: return CodePointIterator.ofString(string).hexDecode().drain(); } throw new IllegalStateException(); }
private void parseCertificate(final List<Credential> credentials, final XMLStreamReader streamReader) throws RealmUnavailableException, XMLStreamException { parseCredential(streamReader, (algorithm, format, text) -> { if (algorithm == null) algorithm = "X.509"; if (format == null) format = X509_FORMAT; try { final CertificateFactory certificateFactory = CertificateFactory.getInstance(algorithm); credentials.add(new X509CertificateChainPublicCredential((X509Certificate) certificateFactory.generateCertificate( CodePointIterator.ofString(text).base64Decode().asInputStream()))); } catch (CertificateException | ClassCastException e) { throw ElytronMessages.log.fileSystemRealmCertificateReadError(format, path, streamReader.getLocation().getLineNumber(), name); } }); }
private void parsePublicKey(final List<Credential> credentials, final XMLStreamReader streamReader) throws RealmUnavailableException, XMLStreamException { parseCredential(streamReader, (algorithm, format, text) -> { if (algorithm == null) { throw ElytronMessages.log.fileSystemRealmMissingAttribute("algorithm", path, streamReader.getLocation().getLineNumber(), name); } if (format == null) { format = X509_FORMAT; } else if (!X509_FORMAT.equals(format)) { throw ElytronMessages.log.fileSystemRealmUnsupportedKeyFormat(format, path, streamReader.getLocation().getLineNumber(), name); } try { KeyFactory keyFactory = KeyFactory.getInstance(algorithm); credentials.add(new PublicKeyCredential(keyFactory.generatePublic(new PKCS8EncodedKeySpec(CodePointIterator.ofString(text).base64Decode().drain())))); } catch (NoSuchAlgorithmException e) { throw ElytronMessages.log.fileSystemRealmUnsupportedKeyAlgorithm(format, path, streamReader.getLocation().getLineNumber(), name, e); } catch (InvalidKeySpecException e) { throw ElytronMessages.log.fileSystemRealmUnsupportedKeyFormat(format, path, streamReader.getLocation().getLineNumber(), name); } }); }
private static String getEncodedJson(JsonObject jsonObject) { return CodePointIterator.ofString(jsonObject.toString()).asUtf8().base64Encode(BASE64_URL, false).drainToString(); }
private static <P> P[] parseMultiPem(final ConfigurationXMLStreamReader reader, final Class<P> pemType, final IntFunction<P[]> ctor) throws ConfigXMLParseException { requireNoAttributes(reader); final Iterator<PemEntry<?>> pemContent = Pem.parsePemContent(CodePointIterator.ofString(reader.getElementText())); if (! reader.hasNext()) throw reader.unexpectedDocumentEnd(); final ArrayList<P> arrayList = new ArrayList<>(); while (pemContent.hasNext()) { final PemEntry<?> pemEntry = pemContent.next(); final P pem = pemEntry.tryCast(pemType); if (pem == null) throw xmlLog.xmlWrongPemType(reader, pemType, pemEntry.getEntry().getClass()); arrayList.add(pem); } if (arrayList.isEmpty()) throw xmlLog.xmlNoPemContent(reader); return arrayList.toArray(ctor.apply(arrayList.size())); }
/** * Convert the given hexadecimal OTP value into a hash. * * @param otp the OTP in hexadecimal format * @return the OTP hash that corresponds to the given hexadecimal value * @throws SaslException if an error occurs while parsing the hexadecimal value */ public static byte[] convertFromHex(String otp) throws SaslException { final CodePointIterator cpi = CodePointIterator.ofString(otp); final CodePointIterator di = cpi.delimitedBy(DELIMS); // Remove any white space final StringBuilder sb = new StringBuilder(); while (di.hasNext()) { di.drainTo(sb); skipDelims(di, cpi); } return CodePointIterator.ofString(sb.toString()).hexDecode().drain(); }
private String nameFor(Path path) { String fileName = path.toString(); fileName = fileName.substring(0, fileName.length() - 4); // remove ".xml" if (encoded) { CodePointIterator it = CodePointIterator.ofString(fileName); it.delimitedBy('-').skipAll(); it.next(); // skip '-' fileName = it.base32Decode(Base32Alphabet.STANDARD, false) .asUtf8String().drainToString(); } return fileName; }
static String[] dictionaryPropertyToArray(String property) throws SaslException { String[] dictionary = new String[OTP.DICTIONARY_SIZE]; CodePointIterator cpi = CodePointIterator.ofString(property); CodePointIterator di = cpi.delimitedBy(OTP.DICTIONARY_DELIMITER); for (int i = 0; i < dictionary.length; i++) { dictionary[i] = di.drainToString(); skipDelims(di, cpi, OTP.DICTIONARY_DELIMITER); } return dictionary; } }
private byte[] decodeUsingAlphabet(String payload) { if (picketBoxCompatibility) { return picketBoxBase64Decode(payload); } else { ByteIterator byteIterator = isBase64(alphabet) ? CodePointIterator.ofString(payload).base64Decode(getAlphabet64(alphabet), usePadding) : CodePointIterator.ofString(payload).base32Decode(getAlphabet32(alphabet)); return byteIterator.drain(); } }
private static String getKeyValue(final String requiredKey, final String keyAndValue) throws IllegalArgumentException { // key:value final CodePointIterator cpi = CodePointIterator.ofString(keyAndValue); final CodePointIterator di = cpi.delimitedBy(':'); if (! requiredKey.equalsIgnoreCase(di.drainToString())) { throw log.invalidCertificateExtensionStringValue(keyAndValue); } skipDelims(di, cpi, ':'); return di.drainToString(); }
private static <P> P parsePem(final ConfigurationXMLStreamReader reader, final Class<P> pemType) throws ConfigXMLParseException { requireNoAttributes(reader); final Iterator<PemEntry<?>> pemContent = Pem.parsePemContent(CodePointIterator.ofString(reader.getElementText())); if (! reader.hasNext()) throw reader.unexpectedDocumentEnd(); if (! pemContent.hasNext()) throw xmlLog.xmlNoPemContent(reader); final PemEntry<?> pemEntry = pemContent.next(); final P pem = pemEntry.tryCast(pemType); if (pem == null) throw xmlLog.xmlWrongPemType(reader, pemType, pemEntry.getEntry().getClass()); return pem; }
private static List<AccessDescription> getAccessDescriptions(final String extensionValue) throws IllegalArgumentException { // method:location-type:location-value(,method:location-type:location-value)* final CodePointIterator cpi = CodePointIterator.ofString(extensionValue); final CodePointIterator di = cpi.delimitedBy(DELIMS); if (! di.hasNext()) { throw log.invalidCertificateExtensionStringValue(extensionValue); } List<AccessDescription> accessDescriptions = new ArrayList<>(); while (di.hasNext()) { accessDescriptions.add(getAccessDescription(di.drainToString())); skipDelims(di, cpi, DELIMS); } return accessDescriptions; }
private static List<GeneralName> getGeneralNames(final String extensionValue) throws IllegalArgumentException { // type:val(,type:val)* final CodePointIterator cpi = CodePointIterator.ofString(extensionValue); final CodePointIterator di = cpi.delimitedBy(DELIMS); if (! di.hasNext()) { throw log.invalidCertificateExtensionStringValue(extensionValue); } List<GeneralName> generalNames = new ArrayList<>(); while (di.hasNext()) { generalNames.add(getGeneralName(di.drainToString())); skipDelims(di, cpi, DELIMS); } return generalNames; }
/** * Get the key authorization string for this challenge. * * @param account the ACME account information to use (must not be {@code null}) * @return the key authorization string for this challenge * @throws AcmeException if the key authorization string cannot be determined */ public String getKeyAuthorization(AcmeAccount account) throws AcmeException { Assert.checkNotNullParam("account", account); JsonObject jwk = getJwk(account.getPublicKey(), account.getAlgHeader()); byte[] jwkWithoutWhitespace = CodePointIterator.ofString(jwk.toString()).skip(Character::isWhitespace).skipCrLf().asUtf8().drain(); try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); messageDigest.update(jwkWithoutWhitespace); byte[] jwkThumbprint = messageDigest.digest(); return token + "." + base64UrlEncode(jwkThumbprint); } catch (NoSuchAlgorithmException e) { throw acme.unableToDetermineKeyAuthorizationString(e); } }
private static AccessDescription getAccessDescription(final String methodAndTypeAndValue) throws IllegalArgumentException { // method:location-type:location-value final CodePointIterator cpi = CodePointIterator.ofString(methodAndTypeAndValue); final CodePointIterator di = cpi.delimitedBy(':'); if (! di.hasNext()) { throw log.invalidCertificateExtensionStringValue(methodAndTypeAndValue); } final String accessMethodId = oidFromMethod(di.drainToString()); skipDelims(di, cpi, ':'); final String typeAndValue = cpi.drainToString(); final GeneralName accessLocation = getGeneralName(typeAndValue); return new AccessDescription(accessMethodId, accessLocation); }