Java Code Examples for android.util.Base64#encodeToString()

The following examples show how to use android.util.Base64#encodeToString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MuPDFReflowView.java    From mupdf-android with GNU Affero General Public License v3.0 6 votes vote down vote up
public void setPage(int page, PointF size) {
	mPage = page;
	if (mLoadHTML != null) {
		mLoadHTML.cancel(true);
	}
	mLoadHTML = new AsyncTask<Void,Void,byte[]>() {
		@Override
		protected byte[] doInBackground(Void... params) {
			return mCore.html(mPage);
		}
		@Override
		protected void onPostExecute(byte[] result) {
			String b64 = Base64.encodeToString(result, Base64.DEFAULT);
			loadData(b64, "text/html; charset=utf-8", "base64");
		}
	};
	mLoadHTML.execute();
}
 
Example 2
Source File: TestActivity.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private String encode(String inputString) {
    try {
        if (isKeyCreated() && initCipher(Cipher.ENCRYPT_MODE)) {
            byte[] bytes = cipher.doFinal(inputString.getBytes());
            return Base64.encodeToString(bytes, Base64.NO_WRAP);
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    return null;
}
 
Example 3
Source File: Utils.java    From xGetter with Apache License 2.0 5 votes vote down vote up
public static String base64Encode(String text) {
    byte[] data = new byte[0];
    try {
        data = text.getBytes("UTF-8");
        return Base64.encodeToString(data, Base64.DEFAULT);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 4
Source File: DetailFragment.java    From kolabnotes-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    if (requestCode == Utils.READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            loadImageFromIntent(resultData);
        }
    } else if (requestCode == DRAWEDITOR_ACTIVITY_RESULT_CODE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            if (resultData.hasExtra(DrawEditorActivity.TAG_RETURN_BITMAP) &&
                    resultData.getByteArrayExtra(DrawEditorActivity.TAG_RETURN_BITMAP) != null) {
                byte[] image = resultData.getByteArrayExtra(DrawEditorActivity.TAG_RETURN_BITMAP);
                String prefix = "data:image/png;base64,";
                String imageEncoded = prefix + Base64.encodeToString(image, Base64.NO_WRAP);

                String alt = UUID.randomUUID().toString();

                /* Set focus, as after rotate focus is lost and it's impossible to insert an image */
                editor.focusEditor();
                editor.insertImage(imageEncoded, alt);
                editor.getScaleX();
                putImage(alt, imageEncoded);
                
                if (activity instanceof OnFragmentCallback) {
                    ((OnFragmentCallback) activity).fileSelected();
                }
            }
        }
    }else if(requestCode == ATTACHMENT_ACTIVITY_RESULT_CODE){
        if (activity instanceof OnFragmentCallback) {
            ((OnFragmentCallback) activity).fileSelected();
        }
    }
}
 
Example 5
Source File: AESUtils.java    From SmallGdufe-Android with GNU General Public License v3.0 5 votes vote down vote up
public static String encryptLocal(String cleartext)  {
    if(TextUtils.isEmpty(cleartext)){
        return "";
    }
    try {
        String encrypted = encrypt(AppConfig.localAesSeed,cleartext);
        return Base64.encodeToString(encrypted.getBytes(), Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: Converter.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressLint("NewApi")
private String base64(byte[] bytes) {
    String encoded;
    try {
        encoded = Base64.encodeToString(bytes, Base64.NO_WRAP);
    } catch (Throwable t) {
        encoded = null;
        // not android, so will try with pure java
    }
    if (encoded == null) {
        encoded = java.util.Base64.getEncoder().encodeToString(bytes);
    }
    return encoded;
}
 
Example 7
Source File: WebSocketConnectionRFC6455.java    From WebSocket-for-Android with Apache License 2.0 5 votes vote down vote up
public static String hashKey(String key)
{
    try
    {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(key.getBytes(_utf8));
        md.update(MAGIC);
        return Base64.encodeToString(md.digest(), Base64.NO_WRAP);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: Utility.java    From indigenous-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generate sha256
 *
 * @param string
 *   The string to hash.
 *
 * @return base64 encoded string
 */
public static String sha256(String string) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(string.getBytes());
        byte[] byteData = md.digest();
        String encoded = Base64.encodeToString(byteData, Base64.NO_WRAP);
        return encoded.trim().replace("=", "").replace("+", "-").replace("/", "_");
    }
    catch (Exception e) {
        return string;
    }
}
 
Example 9
Source File: RichEditorUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
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);
}
 
Example 10
Source File: PictureUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 将bitmap转为Base64字符串
 *
 * @param bitmap
 * @return base64字符串
 */
public static String bitmapToString(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
    byte[] bytes = outputStream.toByteArray();
    //Base64算法加密,当字符串过长(一般超过76)时会自动在中间加一个换行符,字符串最后也会加一个换行符。
    // 导致和其他模块对接时结果不一致。所以不能用默认Base64.DEFAULT,而是Base64.NO_WRAP不换行
    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
 
Example 11
Source File: HashUtil.java    From Rumble with GNU General Public License v3.0 5 votes vote down vote up
public static final String computeStatusUUID(String author_uid, String group_gid, String post, long timeOfCreation) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(author_uid.getBytes());
        md.update(group_gid.getBytes());
        md.update(post.getBytes());
        md.update(ByteBuffer.allocate(8).putLong(timeOfCreation).array());
        return Base64.encodeToString(md.digest(),0, PushStatus.STATUS_ID_RAW_SIZE,Base64.NO_WRAP);
    }
    catch (NoSuchAlgorithmException ignore) {
        return null;
    }
}
 
Example 12
Source File: WebSocketHandler.java    From weex with Apache License 2.0 5 votes vote down vote up
private static String generateServerKey(String clientKey) {
  try {
    String serverKey = clientKey + SERVER_KEY_GUID;
    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    sha1.update(Utf8Charset.encodeUTF8(serverKey));
    return Base64.encodeToString(sha1.digest(), Base64.NO_WRAP);
  } catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
  }
}
 
