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:de.thm.arsnova.util.ImageUtils.java

/**
 * Converts an image to an Base64 String.
 *
 * @param  imageUrl The image url as a {@link String}
 * @return The Base64 {@link String} of the image on success, otherwise <code>null</code>.
 *//* w w  w.ja va 2 s . c  om*/
public String encodeImageToString(final String imageUrl) {

    final String[] urlParts = imageUrl.split("\\.");

    // get format
    //
    // The format is read dynamically. We have to take control
    // in the frontend that no unsupported formats are transmitted!
    if (urlParts.length > 0) {
        final String extension = urlParts[urlParts.length - 1];

        return IMAGE_PREFIX_START + extension + IMAGE_PREFIX_MIDDLE
                + Base64.encodeBase64String(convertFileToByteArray(imageUrl));
    }

    return null;
}

From source file:models.FrontendUser.java

/**
 * Constructs a frontend user from the given information.
 * <p>/* www.  j  a  v  a  2  s  . com*/
 * This constructor takes the clear text password, generates
 * a salt for the given password and hashes it using the generated
 * salt.
 *
 * @param firstName First name of the user.
 * @param lastName  Last name of the user.
 * @param password  Clear-text password of the user.
 * @param mail      Mail address of the user.
 */
public FrontendUser(String firstName, String lastName, String password, String mail) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.mail = mail;

    byte[] generatedSalt = Password.getInstance().generateSalt();

    this.salt = Base64.encodeBase64String(generatedSalt);

    this.password = new String(Password.getInstance().hash(password.toCharArray(), generatedSalt));
}

From source file:com.springcryptoutils.core.digest.DigesterImpl.java

/**
 * Returns the message digest. The representation of the message digest
 * depends on the outputMode property./* w  w  w. j  a  va 2s.co  m*/
 *
 * @param message the message
 * @return the message digest
 * @see #setOutputMode(OutputMode)
 */
public String digest(String message) {
    final byte[] messageAsByteArray;

    try {
        messageAsByteArray = message.getBytes(charsetName);
    } catch (UnsupportedEncodingException e) {
        throw new DigestException("error converting message to byte array: charsetName=" + charsetName, e);
    }

    final byte[] digest = digest(messageAsByteArray);

    switch (outputMode) {
    case BASE64:
        return Base64.encodeBase64String(digest);
    case HEX:
        return Hex.encodeHexString(digest);
    default:
        return null;
    }
}

From source file:edu.kit.scc.cdmi.rest.CapabilitiesTest.java

@Test
public void testGetCapabilitiesNotAuthorized() {
    String authString = Base64.encodeBase64String((restUser + "1:" + restPassword).getBytes());

    Response response = given().header("Authorization", "Basic " + authString).and()
            .header("Content-Type", "application/cdmi-capability").when().get("/cdmi_capabilities").then()
            .statusCode(org.apache.http.HttpStatus.SC_UNAUTHORIZED).extract().response();

    log.debug("Response {}", response.asString());
}

From source file:cipher.UsableCipher.java

public void encryptDecryptAndPrint(String plaintext) throws Exception {
    byte[] ciphertext = encrypt(plaintext);
    System.out.println(String.format("%45s -> PLAIN: %s     CIPHERTEXT: %-25s  %d", transformation, plaintext,
            Base64.encodeBase64String(ciphertext), ciphertext.length));
    if (!plaintext.equals(decrypt(ciphertext))) {
        System.out.println("OOPS!!! IT DID NOT DECRYPT RIGHT! Plaintext --> " + plaintext);
    }/*  w w w.j  av  a 2  s. co  m*/
}

From source file:com.consol.citrus.ws.actions.SendSoapMessageAction.java

