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 getHashKey(Context context) {
    // Add code to print out the key hash
    try {//  w  ww.  java  2  s.c o  m
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            return Base64.encodeToString(md.digest(), Base64.DEFAULT);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
    return null;
}

From source file:Main.java

public static final String getHttpBasicAuthHeader(String username, String password) {
    String digest = username + ":" + password;
    return "Basic " + Base64.encodeToString(digest.getBytes(), Base64.NO_WRAP);
}

From source file:Main.java

public static String encodeBase64(String s) {
    return Base64.encodeToString(s.getBytes(), Base64.DEFAULT);
}

From source file:Main.java

public static String keyHash(Context context) {
    String key = "";
    try {//from w w  w. j  a  v a2s  .c  om
        PackageInfo info = context.getPackageManager().getPackageInfo("org.tathva.triloaded.anubhava",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("anas", key);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    return key;
}

From source file:Main.java

public static String makeBase64(String message) {
    if (message != null) {
        return Base64.encodeToString(message.getBytes(), Base64.URL_SAFE);
    } else/*  www .j  av a2 s. c om*/
        return null;
}

From source file:Main.java

public static String _byteArrayToB64(byte[] a) {
    return Base64.encodeToString(a, Base64.NO_WRAP);
}

From source file:Main.java

public static void writeByteArrayAttribute(XmlSerializer out, String name, byte[] value) throws IOException {
    if (value != null) {
        out.attribute(null, name, Base64.encodeToString(value, Base64.DEFAULT));
    }//from   w ww. j  av a 2s.c o m
}

From source file:Main.java

/**
 * Print hash key/*  w ww . j  av a  2s .  c om*/
 */
public static void printHashKey(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(TAG, PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d(TAG, "keyHash: " + keyHash);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

public static String toString(X509Certificate certificate) throws CertificateEncodingException {
    String header = "-----BEGIN CERTIFICATE-----\n";
    String cert = Base64.encodeToString(certificate.getEncoded(), Base64.DEFAULT);
    String footer = "-----END CERTIFICATE-----";
    return header + cert + footer;
}

From source file:Main.java

/**
 * A convenience method that generates a XOR key pair for a given key. It was used to generate
 * the key for {@link MainActivity#useXorStringHiding(String)} method.
 *
 * @param key The source key to use in generating the XOR key halves
 * @return a two-value string array containing both parts of the XOR key
 *///www  .ja  v a 2  s.  com
public static String[] generateKeyXorParts(String key) {
    String[] keyParts = new String[2];

    byte[] xorRandom = new byte[key.length()];
    byte[] xorMatch = new byte[key.length()];
    byte[] keyBytes = key.getBytes();
    for (int i = 0; i < key.length(); i++) {
        xorRandom[i] = (byte) (256 * Math.random());
        xorMatch[i] = (byte) (xorRandom[i] ^ keyBytes[i]);
    }
    keyParts[0] = Base64.encodeToString(xorRandom, 0);
    keyParts[1] = Base64.encodeToString(xorMatch, 0);
    Log.i(TAG, "XOR Key Part 0: " + keyParts[0]);
    Log.i(TAG, "XOR Key Part 1: " + keyParts[1]);

    return keyParts;
}