Example 13
Source File: MainActivity.java    From UAF with Apache License 2.0 5 votes vote down vote up
private String getFacetID(PackageInfo paramPackageInfo) {
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(paramPackageInfo.signatures[0].toByteArray());
        Certificate certificate = CertificateFactory.getInstance("X509").generateCertificate(byteArrayInputStream);
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        String facetID = "android:apk-key-hash:" + Base64.encodeToString(((MessageDigest) messageDigest).digest(certificate.getEncoded()), 3);
        return facetID;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 14
Source File: Utils.java    From android-utils with MIT License 5 votes vote down vote up
/**
 * Serializes the Bitmap to Base64
 *
 * @return Base64 string value of a {@linkplain android.graphics.Bitmap} passed in as a parameter
 * @throws NullPointerException If the parameter bitmap is null.
 **/
public static String toBase64(Bitmap bitmap) {

    if (bitmap == null) {
        throw new NullPointerException("Bitmap cannot be null");
    }

    String base64Bitmap = null;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] imageBitmap = stream.toByteArray();
    base64Bitmap = Base64.encodeToString(imageBitmap, Base64.DEFAULT);

    return base64Bitmap;
}
 
Example 15
Source File: CdeHelper.java    From letv with Apache License 2.0 4 votes vote down vote up
public String getDownloadSpeedUrl(String linkshellUrl) {
    return TextUtils.isEmpty(linkshellUrl) ? linkshellUrl : "http://127.0.0.1:" + getServicePort() + "/state/play?enc=base64&url=" + Base64.encodeToString(linkshellUrl.getBytes(), 2);
}
 
Example 16
Source File: PluginResult.java    From wildfly-samples with MIT License 4 votes vote down vote up
public PluginResult(Status status, byte[] data, boolean binaryString) {
    this.status = status.ordinal();
    this.messageType = binaryString ? MESSAGE_TYPE_BINARYSTRING : MESSAGE_TYPE_ARRAYBUFFER;
    this.encodedMessage = Base64.encodeToString(data, Base64.NO_WRAP);
}
 
Example 17
Source File: ServiceDiscoveryResult.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
public String getVer() {
	return Base64.encodeToString(this.ver, Base64.NO_WRAP);
}
 
Example 18
Source File: Utils.java    From SecuritySample with Apache License 2.0 4 votes vote down vote up
public static String calcApkDigest(final Context context) {
    byte[] hashed2 = getApkFileDigest(context);
    String encoded2 = Base64.encodeToString(hashed2, Base64.NO_WRAP);
    return encoded2;
}
 
Example 19
Source File: MP4Config.java    From spydroid-ipcamera with GNU General Public License v3.0 4 votes vote down vote up
public MP4Config(byte[] sps, byte[] pps) {
	mPPS = Base64.encodeToString(pps, 0, pps.length, Base64.NO_WRAP);
	mSPS = Base64.encodeToString(sps, 0, sps.length, Base64.NO_WRAP);
	mProfilLevel = MP4Parser.toHexString(sps,1,3);
}
 
Example 20
Source File: TurbolinksHelper.java    From turbolinks-android with MIT License 3 votes vote down vote up
/**
 * <p>Gets the base64-encoded string of a local asset file (typically a Javascript or HTML file)</p>
 *
 * @param context  An activity context.
 * @param filePath Local file path relative to the main/src directory.
 * @return A base-64 encoded string of the file contents.
 * @throws IOException Typically if a file cannot be found or read in.
 */
static String getContentFromAssetFile(Context context, String filePath) throws IOException {
    InputStream inputStream = context.getAssets().open(filePath);
    byte[] buffer = new byte[inputStream.available()];
    inputStream.read(buffer);
    inputStream.close();
    return Base64.encodeToString(buffer, Base64.NO_WRAP);
}