Search in sources :

Example 1 with Decoder

use of java.util.Base64.Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64 method testMalformedPadding.

private static void testMalformedPadding() throws Throwable {
    Object[] data = new Object[] { // illegal ending unit
    "$=#", // illegal ending unit
    "", // illegal ending unit
    0, // dangling single byte
    "A", // dangling single byte
    "", // dangling single byte
    0, "A=", "", 0, "A==", "", 0, "QUJDA", "ABC", 4, "QUJDA=", "ABC", 4, "QUJDA==", "ABC", 4, // unnecessary padding
    "=", // unnecessary padding
    "", // unnecessary padding
    0, //"ABC".encode() -> "QUJD"
    "QUJD=", //"ABC".encode() -> "QUJD"
    "ABC", //"ABC".encode() -> "QUJD"
    4, // incomplete padding
    "AA=", // incomplete padding
    "", // incomplete padding
    0, "QQ=", "", 0, // incorrect padding
    "QQ=N", // incorrect padding
    "", // incorrect padding
    0, "QQ=?", "", 0, "QUJDQQ=", "ABC", 4, "QUJDQQ=N", "ABC", 4, "QUJDQQ=?", "ABC", 4 };
    Base64.Decoder[] decs = new Base64.Decoder[] { Base64.getDecoder(), Base64.getUrlDecoder(), Base64.getMimeDecoder() };
    for (Base64.Decoder dec : decs) {
        for (int i = 0; i < data.length; i += 3) {
            final String srcStr = (String) data[i];
            final byte[] srcBytes = srcStr.getBytes("ASCII");
            final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
            byte[] expected = ((String) data[i + 1]).getBytes("ASCII");
            int pos = (Integer) data[i + 2];
            // decode(byte[])
            checkIAE(() -> dec.decode(srcBytes));
            // decode(String)
            checkIAE(() -> dec.decode(srcStr));
            // decode(ByteBuffer)
            checkIAE(() -> dec.decode(srcBB));
            // wrap stream
            checkIOE(new Testable() {

                public void test() throws IOException {
                    try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                        while (is.read() != -1) ;
                    }
                }
            });
        }
    }
}
Also used : Base64(java.util.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with Decoder

use of java.util.Base64.Decoder in project riposte by Nike-Inc.

the class BasicAuthSecurityValidator method validateSecureRequestForEndpoint.

@Override
public void validateSecureRequestForEndpoint(RequestInfo<?> requestInfo, Endpoint<?> endpoint) {
    String authorizationHeader = requestInfo.getHeaders().get("Authorization");
    if (authorizationHeader == null) {
        throw new Unauthorized401Exception("Missing authorization header.", requestInfo.getPath(), null);
    }
    final String[] authSplit = authorizationHeader.split(" ");
    if (authSplit.length != 2 || !"Basic".equals(authSplit[0])) {
        throw new Unauthorized401Exception("Authorization header does not contain Basic", requestInfo.getPath(), authorizationHeader);
    }
    Base64.Decoder decoder = Base64.getDecoder();
    byte[] decodedBytes;
    try {
        decodedBytes = decoder.decode(authSplit[1]);
    } catch (IllegalArgumentException ex) {
        throw new Unauthorized401Exception("Malformed Authorization header (not Base64 encoded), caused by: " + ex.toString(), requestInfo.getPath(), authorizationHeader);
    }
    String pair = new String(decodedBytes);
    String[] userDetails = pair.split(":", 2);
    if (userDetails.length != 2) {
        throw new Unauthorized401Exception("Malformed Authorization header.", requestInfo.getPath(), authorizationHeader);
    }
    String username = userDetails[0];
    String password = userDetails[1];
    if (!username.equals(expectedUsername) || !password.equals(expectedPassword)) {
        throw new Unauthorized401Exception("Invalid username or password", requestInfo.getPath(), authorizationHeader);
    }
}
Also used : Base64(java.util.Base64) Unauthorized401Exception(com.nike.riposte.server.error.exception.Unauthorized401Exception)

Example 3 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testRoundTrip_allBytes_mime_singleLine.

/**
     * Checks that if the lineSeparator is empty or the line length is {@code <= 3}
     * or larger than the data to be encoded, a single line is returned.
     */
public void testRoundTrip_allBytes_mime_singleLine() {
    Decoder decoder = Base64.getMimeDecoder();
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(76, new byte[0]), decoder);
    // Line lengths <= 3 mean no wrapping; the separator is ignored in that case.
    byte[] separator = new byte[] { '*' };
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MIN_VALUE, separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(-1, separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(0, separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(1, separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(2, separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(3, separator), decoder);
    // output fits into the permitted line length
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(ALL_BYTE_VALUES_ENCODED.length(), separator), decoder);
    checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MAX_VALUE, separator), decoder);
}
Also used : Decoder(java.util.Base64.Decoder)

Example 4 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testRoundTrip_wrap_url.

public void testRoundTrip_wrap_url() throws Exception {
    Encoder encoder = Base64.getUrlEncoder();
    Decoder decoder = Base64.getUrlDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Example 5 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testRoundtrip_wrap_mime.

public void testRoundtrip_wrap_mime() throws Exception {
    Encoder encoder = Base64.getMimeEncoder();
    Decoder decoder = Base64.getMimeDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Aggregations

Decoder (java.util.Base64.Decoder)26 Base64 (java.util.Base64)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 Encoder (java.util.Base64.Encoder)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)6 IOException (java.io.IOException)5 KeyFactory (java.security.KeyFactory)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 JOSEObjectType (com.nimbusds.jose.JOSEObjectType)4 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)4 JWSHeader (com.nimbusds.jose.JWSHeader)4 JWSSigner (com.nimbusds.jose.JWSSigner)4 MACSigner (com.nimbusds.jose.crypto.MACSigner)4 JWKSource (com.nimbusds.jose.jwk.source.JWKSource)4 DefaultJOSEObjectTypeVerifier (com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier)4 JWSKeySelector (com.nimbusds.jose.proc.JWSKeySelector)4 JWSVerificationKeySelector (com.nimbusds.jose.proc.JWSVerificationKeySelector)4 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)4 SignedJWT (com.nimbusds.jwt.SignedJWT)4 RSAPublicKey (java.security.interfaces.RSAPublicKey)4