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

/**
 * Prints your current certificate signature to the Logcat. Use this method to obtain your certificate signature.
 *
 * @param context The application context.
 *//*from  w  w w .j a v  a 2  s .c  om*/

public static void getCertificateSignature(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);

        // The APK is signed with multiple signatures, probably it was tampered.
        if (packageInfo.signatures.length > 1) {
            return;
        }

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");

            md.update(signature.toByteArray());

            Log.d("TAMPERING_PROTECTION", "**" + Base64.encodeToString(md.digest(), Base64.DEFAULT) + "**");
        }
    } catch (Exception exception) {
        Log.d("TAMPERING_PROTECTION", exception.getStackTrace().toString());
    }
}

From source file:Main.java

/**
 * A double check about app signature that was passed by MainActivity as facetID.
 * @param facetId a string value composed by app hash. I.e. android:apk-key-hash:Lir5oIjf552K/XN4bTul0VS3GfM
 * @param context Application Context/* w  w w .  j a  v  a  2 s.  c  o  m*/
 * @return true if the signature executed on runtime matches if signature sent by MainActivity
 */
private static boolean checkAppSignature(String facetId, Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature sign : packageInfo.signatures) {
            byte[] sB = sign.toByteArray();
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(sign.toByteArray());
            String currentSignature = Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT);
            if (currentSignature.toLowerCase().contains(facetId.split(":")[2].toLowerCase())) {
                return true;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

static String createEncodedParam(String rawParam) {
    return Base64.encodeToString(rawParam.getBytes(), Base64.DEFAULT);
}

From source file:Main.java

/**
 * Gets the key hash of application package's certificate signature.
 * @since   0.1.1//from   www  . j  a  v a2  s. com
 * @param   aContext The context from which the package manager is retrieved to get the signature's information.
 * @return   The list of signatures embedded to the application package.
 */
public static List<String> getKeyHash(Context aContext) {
    try {
        PackageInfo info = aContext.getPackageManager().getPackageInfo(getPackageName(aContext),
                PackageManager.GET_SIGNATURES);

        List<String> keyHashList = new ArrayList<String>(info.signatures.length);

        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            keyHashList.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return keyHashList;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * File uri to base64./*w  ww.jav  a  2  s.  c  om*/
 *
 * @param uri      the uri
 * @param resolver the resolver
 * @return the string
 */
public static String fileUriToBase64(Uri uri, ContentResolver resolver) {
    String encodedBase64 = "";
    try {
        byte[] bytes = readBytes(uri, resolver);
        encodedBase64 = Base64.encodeToString(bytes, 0);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return encodedBase64;
}

From source file:Main.java

public static String formatBasicAuthorization(final String token) {
    return String.format("Basic %s",
            Base64.encodeToString(token.getBytes(Charset.forName("UTF-8")), Base64.NO_WRAP));
}

From source file:Main.java

/**
 * Encode an image form the given path into a {@link Base64}  string
 *
 * @param imagePath Path of the image to encode (must be absolute)
 * @return a {@link Base64} encoded string.
 *///from w  w  w.jav  a  2 s .  c om
public static String encodeBitmap(String imagePath) {
    Bitmap image = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] b = bos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static void printKeyHash(Activity pActivity) {
    // Add code to print out the key hash
    try {//from ww w  .j a va2s .  c  o  m
        PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("KeyHash:", e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.d("KeyHash:", e.toString());
    }
}

From source file:Main.java

/***
 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
 * request urls/*from   ww  w  .j  a  v  a2s  .co m*/
 * 
 * @param data The data to be signed.
 * @param key The signing key.
 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws SignatureException when signature generation fails
 */
public static String getHMac(String data, String key) throws SignatureException {

    if (data == null) {
        throw new NullPointerException("Data to be signed cannot be null");
    }

    String result = null;
    try {

        final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance &
        // initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] digest = mac.doFinal(data.getBytes());

        if (digest != null) {
            // Base 64 Encode the results
            result = Base64.encodeToString(digest, Base64.NO_WRAP);
        }

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }

    return result;
}

From source file:Main.java

/**
 *  //w w  w. j  av a 2 s.c o m
 * @param s
 * @param keyString
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static String sha1(String s, String keyString)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    Mac mac = getMac(keyString, KEY_HASH_SHA1);
    byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
    return new String(Base64.encodeToString(bytes, 0));
}