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 PasswordSpec decodeClearPasswordSpec(ByteIterator iterator) { return new ClearPasswordSpec(iterator.asUtf8String().drainToString().toCharArray()); } }
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(); }
/** * Convert the given OTP hash into hexadecimal format. * * @param otp the OTP hash * @return the hexadecimal value that corresponds to the given OTP hash */ public static String convertToHex(byte[] otp) { return ByteIterator.ofBytes(otp).hexEncode().drainToString(); }
private static CipherSuitePredicate parseAndPredicate(CipherSuitePredicate item, final CodePointIterator i) { final ArrayList<CipherSuitePredicate> list = new ArrayList<>(); list.add(item); do { list.add(getSimplePredicateByName(i.delimitedBy('+', ':', ',', ' ').drainToString())); } while (i.hasNext() && i.next() == '+'); return CipherSuitePredicate.matchAll(list.toArray(new CipherSuitePredicate[list.size()])); }
private static CipherSuitePredicate parsePredicate(final CodePointIterator i) { return parsePredicate(i, i.delimitedBy('+', ':', ',', ' ').drainToString()); }
@Override public String createLogoutParameter(String sessionId) { try { Signature signature = Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM); signature.initSign(this.keyPair.getPrivate()); Base64.Encoder urlEncoder = Base64.getUrlEncoder(); return sessionId + "." + ByteIterator.ofBytes(urlEncoder.encode(ByteIterator.ofBytes(sessionId.getBytes(StandardCharsets.UTF_8)).sign(signature).drain())).asUtf8String().drainToString(); } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { throw new IllegalStateException(e); } }
static String base64UrlEncode(byte[] data) { return ByteIterator.ofBytes(data).base64Encode(BASE64_URL, false).drainToString(); }
private static char[] unmask(final String algorithm, final char[] initialKeyMaterial, final int iterationCount, final byte[] salt, final byte[] bytes) throws InvalidKeySpecException { final Cipher cipher = getCipher(algorithm, initialKeyMaterial, iterationCount, salt, Cipher.DECRYPT_MODE); try { return ByteIterator.ofBytes(cipher.doFinal(bytes)).asUtf8String().drainToString().toCharArray(); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new InvalidKeySpecException(e); } }
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()); } } }
private static String getEncodedJson(JsonObject jsonObject) { return CodePointIterator.ofString(jsonObject.toString()).asUtf8().base64Encode(BASE64_URL, false).drainToString(); }
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; } }
public String generateHashedHexURP(final String userName, final String realm, final char[] password, final boolean utf8StringConversion) { byte[] hashedURP = generateHashedURP(userName, realm, password, utf8StringConversion); return ByteIterator.ofBytes(hashedURP).hexEncode().drainToString(); }
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); }
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 CipherSuitePredicate parsePredicate(final CodePointIterator i, final String word) { CipherSuitePredicate item = getSimplePredicateByName(word); if (i.hasNext() && i.next() == '+') { if (item == null) { throw ElytronMessages.log.mechSelectorTokenNotAllowed("+", i.getIndex(), i.drainToString()); } return parseAndPredicate(item, i); } else { return item; } }
private String encodeUsingAlphabet(byte[] payload) { if (picketBoxCompatibility) { return picketBoxBased64Encode(payload); } else { CodePointIterator codePointIterator = isBase64(alphabet) ? ByteIterator.ofBytes(payload).base64Encode(getAlphabet64(alphabet), usePadding) : ByteIterator.ofBytes(payload).base32Encode(getAlphabet32(alphabet)); return codePointIterator.drainToString(); } }
@Override public void persistCredential(final Credential credential) throws RealmUnavailableException { OneTimePassword password = credential.castAndApply(PasswordCredential.class, c -> c.getPassword(OneTimePassword.class)); try { Attributes attributes = new BasicAttributes(); attributes.put(algorithmAttributeName, password.getAlgorithm()); attributes.put(hashAttributeName, ByteIterator.ofBytes(password.getHash()).base64Encode().drainToString()); attributes.put(seedAttributeName, password.getSeed()); attributes.put(sequenceAttributeName, Integer.toString(password.getSequenceNumber())); context.modifyAttributes(distinguishedName, DirContext.REPLACE_ATTRIBUTE, attributes); } catch (NamingException e) { throw log.ldapRealmCredentialPersistingFailed(credential.toString(), distinguishedName, e); } }
private void sendChallenge(byte[] responseToken, HttpServerResponse response, int statusCode) { if (httpSpnego.isTraceEnabled()) { httpSpnego.tracef("Sending intermediate challenge: %s", Arrays2.objectToString(responseToken)); } if (responseToken == null) { response.addResponseHeader(WWW_AUTHENTICATE, NEGOTIATE); } else { String responseConverted = ByteIterator.ofBytes(responseToken).base64Encode().drainToString(); response.addResponseHeader(WWW_AUTHENTICATE, CHALLENGE_PREFIX + responseConverted); } if (statusCode != 0) { response.setStatusCode(statusCode); } }