Example usage for org.apache.commons.codec.binary Base64 Base64

List of usage examples for org.apache.commons.codec.binary Base64 Base64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 Base64.

Prototype

public Base64() 

Source Link

Document

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

Usage

From source file:com.trsst.server.AbstractMultipartAdapter.java

private InputStream getDataInputStream(InputStream stream) throws IOException {
    Base64 base64 = new Base64();
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];
    int i;/*  w  ww .  ja va  2 s  .  c  om*/
    while ((i = stream.read(buffer)) != -1) {
        bo.write(buffer, 0, i);
    }
    return new ByteArrayInputStream(base64.decode(bo.toByteArray()));
}

From source file:fi.aalto.seqpig.io.QseqStorer.java

@Override
public void checkSchema(ResourceSchema s) throws IOException {

    allQseqFieldNames = new HashMap<String, Integer>();
    String[] fieldNames = s.fieldNames();

    for (int i = 0; i < fieldNames.length; i++) {
        //System.out.println("field: "+fieldNames[i]);
        allQseqFieldNames.put(fieldNames[i], new Integer(i));
    }/*from   w w  w. j  a v a2s .co  m*/

    if (!( /*allQseqFieldNames.containsKey("instrument")
           && allQseqFieldNames.containsKey("run_number")
           && allQseqFieldNames.containsKey("flow_cell_id")
           && allQseqFieldNames.containsKey("lane")
           && allQseqFieldNames.containsKey("tile")
           && allQseqFieldNames.containsKey("xpos")
           && allQseqFieldNames.containsKey("ypos")
           && allQseqFieldNames.containsKey("read")
           && allQseqFieldNames.containsKey("filter")
           && allQseqFieldNames.containsKey("control_number")
           && allQseqFieldNames.containsKey("index_sequence")*/
    allQseqFieldNames.containsKey("sequence") && allQseqFieldNames.containsKey("quality")))
        throw new IOException("Error: Incorrect Qseq tuple-field name or compulsory field missing");

    //BASE64Encoder encode = new BASE64Encoder();
    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;
    //p.setProperty("someproperty", "value");

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allQseqFieldNames);
    ostream.close();
    //datastr = encode.encode(bstream.toByteArray());
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allQseqFieldNames", datastr); //new String(bstream.toByteArray(), "UTF8"));
}

From source file:fi.aalto.seqpig.io.FastqStorer.java

@Override
public void checkSchema(ResourceSchema s) throws IOException {

    allFastqFieldNames = new HashMap<String, Integer>();
    String[] fieldNames = s.fieldNames();

    for (int i = 0; i < fieldNames.length; i++) {
        //System.out.println("field: "+fieldNames[i]);
        allFastqFieldNames.put(fieldNames[i], new Integer(i));
    }/*w w  w. j ava  2s .  co m*/

    if (!( /*allFastqFieldNames.containsKey("instrument")
           && allFastqFieldNames.containsKey("run_number")
           && allFastqFieldNames.containsKey("flow_cell_id")
           && allFastqFieldNames.containsKey("lane")
           && allFastqFieldNames.containsKey("tile")
           && allFastqFieldNames.containsKey("xpos")
           && allFastqFieldNames.containsKey("ypos")
           && allFastqFieldNames.containsKey("read")
           && allFastqFieldNames.containsKey("filter")
           && allFastqFieldNames.containsKey("control_number")
           && allFastqFieldNames.containsKey("index_sequence")*/
    allFastqFieldNames.containsKey("sequence") && allFastqFieldNames.containsKey("quality")))
        throw new IOException("Error: Incorrect Fastq tuple-field name or compulsory field missing");

    //BASE64Encoder encode = new BASE64Encoder();
    Base64 codec = new Base64();
    Properties p = UDFContext.getUDFContext().getUDFProperties(this.getClass());
    String datastr;
    //p.setProperty("someproperty", "value");

    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
    ObjectOutputStream ostream = new ObjectOutputStream(bstream);
    ostream.writeObject(allFastqFieldNames);
    ostream.close();
    //datastr = encode.encode(bstream.toByteArray());
    datastr = codec.encodeBase64String(bstream.toByteArray());
    p.setProperty("allFastqFieldNames", datastr); //new String(bstream.toByteArray(), "UTF8"));
}

From source file:cz.fi.muni.xkremser.editor.server.AuthenticationServlet.java

private String decode(HttpServletRequest req, String authHeader) {
    //always wise to assert your assumptions
    assert authHeader.substring(0, 6).equals("Basic ");
    //will contain "Ym9iOnNlY3JldA=="
    String basicAuthEncoded = authHeader.substring(6);
    //will contain "bob:secret"
    String basicAuthAsString = new String(new Base64().decode(basicAuthEncoded.getBytes()));
    return basicAuthAsString;
}

From source file:com.hss.assignment4.LoginFrame.java

private String hashing(String password) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-512");
    md.update(password.getBytes());//w w w.  j  a v a  2  s.c om
    byte byteData[] = md.digest();
    String encodedString = new Base64().encodeBase64String(byteData);
    return encodedString;
}

From source file:com.shuffle.bitcoin.blockchain.Btcd.java

