private static PasswordSpec decodeDigestPasswordSpec(ByteIterator iterator) { String username = iterator.drainToUtf8(iterator.getPackedBE32()); String realm = iterator.drainToUtf8(iterator.getPackedBE32()); byte[] digest = iterator.drain(); return new DigestPasswordSpec(username, realm, digest); }
/** * Convenience method to directly drain a certain number of bytes to a UTF-8 string. If fewer than {@code count} * bytes are available, only the available bytes will be used to construct the string. * * @param count the maximum number of bytes to consume * @return the UTF-8 string */ public String drainToUtf8(int count) { return new String(drain(count), StandardCharsets.UTF_8); }
/** * Drain up to {@code count} bytes from this iterator, returning the result. * * @param count the number of bytes to read * @return the array of consumed bytes (may be smaller than {@code count}) */ public byte[] drain(int count) { if (count == 0) return NO_BYTES; final byte[] b = new byte[count]; final int cnt = drain(b); return cnt == 0 ? NO_BYTES : cnt < b.length ? Arrays.copyOf(b, cnt) : b; }
/** * Convenience method to directly drain a certain number of bytes to a Latin-1 string. If fewer than {@code count} * bytes are available, only the available bytes will be used to construct the string. * * @param count the maximum number of bytes to consume * @return the Latin-1 string */ public String drainToLatin1(int count) { return new String(drain(count), StandardCharsets.ISO_8859_1); }
public int read(final byte[] b, final int off, final int len) throws IOException { if (len == 0) return 0; if (! iter.hasNext()) return - 1; return iter.drain(b, off, len); } }
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 byte[] decodeUncheckedStringAsBytes() throws ASN1Exception { int length = readLength(); byte[] result = new byte[length]; if ((length != 0) && (bi.drain(result, 0, length) != length)) { throw log.asnUnexpectedEndOfInput(); } return result; }
@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); } }
@Override public byte[] decodeOctetString() throws ASN1Exception { readTag(OCTET_STRING_TYPE); int length = readLength(); byte[] result = new byte[length]; if ((length != 0) && (bi.drain(result, 0, length) != length)) { throw log.asnUnexpectedEndOfInput(); } return result; }
private static Password createSimpleDigestPassword(String algorithm, int prefixSize, byte[] userPassword) throws InvalidKeySpecException { int length = userPassword.length - prefixSize; byte[] digest = CodePointIterator.ofUtf8Bytes(userPassword, prefixSize, length).base64Decode().drain(); return SimpleDigestPassword.createRaw(algorithm, digest); }
private static byte[] mask(final String algorithm, final char[] initialKeyMaterial, final int iterationCount, final byte[] salt, final char[] chars) throws InvalidKeySpecException { final Cipher cipher = getCipher(algorithm, initialKeyMaterial, iterationCount, salt, Cipher.ENCRYPT_MODE); try { return cipher.doFinal(CodePointIterator.ofChars(chars).asUtf8().drain()); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new InvalidKeySpecException(e); } }
private static void composeCryptBasedPassword(ByteArrayOutputStream out, UnixDESCryptPassword password) throws IOException { out.write(ModularCrypt.MOD_CRYPT.encode(password.getSalt() & 0x3f)); out.write(ModularCrypt.MOD_CRYPT.encode(password.getSalt() >> 6 & 0x3f)); out.write(ByteIterator.ofBytes(password.getHash()).base64Encode(ModularCrypt.MOD_CRYPT, false).asUtf8().drain()); }
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); }