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 encodedSafe(String data) throws UnsupportedEncodingException {
    //           byte[] b = Base64.encodeBase64(data.getBytes(ENCODING),true);
    byte[] b = Base64.encode(data.getBytes(ENCODING), Base64.URL_SAFE);
    return new String(b, ENCODING);
}

From source file:Main.java

public static String createPubNubSafeBase64Hash(String input) {
    try {/*w  ww. j a  v  a 2  s  .c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(input.getBytes());
        String encodedChannelName = Base64.encodeToString(messageDigest.digest(), Base64.URL_SAFE);

        //pubnub channel names cannot be more than 92 characters
        if (encodedChannelName.length() > 92) {
            encodedChannelName = encodedChannelName.substring(0, 91);
        }
        //pubnub channel names cannot have whitespace characters
        return encodedChannelName.trim();
    } catch (Exception e) {
        Log.d("X", "Error in encoding: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

public static RSAPublicKey buildPublicKeyFromBase64String(String key)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    byte[] byteKey = Base64.decode(key.getBytes(), Base64.NO_WRAP | Base64.URL_SAFE);
    X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
    KeyFactory kf = KeyFactory.getInstance("RSA");

    return (RSAPublicKey) kf.generatePublic(X509publicKey);
}

From source file:Main.java

public static String getStringMD5(String key) {
    MessageDigest md5 = null;/* w w  w.j  av a  2s .co m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md5.update(key.getBytes());
    //important: use Base64.URL_SAFE flag to avoid "+" and "/"
    return new String(Base64.encode(md5.digest(), Base64.URL_SAFE));
}

From source file:Main.java

public static String generateShortUuid() {
    UUID uuid = UUID.randomUUID();
    MessageDigest md = null;/*from w  w w  .java2s.com*/
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return uuid.toString();
    }

    md.update(uuid.toString().getBytes());
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING).substring(0, 20);
}

From source file:Main.java

public static String encodeUrlSafe64(byte[] array) {
    int flags = Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE;
    return Base64.encodeToString(array, flags);
}

From source file:Main.java

public static String makeSHA1HashBase64(byte[] bytes) {
    try {/* w w  w .  j  a  v a  2  s  .  c  o  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static String encodeB64(byte[] bytes) {
    try {/*from   w  ww  . ja  v  a  2s. c om*/
        return Base64.encodeToString(bytes, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static String makeBase64(String message) {
    if (message != null) {
        return Base64.encodeToString(message.getBytes(), Base64.URL_SAFE);
    } else/* w  w  w  .j  av a  2 s . c o m*/
        return null;
}

From source file:Main.java

public static String getQETAG(String path) {
    byte[] SHA1Byte;
    try {//  ww w  .j av  a2 s.  co m
        SHA1Byte = getFileSHA1(path);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    if (null == SHA1Byte) {
        throw new IllegalArgumentException("SHA1 must not be empty!");
    }

    if (SHA1Byte.length != 20) {
        throw new IllegalArgumentException("SHA1 length must be 20! Current length:" + SHA1Byte.length);
    }

    byte[] QETAGByte = new byte[21];
    QETAGByte[0] = 0x16;

    System.arraycopy(SHA1Byte, 0, QETAGByte, 1, 20);

    return Base64.encodeToString(QETAGByte, Base64.URL_SAFE | Base64.NO_WRAP);
}