Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

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

Prototype

public static String encodeToString(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:Main.java

public static String toBase64(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();

    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}

From source file:Main.java

public static String getQETAG(String path) {
    byte[] SHA1Byte;
    try {/*  w  w  w  .j av a 2  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);
}

From source file:Main.java

public static String imgsToBase64(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

/**
 * Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy
 * authorization. The format of Basic auth is "username:password" as a base64 string.
 *
 * @param username username to encode//ww w . j a  v  a2 s.  c o m
 * @param password password to encode
 * @return a base-64 encoded string containing <code>username:password</code>
 */
public static String base64EncodeBasicCredentials(String username, String password) {
    String credentialsToEncode = username + ':' + password;
    // using UTF-8, which is the modern de facto standard, and which retains compatibility with US_ASCII for ASCII characters,
    // as required by RFC 7616, section 3: http://tools.ietf.org/html/rfc7617#section-3
    byte[] credentialsAsUtf8Bytes = credentialsToEncode.getBytes(Charset.forName("UTF-8"));
    return Base64.encodeToString(credentialsAsUtf8Bytes, Base64.DEFAULT);
}

From source file:Main.java

public static String generateBasicAuthToken(String name, String secret) {
    StringBuilder sb = new StringBuilder();
    sb.append(name).append(":").append(secret);
    try {/*from   w  w w . j  av a  2s . c  om*/
        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 void savePrivateKey(RSAPrivateKeySpec privateKey, Context context) {
    BigInteger privateModBI = privateKey.getModulus();
    BigInteger privateExpBI = privateKey.getPrivateExponent();

    byte[] privateModBA = privateModBI.toByteArray();// Base64.encodeInteger(pubModBI);
    // // for some
    // strange reason
    // this throws
    // NoSuchMethodError
    byte[] privateExpBA = privateExpBI.toByteArray();// Base64.encodeInteger(pubExpBI);

    try {/*from   w  ww  . j av a  2s.c  o m*/
        String privateModBase64Str = Base64.encodeToString(privateModBA, Base64.NO_WRAP);
        String privateExpBase64Str = Base64.encodeToString(privateExpBA, Base64.NO_WRAP);
        savePrivateKey(privateModBase64Str, privateExpBase64Str, context);

    } catch (NoSuchMethodError e) {
        Log.e(TAG, "Base64.encode() method not available", e);
    }
}

From source file:Main.java

/**
 * @param context/*  www  .  j  a v a  2  s.co  m*/
 * @return KeyHash
 * follow facebook developers link to get release key hash
 * https://developers.facebook.com/docs/android/getting-started#release-key-hash
 */
public static String getKeyHash(Context context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(
                context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e1) {
    } catch (NoSuchAlgorithmException e) {
    } catch (Exception e) {
    }
    return key;
}

From source file:Main.java

public static String generateKey(Object... objects) {

    Object[] args = objects;//w  w w .  j  av  a  2  s  .  com

    if (args == null) {
        throw new NullPointerException("Cannot generate key with no params!");
    }

    StringBuilder stringBuilder = new StringBuilder();

    for (Object o : args) {
        if (o != null) {
            if (o instanceof String) {
                stringBuilder.append(o);
                stringBuilder.append("_");
            } else {
                String json = gson.toJson(o);
                stringBuilder.append(json);
                stringBuilder.append("_");
            }
        }
    }

    return Base64.encodeToString(stringBuilder.toString().getBytes(), Base64.DEFAULT);
}

From source file:Main.java

public static String smallFileSha1(File file) {
    InputStream inputStream = null;
    try {/*from   w  ww .j  av a2s  . c  o  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

private static String getBytesAsString(byte[] bytes) {
    if (bytes == null)
        return "null";
    try {/*w  ww.  j av  a2 s . co m*/
        CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
        CharBuffer r = d.decode(ByteBuffer.wrap(bytes));
        return r.toString();
    } catch (Exception e) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }
}