Example usage for android.util Base64 NO_WRAP

List of usage examples for android.util Base64 NO_WRAP

Introduction

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

Prototype

int NO_WRAP

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

Click Source Link

Document

Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).

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 {//from w ww. java2s . c  o 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 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 {/* ww  w  .j a v a  2  s . com*/
        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

public static String generateBasicAuthToken(String name, String secret) {
    StringBuilder sb = new StringBuilder();
    sb.append(name).append(":").append(secret);
    try {//w w  w . j a  va 2 s.  c o  m
        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 String smallFileSha1(File file) {
    InputStream inputStream = null;
    try {//from w  w w. ja v  a 2  s .  c om
        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 {/*from  w  w w . j  a v  a 2s . c om*/
        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);
    }
}

From source file:Main.java

public static String getKeyFromParams(List<NameValuePair> nameValuePairs, String publishId) {
    Collections.sort(nameValuePairs, new Comparator<NameValuePair>() {
        @Override/* w  w  w  .j av  a2s . c  o  m*/
        public int compare(NameValuePair p1, NameValuePair p2) {
            return p1.getName().compareTo(p2.getName());
        }
    });

    StringBuilder keyBuilder = new StringBuilder();
    boolean isFirst = true;
    for (NameValuePair nvp : nameValuePairs) {
        if (!isFirst) {
            keyBuilder.append("&");
        }
        keyBuilder.append(nvp.getName()).append("=").append(nvp.getValue());
        isFirst = false;
    }

    keyBuilder.append("&").append(publishId);

    String key = keyBuilder.toString();
    byte[] keyBytes = getBytes(key);
    return getMd5Digest(new String(Base64.encode(keyBytes, Base64.NO_WRAP)));
}

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

/***
 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
 * request urls/*from w  w  w . j  a  v a 2 s .  com*/
 * 
 * @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

/**
 * Decrypt and decode ciphertext using 256-bit AES with key generated from password
 *
 * @param password used to generated key
 * @param base64EncodedCipherText the encrpyted message encoded with base64
 * @return message in Plain text (String UTF-8)
 * @throws GeneralSecurityException if there's an issue decrypting
 *//* w  w  w.  j a va2  s  .  c o  m*/
public static String decrypt(final String password, String base64EncodedCipherText) {

    try {
        final SecretKeySpec key = generateKey(password);

        log("base64EncodedCipherText", base64EncodedCipherText);
        byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP);
        log("decodedCipherText", decodedCipherText);

        byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText);

        log("decryptedBytes", decryptedBytes);
        String message = new String(decryptedBytes, CHARSET);
        log("message", message);

        return message;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}