Search in sources :

Example 6 with Decoder

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

the class Base64Test method testRoundTrip_allBytes_mime_lineLength_isRoundedDown.

/**
     * checks that the specified line length is rounded down to the nearest multiple of 4.
     */
public void testRoundTrip_allBytes_mime_lineLength_isRoundedDown() {
    Decoder decoder = Base64.getMimeDecoder();
    byte[] separator = new byte[] { '\r', '\n' };
    checkRoundTrip_allBytes(Base64.getMimeEncoder(60, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(63, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(10, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 8));
}
Also used : Decoder(java.util.Base64.Decoder)

Example 7 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 8 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 9 with Decoder

use of java.util.Base64.Decoder in project webpieces by deanhiller.

the class SettingsMarshaller method unmarshalPayload.

public List<Http2Setting> unmarshalPayload(String base64SettingsPayload) {
    Decoder decoder = Base64.getDecoder();
    byte[] decoded = decoder.decode(base64SettingsPayload);
    ByteBuffer buf = ByteBuffer.wrap(decoded);
    return unmarshal(buf);
}
Also used : Decoder(java.util.Base64.Decoder) ByteBuffer(java.nio.ByteBuffer)

Example 10 with Decoder

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

the class Base64Test method testEncoder_encodeArrayToArray.

/**
     * Tests {@link Decoder#decode(byte[], byte[])} for correctness.
     */
public void testEncoder_encodeArrayToArray() {
    Encoder encoder = Base64.getEncoder();
    // Empty input
    assertEquals(0, encoder.encode(new byte[0], new byte[0]));
    // Test data for non-empty input
    byte[] input = "abcefghi".getBytes(US_ASCII);
    String expectedString = "YWJjZWZnaGk=";
    byte[] encodedBytes = expectedString.getBytes(US_ASCII);
    // Non-empty input: output array too short
    byte[] tooShort = new byte[encodedBytes.length - 1];
    try {
        encoder.encode(input, tooShort);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    // Non-empty input: output array longer than required
    byte[] tooLong = new byte[encodedBytes.length + 1];
    int tooLongBytesEncoded = encoder.encode(input, tooLong);
    assertEquals(encodedBytes.length, tooLongBytesEncoded);
    assertEquals(0, tooLong[tooLong.length - 1]);
    assertArrayPrefixEquals(tooLong, encodedBytes.length, encodedBytes);
    // Non-empty input: output array has exact minimum required size
    byte[] justRight = new byte[encodedBytes.length];
    int justRightBytesEncoded = encoder.encode(input, justRight);
    assertEquals(encodedBytes.length, justRightBytesEncoded);
    assertArrayEquals(encodedBytes, justRight);
}
Also used : Encoder(java.util.Base64.Encoder)

Aggregations

Decoder (java.util.Base64.Decoder)16 Encoder (java.util.Base64.Encoder)6 ByteBuffer (java.nio.ByteBuffer)4 Base64 (java.util.Base64)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Unauthorized401Exception (com.nike.riposte.server.error.exception.Unauthorized401Exception)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BASE64Decoder (sun.misc.BASE64Decoder)1 BASE64Encoder (sun.misc.BASE64Encoder)1