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:Main.java

public static String generateBasicAuthToken(String name, String secret) {
    StringBuilder sb = new StringBuilder();
    sb.append(name).append(":").append(secret);
    try {/* w ww  .j  av  a  2 s .c  o m*/
        return AUTH_PREFIX_BASIC
                + Base64.encodeToString(sb.toString().getBytes("UTF-8"), Base64.URL_SAFE | Base64.NO_WRAP);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String smallFileSha1(File file) {
    InputStream inputStream = null;
    try {/*from   w  w w  . ja v a 2  s  . co m*/
        MessageDigest md = MessageDigest.getInstance("SHA1");
        inputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int nRead = 0;
        while ((nRead = inputStream.read(buffer)) != -1) {
            md.update(buffer, 0, nRead);
        }
        byte[] digest = md.digest();
        byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1).array();
        byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, 4)
                .put(digest, 0, digest.length).array();
        return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

static byte[] decodeB64(String s64) {
    return Base64.decode(s64, Base64.URL_SAFE);
}

From source file:Main.java

/**
 * Method deciphers previously ciphered message
 *
 * @param message ciphered message/*  w w w .  j  ava 2s  . co  m*/
 * @param salt    salt which was used for ciphering
 * @return deciphered message
 */
@NonNull
public static String fromX(@NonNull String message, @NonNull String salt) throws IllegalArgumentException {
    return x(new String(Base64.decode(message, Base64.URL_SAFE)), salt);
}

From source file:Main.java

public static String completeJweFromSIM(String jweSIM) {
    // android.os.Debug.waitForDebugger();

    try {//from   w w  w.ja  v a  2 s .c  om
        if (jweSIM != null && jweSIM.length() > 0) {
            String parts[] = jweSIM.split("\\.");
            ;
            if (parts != null && parts.length == 5) {
                // retrieve hmac key
                byte hmac_key[] = Base64.decode(parts[4], Base64.URL_SAFE);
                if (hmac_key != null && hmac_key.length == 16) {
                    // init hash instance
                    Mac hmac = Mac.getInstance("HmacSHA256", "SC");
                    hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));

                    byte[] aad = parts[0].getBytes();
                    long al = aad.length * 8;
                    byte[] iv_key = decodeB64(parts[2]);
                    byte[] cryptedBytes = decodeB64(parts[3]);

                    // build data to hash
                    byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8];
                    int offset = 0;
                    System.arraycopy(aad, offset, hmacData, 0, aad.length);
                    offset += aad.length;
                    System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length);
                    offset += iv_key.length;
                    System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length);
                    offset += cryptedBytes.length;
                    ByteBuffer buffer = ByteBuffer.allocate(8);
                    buffer.putLong(al);
                    System.arraycopy(buffer.array(), 0, hmacData, offset, 8);

                    // compute hac value
                    byte[] hmacValue = hmac.doFinal(hmacData);
                    // authentication tag
                    byte[] auth_tag = Arrays.copyOf(hmacValue, 16);
                    String auth_tag64 = encodeB64(auth_tag);

                    // A.2.7. Complete Representation
                    String finalString = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3] + "."
                            + auth_tag64;

                    //                  // just for verification
                    //                  byte jwt64 [] = decryptJWE(finalString, RsaKeyTim.privRsaKey);
                    //                  if(jwt64!=null) {
                    //                     String jws = new String(jwt64);
                    //                     Log.d("completeJweFromSIM", "jws verify Key TIM :"+verifyJWS(jws,RsaKeyTim.pubRsaKey));
                    //                  }

                    return finalString;
                }
            }
            // 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.orange.oidc.secproxy_service.Token.java

static String getJSON(String token) {
    String ds = null;/*from   w w w  . j a  va 2 s  . co m*/

    String tokenB64 = null;
    if (token != null) {
        String[] p = token.split("\\.");
        if (p != null && p.length == 3) {
            tokenB64 = p[1];
        }
    }

    if (tokenB64 != null) {
        byte[] decoded = null;
        try {
            decoded = Base64.decode(tokenB64, Base64.URL_SAFE);
            ds = new String(decoded, "UTF-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (decoded == null) {
            try {
                decoded = Base64.decode(tokenB64, Base64.DEFAULT);
                ds = new String(decoded, "UTF-8");
            } catch (Exception ee) {
                // TODO Auto-generated catch block
                ee.printStackTrace();
            }
        }
    }
    return ds;
}

From source file:squar.com.firebasetest.token.JWTEncoder.java

private static String sign(String secret, String secureBits) {
    try {/*from ww w .ja  va2s . co  m*/
        Mac sha256_HMAC = Mac.getInstance(HMAC_256);
        SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(UTF8_CHARSET), HMAC_256);
        sha256_HMAC.init(secret_key);
        byte sig[] = sha256_HMAC.doFinal(secureBits.getBytes(UTF8_CHARSET));
        //      return Base64.encodeBase64URLSafeString(sig);
        return Base64.encodeToString(sig, Base64.URL_SAFE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.samples.apps.abelana.AbelanaThings.java

public static AbelanaUser start(Context ctx, String atok, String secret) {
    AbelanaUser user = new AbelanaUser();

    String[] part = atok.split("\\.");
    byte[] jb = Base64.decode(part[1], Base64.URL_SAFE);
    String json = new String(jb);
    byte[] half = Base64.decode(secret, Base64.URL_SAFE);
    String halfpw = new String(half);

    try {/*from w ww.  j  a v a2 s.  c o m*/
        JSONObject pojo = new JSONObject(json);
        user.UserID = pojo.getString("UserID");
        user.Exp = pojo.getLong("Exp");
        user.Iat = pojo.getLong("Iat");
    } catch (JSONException e) {
        System.out.println("Abelana User - convert json " + e.toString());
        return null;
    }
    if (storage == null) {
        AbelanaThings at = new AbelanaThings(ctx, halfpw);
    }
    return user;
}

From source file:com.tapchatapp.android.service.GCMReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String messageType = mGCM.getMessageType(intent);
    if (!messageType.equals(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE)) {
        return;//from w ww. j a va  2 s  .  c o  m
    }

    try {
        byte[] cipherText = Base64.decode(intent.getStringExtra("payload"), Base64.URL_SAFE | Base64.NO_WRAP);
        byte[] iv = Base64.decode(intent.getStringExtra("iv"), Base64.URL_SAFE | Base64.NO_WRAP);

        byte[] key = mPusherClient.getPushKey();
        if (key == null) {
            // I don't think this will ever happen
            throw new Exception("Received push notification before receiving decryption key.");
        }

        JSONObject message = new JSONObject(new String(decrypt(cipherText, key, iv), "UTF-8"));

        Intent broadcastIntent = new Intent(TapchatApp.ACTION_MESSAGE_NOTIFY);
        addExtras(broadcastIntent, message);
        context.sendOrderedBroadcast(broadcastIntent, null);
    } catch (Exception ex) {
        Log.e(TAG, "Error parsing push notification", ex);
    }
}

From source file:squar.com.firebasetest.token.JWTEncoder.java

private static String encodeJson(JSONObject jsonData) {
    //    return Base64.encodeBase64URLSafeString(jsonData.toString().getBytes(UTF8_CHARSET));
    //    return new String(Base64.encodeBase64(jsonData.toString().getBytes(UTF8_CHARSET)));
    return Base64.encodeToString(jsonData.toString().getBytes(UTF8_CHARSET), Base64.URL_SAFE);
}