jdk/src/share/classes/com/sun/net/httpserver/BasicAuthenticator.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 6906 d57020b6118f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * BasicAuthenticator provides an implementation of HTTP Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * authentication. It is an abstract class and must be extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to provide an implementation of {@link #checkCredentials(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * which is called to verify each incoming request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public abstract class BasicAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Creates a BasicAuthenticator for the given HTTP realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @param realm The HTTP Basic authentication realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @throws NullPointerException if the realm is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public BasicAuthenticator (String realm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.realm = realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * returns the realm this BasicAuthenticator was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @return the authenticator's realm string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public String getRealm () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public Result authenticate (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        HttpContext context = t.getHttpContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Headers rmap = (Headers) t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * look for auth token
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String auth = rmap.getFirst ("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (auth == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Headers map = (Headers) t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return new Authenticator.Retry (401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int sp = auth.indexOf (' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return new Authenticator.Failure (401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        byte[] b = Base64.base64ToByteArray (auth.substring(sp+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String userpass = new String (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int colon = userpass.indexOf (':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String uname = userpass.substring (0, colon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        String pass = userpass.substring (colon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (checkCredentials (uname, pass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return new Authenticator.Success (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                new HttpPrincipal (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    uname, realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            /* reject the request again with 401 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Headers map = (Headers) t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return new Authenticator.Failure(401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * called for each incoming request to verify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * given name and password in the context of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Authenticator's realm. Any caching of credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * must be done by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param username the username from the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param password the password from the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return <code>true</code> if the credentials are valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *    <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public abstract boolean checkCredentials (String username, String password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
class Base64 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Translates the specified byte array into a Base64 string as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Preferences.put(byte[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static String byteArrayToBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return byteArrayToBase64(a, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Translates the specified byte array into an "aternate representation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Base64 string.  This non-standard variant uses an alphabet that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * not contain the uppercase alphabetic characters, which makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * suitable for use in situations where case-folding occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static String byteArrayToAltBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return byteArrayToBase64(a, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static String byteArrayToBase64(byte[] a, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int aLen = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int numFullGroups = aLen/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int numBytesInPartialGroup = aLen - 3*numFullGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int resultLen = 4*((aLen + 2)/3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        StringBuffer result = new StringBuffer(resultLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // Translate all full groups from byte array elements to Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int inCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int byte2 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            result.append(intToAlpha[byte2 & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Translate partial group if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (numBytesInPartialGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (numBytesInPartialGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                result.append(intToAlpha[(byte0 << 4) & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                result.append("==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // assert numBytesInPartialGroup == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                result.append(intToAlpha[(byte1 << 2)&0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // assert inCursor == a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // assert result.length() == resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * index values into their "Base64 Alphabet" equivalents as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static final char intToBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * index values into their "Alternate Base64 Alphabet" equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * This alternate alphabet does not use the capital letters.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * designed for use in environments where "case folding" occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static final char intToAltBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ';', '<', '>', '@', '[', ']', '^',  '`', '_', '{', '|', '}', '~',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        'n', 'o', 'p', 'q', 'r', 's', 't',  'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        '0', '1', '2', '3', '4', '5', '6',  '7', '8', '9', '+', '?'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Translates the specified Base64 string (as per Preferences.get(byte[]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *        string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static byte[] base64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return base64ToByteArray(s, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Translates the specified "aternate representation" Base64 string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throw IllegalArgumentException or ArrayOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *        if <tt>s</tt> is not a valid alternate representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *        Base64 string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static byte[] altBase64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return base64ToByteArray(s, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static byte[] base64ToByteArray(String s, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        byte[] alphaToInt = (alternate ?  altBase64ToInt : base64ToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int sLen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int numGroups = sLen/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (4*numGroups != sLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                "String length must be a multiple of four.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int missingBytesInLastGroup = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int numFullGroups = numGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (sLen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (s.charAt(sLen-1) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                numFullGroups--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (s.charAt(sLen-2) == '=')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Translate all full groups from base64 to byte array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int inCursor = 0, outCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            result[outCursor++] = (byte) ((ch2 << 6) | ch3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Translate partial group, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (missingBytesInLastGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (missingBytesInLastGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // assert inCursor == s.length()-missingBytesInLastGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // assert outCursor == result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Translates the specified character, which is assumed to be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throw IllegalArgumentException or ArrayOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *        c is not in the Base64 Alphabet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static int base64toInt(char c, byte[] alphaToInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int result = alphaToInt[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IllegalArgumentException("Illegal character " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * This array is a lookup table that translates unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * into their 6-bit positive integer equivalents.  Characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * are not in the Base64 alphabet but fall within the bounds of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * array are translated to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static final byte base64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * This array is the analogue of base64ToInt, but for the nonstandard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * variant that avoids the use of uppercase alphabetic characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static final byte altBase64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        51, 22, 23, 24, 25
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}