@Override
protected SoapMessage createMessage(TestContext context, String messageType) {
    Message message = super.createMessage(context, getMessageType());

    SoapMessage soapMessage = new SoapMessage(message).mtomEnabled(mtomEnabled);
    try {/*w w w .j a  va  2 s .c om*/
        for (SoapAttachment attachment : attachments) {
            attachment.resolveDynamicContent(context);

            if (mtomEnabled) {
                String messagePayload = soapMessage.getPayload(String.class);
                String cid = CID_MARKER + attachment.getContentId();

                if (attachment.isMtomInline() && messagePayload.contains(cid)) {
                    byte[] attachmentBinaryData = FileUtils
                            .readToString(attachment.getInputStream(),
                                    Charset.forName(attachment.getCharsetName()))
                            .getBytes(Charset.forName(attachment.getCharsetName()));
                    if (attachment.getEncodingType().equals(SoapAttachment.ENCODING_BASE64_BINARY)) {
                        log.info("Adding inline base64Binary data for attachment: %s", cid);
                        messagePayload = messagePayload.replaceAll(cid,
                                Base64.encodeBase64String(attachmentBinaryData));
                    } else if (attachment.getEncodingType().equals(SoapAttachment.ENCODING_HEX_BINARY)) {
                        log.info("Adding inline hexBinary data for attachment: %s", cid);
                        messagePayload = messagePayload.replaceAll(cid,
                                Hex.encodeHexString(attachmentBinaryData).toUpperCase());
                    } else {
                        throw new CitrusRuntimeException(String.format(
                                "Unsupported encoding type '%s' for SOAP attachment: %s - choose one of %s or %s",
                                attachment.getEncodingType(), cid, SoapAttachment.ENCODING_BASE64_BINARY,
                                SoapAttachment.ENCODING_HEX_BINARY));
                    }
                } else {
                    messagePayload = messagePayload.replaceAll(cid, String.format(
                            "<xop:Include xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href=\"%s\"/>",
                            cid));
                    soapMessage.addAttachment(attachment);
                }

                soapMessage.setPayload(messagePayload);
            } else {
                soapMessage.addAttachment(attachment);
            }
        }
    } catch (IOException e) {
        throw new CitrusRuntimeException(e);
    }

    return soapMessage;
}

From source file:ch.icclab.cyclops.client.CloudStackAuth.java

/**
 * Simple SHA1 implementation with Base64 encoding of message
 *
 * @param query header to be signed//from  w ww.  j  a v  a  2  s  .c  o  m
 * @return signed string
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws UnsupportedEncodingException
 */
private String signRequest(String query)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    logger.trace("Signing the CloudStack API query");

    Mac sha1_HMAC = Mac.getInstance("HmacSHA1");
    SecretKeySpec secret = new SecretKeySpec(apiConnection.getCloudStackSecretKey().getBytes(), "HmacSHA1");
    sha1_HMAC.init(secret);

    // now sign it and return Base64 representation
    String signature = Base64.encodeBase64String(sha1_HMAC.doFinal(query.getBytes()));

    return URLEncoder.encode(signature, "UTF-8");
}

From source file:com.ddling.client.smtp.SmtpClient.java

private void auth() throws IOException {
    sendData("Auth login");

    int response = getResponse();

    if (response != 334) {
        throw new IOException("Auth login Fail!");
    }//from   w w  w .ja  va2s  .  c om

    String username = Base64.encodeBase64String(UserService.getLoginUser().getUsername().getBytes());

    sendData(username);

    response = getResponse();

    if (response != 334) {
        throw new IOException("Username Auth fail!");
    }

    String password = Base64.encodeBase64String(UserService.getLoginUser().getPassword().getBytes());

    sendData(password);
    response = getResponse();

    if (response != 235) {
        throw new IOException("Auth password fail!");
    }
}

From source file:graphene.util.crypto.PasswordHashTest.java

@Test
public void testSalt() {
    final PasswordHash p = new PasswordHash();

    try {/*from w  w w . j av  a2 s. co  m*/
        // System.out.println(p.createHash("password"));
        final byte[] s = p.pbkdf2("password".toCharArray(), "salt".getBytes(), PasswordHash.PBKDF2_ITERATIONS,
                PasswordHash.HASH_BYTE_SIZE);
        System.out.println(s);
        System.out.println("base64 bytes: " + Base64.encodeBase64String("salt".getBytes()));
        System.out.println("hex of \"salt\".getBytes(): " + p.toHex("salt".getBytes()));
    } catch (final NoSuchAlgorithmException e) {
        logger.error(e.getMessage());
    } catch (final InvalidKeySpecException e) {
        logger.error(e.getMessage());
    }
}