Example usage for android.util Base64 URL_SAFE

List of usage examples for android.util Base64 URL_SAFE

Introduction

In this page you can find the example usage for android.util Base64 URL_SAFE.

Prototype

int URL_SAFE

To view the source code for android.util Base64 URL_SAFE.

Click Source Link

Document

Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and / .

Usage

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private U2FContext parseU2FContextRegister(JSONObject json) {
    try {//  w  w  w.  j  a  va 2 s.co m
        byte[] challenge = null;
        String appId = json.getString(TAG_JSON_APPID);
        int requestId = json.getInt(TAG_JSON_REQUESTID);
        JSONArray array = json.getJSONArray(TAG_JSON_REGISTER_REQUESTS);
        for (int i = 0; i < array.length(); i++) {
            // TODO : only handle USB transport if several are present
            JSONObject registerItem = array.getJSONObject(i);
            if (!registerItem.getString(TAG_JSON_VERSION).equals(VERSION_U2F_V2)) {
                Log.e(TAG, "Invalid register version");
                return null;
            }
            challenge = Base64.decode(registerItem.getString(TAG_JSON_CHALLENGE), Base64.URL_SAFE);
        }
        return new U2FContext(appId, challenge, null, requestId, false);
    } catch (JSONException e) {
        Log.e(TAG, "Error decoding request");
        return null;
    }
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createClientData(U2FContext context) {
    try {//from w w  w  .  j  a  v  a  2s .com
        JSONObject clientData = new JSONObject();
        clientData.put(TAG_JSON_TYP, (context.isSign() ? SIGN_RESPONSE_TYP : REGISTER_RESPONSE_TYP));
        clientData.put(TAG_JSON_CHALLENGE, Base64.encodeToString(context.getChallenge(),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        clientData.put(TAG_JSON_ORIGIN, context.getAppId());
        clientData.put(TAG_JSON_CID_PUBKEY, CID_UNAVAILABLE);
        return clientData.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding client data");
        return null;
    }
}

From source file:com.mobile.natal.natalchart.NetworkUtilities.java

private static String getB64Auth(String login, String pass) {
    String source = login + ":" + pass;
    String ret = "Basic " + Base64.encodeToString(source.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP);
    return ret;/*from w  w w.j a v a2 s  .c  o  m*/
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createU2FResponseSign(U2FContext context, byte[] signature) {
    try {//  w  w  w.  ja va  2s . c om
        JSONObject response = new JSONObject();
        response.put(TAG_JSON_TYPE, SIGN_RESPONSE_TYPE);
        response.put(TAG_JSON_REQUESTID, context.getRequestId());
        JSONObject responseData = new JSONObject();
        responseData.put(TAG_JSON_KEYHANDLE, Base64.encodeToString(context.getChosenKeyHandle(),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        responseData.put(TAG_JSON_SIGNATUREDATA, Base64.encodeToString(signature, 0, signature.length - 2,
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        String clientData = createClientData(context);
        responseData.put(TAG_JSON_CLIENTDATA, Base64.encodeToString(clientData.getBytes("UTF-8"),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        response.put(TAG_JSON_RESPONSEDATA, responseData);
        return response.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding request");
        return null;
    }
}

From source file:org.quantumbadger.redreader.reddit.api.RedditOAuth.java

public static FetchAccessTokenResult fetchAccessTokenSynchronous(final Context context,
        final RefreshToken refreshToken) {

    final String uri = ACCESS_TOKEN_URL;
    StatusLine responseStatus = null;/*ww  w .  j  a  v  a2  s.c om*/

    try {
        final HttpClient httpClient = CacheManager.createHttpClient(context);

        final HttpPost request = new HttpPost(uri);

        final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
        nameValuePairs.add(new BasicNameValuePair("refresh_token", refreshToken.token));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        request.addHeader("Authorization", "Basic "
                + Base64.encodeToString((CLIENT_ID + ":").getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));

        final HttpResponse response = httpClient.execute(request);
        responseStatus = response.getStatusLine();

        if (responseStatus.getStatusCode() != 200) {
            return new FetchAccessTokenResult(FetchAccessTokenResultStatus.UNKNOWN_ERROR,
                    new RRError(context.getString(R.string.error_unknown_title),
                            context.getString(R.string.message_cannotlogin), null, responseStatus,
                            request.getURI().toString()));
        }

        final JsonValue jsonValue = new JsonValue(response.getEntity().getContent());
        jsonValue.buildInThisThread();
        final JsonBufferedObject responseObject = jsonValue.asObject();

        final String accessTokenString = responseObject.getString("access_token");

        if (accessTokenString == null) {
            throw new RuntimeException("Null access token: " + responseObject.getString("error"));
        }

        final AccessToken accessToken = new AccessToken(accessTokenString);

        return new FetchAccessTokenResult(accessToken);

    } catch (IOException e) {
        return new FetchAccessTokenResult(FetchAccessTokenResultStatus.CONNECTION_ERROR,
                new RRError(context.getString(R.string.error_connection_title),
                        context.getString(R.string.error_connection_message), e, responseStatus, uri));

    } catch (Throwable t) {
        return new FetchAccessTokenResult(FetchAccessTokenResultStatus.UNKNOWN_ERROR,
                new RRError(context.getString(R.string.error_unknown_title),
                        context.getString(R.string.error_unknown_message), t, responseStatus, uri));
    }
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createU2FResponseRegister(U2FContext context, byte[] registerResponse) {
    try {/*from  w w  w  .j a  v a2s  .c  o  m*/
        JSONObject response = new JSONObject();
        response.put(TAG_JSON_TYPE, REGISTER_RESPONSE_TYPE);
        response.put(TAG_JSON_REQUESTID, context.getRequestId());
        JSONObject responseData = new JSONObject();
        responseData.put(TAG_JSON_REGISTRATIONDATA, Base64.encodeToString(registerResponse, 0,
                registerResponse.length - 2, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        responseData.put(TAG_JSON_VERSION, VERSION_U2F_V2);
        String clientData = createClientData(context);
        responseData.put(TAG_JSON_CLIENTDATA, Base64.encodeToString(clientData.getBytes("UTF-8"),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        response.put(TAG_JSON_RESPONSEDATA, responseData);
        return response.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding request");
        return null;
    }
}

From source file:com.google.ipc.invalidation.ticl.android.AndroidChannel.java

/** Returns the web encoded version of the channel network endpoint ID for HTTP requests. */
@Override//from www  .  j a v  a 2s  . co  m
protected String getWebEncodedEndpointId() {
    NetworkEndpointId networkEndpointId = getNetworkId();
    return Base64.encodeToString(networkEndpointId.toByteArray(),
            Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);
}

From source file:org.quantumbadger.redreader.reddit.api.RedditOAuth.java

public static FetchAccessTokenResult fetchAnonymousAccessTokenSynchronous(final Context context) {

    final String uri = ACCESS_TOKEN_URL;
    StatusLine responseStatus = null;/*from w  w  w.j a va  2s .  co  m*/

    try {
        final HttpClient httpClient = CacheManager.createHttpClient(context);

        final HttpPost request = new HttpPost(uri);

        final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs
                .add(new BasicNameValuePair("grant_type", "https://oauth.reddit.com/grants/installed_client"));
        nameValuePairs.add(new BasicNameValuePair("device_id", "DO_NOT_TRACK_THIS_DEVICE"));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        request.addHeader("Authorization", "Basic "
                + Base64.encodeToString((CLIENT_ID + ":").getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));

        final HttpResponse response = httpClient.execute(request);
        responseStatus = response.getStatusLine();

        if (responseStatus.getStatusCode() != 200) {
            return new FetchAccessTokenResult(FetchAccessTokenResultStatus.UNKNOWN_ERROR,
                    new RRError(context.getString(R.string.error_unknown_title),
                            context.getString(R.string.message_cannotlogin), null, responseStatus,
                            request.getURI().toString()));
        }

        final JsonValue jsonValue = new JsonValue(response.getEntity().getContent());
        jsonValue.buildInThisThread();
        final JsonBufferedObject responseObject = jsonValue.asObject();

        final String accessTokenString = responseObject.getString("access_token");

        if (accessTokenString == null) {
            throw new RuntimeException("Null access token: " + responseObject.getString("error"));
        }

        final AccessToken accessToken = new AccessToken(accessTokenString);

        return new FetchAccessTokenResult(accessToken);

    } catch (IOException e) {
        return new FetchAccessTokenResult(FetchAccessTokenResultStatus.CONNECTION_ERROR,
                new RRError(context.getString(R.string.error_connection_title),
                        context.getString(R.string.error_connection_message), e, responseStatus, uri));

    } catch (Throwable t) {
        return new FetchAccessTokenResult(FetchAccessTokenResultStatus.UNKNOWN_ERROR,
                new RRError(context.getString(R.string.error_unknown_title),
                        context.getString(R.string.message_cannotlogin), t, responseStatus, uri));
    }
}

From source file:com.microsoft.aad.adal.Oauth2.java

public static String decodeProtocolState(String encodedState) {

    if (!StringExtensions.IsNullOrBlank(encodedState)) {
        byte[] stateBytes = Base64.decode(encodedState, Base64.NO_PADDING | Base64.URL_SAFE);

        return new String(stateBytes);
    }/*from  w w  w .  ja  v  a  2 s. co m*/

    return null;
}

From source file:com.swisscom.safeconnect.backend.BackendConnector.java

public static String getBase64EncodedString(String string) {
    try {//from  ww w.  jav a  2  s  .c o  m
        String b64EncodedPhoneNumber = Base64.encodeToString(string.getBytes("UTF-8"), Base64.URL_SAFE);
        return URLEncoder.encode(b64EncodedPhoneNumber, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        if (BuildConfig.DEBUG)
            Log.e(Config.TAG, "Exception when encoding url string", e);
        return null;
    }
}