Example usage for android.util Base64 DEFAULT

List of usage examples for android.util Base64 DEFAULT

Introduction

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

Prototype

int DEFAULT

To view the source code for android.util Base64 DEFAULT.

Click Source Link

Document

Default values for encoder/decoder flags.

Usage

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

public static String uploadFile(String path) {
    String sourceFileUri = path;// w w w. ja  v  a2  s  .c o m
    File file = new File(sourceFileUri);
    ByteArrayOutputStream objByteArrayOS = null;
    FileInputStream objFileIS = null;
    boolean isSuccess = false;
    String strAttachmentCoded = null;

    if (!file.isFile()) {
        Log.w(TAG, "Source File not exist :" + sourceFileUri);
    } else {
        try {
            objFileIS = new FileInputStream(file);
            objByteArrayOS = new ByteArrayOutputStream();
            byte[] byteBufferString = new byte[(int) file.length()];
            objFileIS.read(byteBufferString);

            byte[] byteBinaryData = Base64.encode(byteBufferString, Base64.DEFAULT);
            strAttachmentCoded = new String(byteBinaryData);

            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Upload file to server", "error: " + e.getMessage(), e);
        } finally {
            try {
                objByteArrayOS.close();
                objFileIS.close();
            } catch (IOException e) {
                Log.e(TAG, "Error : " + e.getMessage());
            }
        }
    }

    if (isSuccess) {
        return strAttachmentCoded;
    } else {
        return "No Picture";
    }
}

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//  w  w w  . j av a2  s .co  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

/**
 * Base64 to File/*  ww  w .j a va 2s.co  m*/
 * @param base64Str
 * @param filePath
 * @param fileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveBase64StringToFile(String base64Str, String filePath, String fileName)
        throws FileNotFoundException, IOException {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
        File dir = new File(filePath);
        if (!dir.exists() && dir.isDirectory()) {
            dir.mkdirs();
        }
        file = new File(filePath, fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        byte[] bfile = Base64.decode(base64Str, Base64.DEFAULT);
        bos.write(bfile);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * @param context/*  w w w . j av  a2  s. c om*/
 * @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;/*from w ww  .j  a v  a 2s .  c  o  m*/

    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

/**
 * 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 a2s .co  m*/

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

/**
 * Gets the key hash of application package's certificate signature.
 * @since   0.1.1//  w w w .  ja  v  a 2  s  .  c o  m
 * @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

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

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  va 2 s. c  om
 * @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;
}