@Override
protected boolean send(Bitcoin.Transaction t)
        throws ExecutionException, InterruptedException, CoinNetworkException {
    if (!t.canSend || t.sent) {
        return false;
    }/*from  www  .j a va  2  s . co  m*/

    String hexTx = null;
    try {
        hexTx = DatatypeConverter.printHexBinary(t.bitcoinj().bitcoinSerialize());
    } catch (BlockStoreException e) {
        return false;
    } catch (IOException er) {
        return false;
    }
    String requestBody = "{\"jsonrpc\":\"2.0\",\"id\":\"null\",\"method\":\"sendrawtransaction\", \"params\":[\""
            + hexTx + "\"]}";

    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        return false;
    }
    connection.setDoOutput(true);
    try {
        connection.setRequestMethod("POST");
    } catch (java.net.ProtocolException e) {
        return false;
    }
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    Base64 b = new Base64();
    String authString = rpcuser + ":" + rpcpass;
    String encoding = b.encodeAsString(authString.getBytes());
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
    connection.setDoInput(true);
    OutputStream out;
    try {
        out = connection.getOutputStream();
    } catch (IOException e) {
        return false;
    }
    try {
        out.write(requestBody.getBytes());
    } catch (IOException e) {
        return false;
    }

    try {
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
            return false;
    } catch (IOException e) {
        return false;
    }

    InputStream is;
    try {
        is = connection.getInputStream();
    } catch (IOException e) {
        return false;
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while (true) {
        try {
            if ((line = rd.readLine()) == null)
                break;
        } catch (IOException e) {
            return false;
        }
        response.append(line);
        response.append('\r');
    }

    try {
        rd.close();
    } catch (IOException e) {
        return false;
    }

    JSONObject json = new JSONObject(response.toString());
    if (json.isNull("result")) {
        JSONObject errorObj = json.getJSONObject("error");
        String errorMsg = errorObj.getString("message");
        String parsed = errorMsg.substring(0, 37);
        // transaction is already in mempool, return true
        if (parsed.equals("TX rejected: already have transaction")) {
            return true;
        }
        throw new CoinNetworkException(errorMsg);
    }

    return true;
}

From source file:com.bcmcgroup.flare.client.ClientUtil.java

/**
 * Encrypt plain text using AES/*from w  w w.  j  a va  2s .  c  o m*/
 *
 * @param plainText the String text to be encrypted in AES
 * @return the encrypted text String
 *
 */
public static String encrypt(String plainText) {
    try {
        byte[] saltBytes = salt.getBytes("UTF-8");
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec spec = new PBEKeySpec(seeds, saltBytes, iterations, keySize);
        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivBytes));
        byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
        return new Base64().encodeAsString(encryptedTextBytes);
    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException when attempting to encrypt a string. ");
    } catch (InvalidKeySpecException e) {
        logger.error("InvalidKeySpecException when attempting to encrypt a string. ");
    } catch (NoSuchPaddingException e) {
        logger.error("NoSuchPaddingException when attempting to encrypt a string. ");
    } catch (IllegalBlockSizeException e) {
        logger.error("IllegalBlockSizeException when attempting to encrypt a string. ");
    } catch (BadPaddingException e) {
        logger.error("BadPaddingException when attempting to encrypt a string. ");
    } catch (UnsupportedEncodingException e) {
        logger.error("UnsupportedEncodingException when attempting to encrypt a string. ");
    } catch (InvalidKeyException e) {
        logger.error("InvalidKeyException when attempting to encrypt a string. ");
    } catch (InvalidAlgorithmParameterException e) {
        logger.error("InvalidAlgorithmParameterException when attempting to encrypt a string. ");
    }
    return null;
}

From source file:com.maxpowered.amazon.advertising.api.SignedRequestsHelper.java

/**
 * Compute the HMAC./*from  w w w. j  av  a 2 s . c o m*/
 *
 * @param stringToSign
 *            String to compute the HMAC over.
 * @return base64-encoded hmac value.
 */
private String hmac(final String stringToSign) {
    String signature = null;
    byte[] data;
    byte[] rawHmac;
    try {
        data = stringToSign.getBytes(UTF8_CHARSET);
        rawHmac = mac.doFinal(data);
        final Base64 encoder = new Base64();
        signature = new String(encoder.encode(rawHmac));
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
    }
    return signature;
}

From source file:com.netscape.cmsutil.util.Utils.java

/**
 * Converts a byte array into a single-line Base-64 encoded string.
 * The line is not terminated with CRLF.
 *
 * @param bytes byte array/*from ww  w .  ja  v a 2  s.c o  m*/
 * @return base-64 encoded data
 */
public static String base64encodeSingleLine(byte[] bytes) {
    return new Base64().encodeToString(bytes);
}

From source file:com.itude.mobile.android.util.DataUtil.java

private byte[] unobfuscate(byte[] data) {
    // reverse step 3:  write a special signature (binary -itu)
    // and /*from w  w w . ja  va 2  s.c  o m*/
    // Reversing step 2: use java.util.zip.Deflator to compress
    data = DataUtil.getInstance().decompress(data, 4);
    // reverse step 1: convert using base64
    Base64 b64 = new Base64();

    return b64.decode(data);
}