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; }
CodePointIterator r = CodePointIterator.ofChars(cryptString, minor == 0 ? 3 : 4); try { int cost = Integer.parseInt(r.limitedTo(2).drainToString()); if (r.hasNext() && r.peekNext() != '$') { throw log.invalidKeySpecCostMustBeTwoDigitInteger(); if (! r.hasNext()) { throw log.invalidKeySpecUnexpectedEndOfPasswordString(); r.next(); byte[] decodedSalt = r.limitedTo(22).base64Decode(BCRYPT, false).drain(); byte[] decodedPassword = r.limitedTo(31).base64Decode(BCRYPT, false).drain();
private static String getEncodedJson(JsonObject jsonObject) { return CodePointIterator.ofString(jsonObject.toString()).asUtf8().base64Encode(BASE64_URL, false).drainToString(); }
/** * 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 CipherSuiteSelector parseSpecial(final CipherSuiteSelector current, final CodePointIterator i) { String word = i.delimitedBy('=', ':').drainToString(); switch (word) { case "STRENGTH": { if (i.hasNext() && i.next() == '=') { throw ElytronMessages.log.mechSelectorTokenNotAllowed("=", i.getIndex(), i.drainToString()); } return current.sortByAlgorithmKeyLength(); } default: { throw ElytronMessages.log.mechSelectorUnknownToken(word, i.drainToString()); } } }
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 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; }
/** * 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 static Password parseUnixMD5CryptPasswordString(final char[] cryptString) throws InvalidKeySpecException { assert cryptString[0] == '$'; // previously tested by doIdentifyAlgorithm assert cryptString[1] == '1'; // previously tested by doIdentifyAlgorithm assert cryptString[2] == '$'; // previously tested by doIdentifyAlgorithm CodePointIterator r = CodePointIterator.ofChars(cryptString, 3); try { final byte[] salt = r.delimitedBy('$').drainToString().getBytes(StandardCharsets.ISO_8859_1); if (! r.hasNext()) { throw log.invalidKeySpecNoSaltTerminatorGiven(); } r.next(); // skip $ final byte[] decoded = r.base64Decode(MOD_CRYPT_LE, false).limitedTo(MD5_IDX_REV.length).drain(); if (decoded.length != MD5_IDX.length) { throw log.invalidHashLength(); } byte[] hash = ByteIterator.ofBytes(decoded, MD5_IDX_REV).drain(); return UnixMD5CryptPassword.createRaw(UnixMD5CryptPassword.ALGORITHM_CRYPT_MD5, salt, hash); } catch (NoSuchElementException e) { throw log.invalidKeySpecUnexpectedEndOfPasswordStringWithCause(e); } }
static String arrayToRealmListProperty(String[] realms, int escapeCharacter, int... delims) { if (realms == null) { return null; } final int[] escapeCharacterAndDelims = Arrays.copyOf(delims, delims.length + 1); escapeCharacterAndDelims[escapeCharacterAndDelims.length - 1] = escapeCharacter; StringBuilder realmList = new StringBuilder(); for (int i = 0; i < realms.length; i++) { if (i != 0) { addDelims(realmList, delims); } CodePointIterator cpi = CodePointIterator.ofString(realms[i]); CodePointIterator di = cpi.delimitedBy(escapeCharacterAndDelims); while (cpi.hasNext()) { if (di.hasNext()) { di.drainTo(realmList); } else { realmList.append((char) escapeCharacter); // escape the delimiter or escape character realmList.append((char) cpi.next()); } } } return realmList.toString(); }
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 static int parseModCryptIterationCount(final CodePointIterator reader, final int minIterations, final int maxIterations, final int defaultIterations) throws InvalidKeySpecException { int iterationCount; final CodePointIterator dr = reader.delimitedBy('$'); try { if (dr.limitedTo(7).contentEquals(CodePointIterator.ofString("rounds="))) { iterationCount = 0; int ch; while (dr.hasNext()) { ch = dr.next(); if (iterationCount != maxIterations) { if (ch >= '0' && ch <= '9') { if (! reader.hasNext()) { throw log.invalidKeySpecNoIterationCountTerminatorGiven(); reader.next(); // skip $ } else { iterationCount = defaultIterations;
private static Password parseUnixDESCryptPasswordString(char[] cryptString) throws InvalidKeySpecException { assert cryptString.length == 13; // previously tested by doIdentifyAlgorithm CodePointIterator r = CodePointIterator.ofChars(cryptString); // 12 bit salt int s0 = MOD_CRYPT.decode(r.next()); int s1 = MOD_CRYPT.decode(r.next()); short salt = (short) (s0 | s1 << 6); // 64 bit hash byte[] hash = r.base64Decode(MOD_CRYPT, false).limitedTo(8).drain(); return UnixDESCryptPassword.createRaw(UnixDESCryptPassword.ALGORITHM_CRYPT_DES, salt, hash); }
static byte[] generateNonce() { SecureRandom random = new SecureRandom(); byte[] nonceData = new byte[NONCE_SIZE]; random.nextBytes(nonceData); return ByteIterator.ofBytes(nonceData).base64Encode().drainToString().getBytes(StandardCharsets.US_ASCII); }
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(); }
public byte[] handleServerResponse(byte[] serverMessage) { // got a successful response if (serverMessage.length == 0) { return null; } // otherwise, server responded with a error message try { String errorMessage = ByteIterator.ofBytes(serverMessage).asUtf8String().base64Decode().asUtf8String().drainToString(); log.debugf("Got error message from server [%s].", errorMessage); } catch (Exception e) { log.errorf(e, "Server returned an unexpected message that is probably an error but could not be parsed."); } // send a last message to abort the authentication return new ByteStringBuilder().append(KV_DELIMITER).toArray(); } }
try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); ByteIterator byteIterator = CodePointIterator.ofChars(nonce.toCharArray()).base64Decode(); byte[] nonceBytes = byteIterator.drain(); if (nonceBytes.length != PREFIX_LENGTH + messageDigest.getDigestLength()) { String saltString = salt == null ? "null" : ByteIterator.ofBytes(salt).hexEncode().drainToString(); log.tracef("Nonce %s rejected due to failed comparison using secret key with seed %s.", nonce, saltString);
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(); } }