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:com.streamsets.lib.security.http.TestSignedSSOTokenParser.java

@Override
protected String createTokenStr(SSOUserPrincipal principal) throws Exception {
    String info = encodeToken(principal);
    String version = createParser().getType();
    String signature = Base64
            .encodeBase64String(DataSignature.get().getSigner(getKeyPair().getPrivate()).sign(info.getBytes()));
    return version + SSOConstants.TOKEN_PART_SEPARATOR + signature + SSOConstants.TOKEN_PART_SEPARATOR + info;
}

From source file:cloudeventbus.codec.Encoder.java

@Override
public void encode(ChannelHandlerContext ctx, Frame frame, ByteBuf out) throws Exception {
    LOGGER.debug("Encoding frame {}", frame);
    switch (frame.getFrameType()) {
    case AUTHENTICATE:
        final AuthenticationRequestFrame authenticationRequestFrame = (AuthenticationRequestFrame) frame;
        out.writeByte(FrameType.AUTHENTICATE.getOpcode());
        out.writeByte(' ');
        final String challenge = Base64.encodeBase64String(authenticationRequestFrame.getChallenge());
        writeString(out, challenge);// ww  w.  j a  v  a 2s .c  o m
        break;
    case AUTH_RESPONSE:
        final AuthenticationResponseFrame authenticationResponseFrame = (AuthenticationResponseFrame) frame;
        out.writeByte(FrameType.AUTH_RESPONSE.getOpcode());
        out.writeByte(' ');

        // Write certificate chain
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final OutputStream base64Out = new Base64OutputStream(outputStream, true, Integer.MAX_VALUE,
                new byte[0]);
        CertificateStoreLoader.store(base64Out, authenticationResponseFrame.getCertificates());
        out.writeBytes(outputStream.toByteArray());
        out.writeByte(' ');

        // Write salt
        final byte[] encodedSalt = Base64.encodeBase64(authenticationResponseFrame.getSalt());
        out.writeBytes(encodedSalt);
        out.writeByte(' ');

        // Write signature
        final byte[] encodedDigitalSignature = Base64
                .encodeBase64(authenticationResponseFrame.getDigitalSignature());
        out.writeBytes(encodedDigitalSignature);
        break;
    case ERROR:
        final ErrorFrame errorFrame = (ErrorFrame) frame;
        out.writeByte(FrameType.ERROR.getOpcode());
        out.writeByte(' ');
        writeString(out, Integer.toString(errorFrame.getCode().getErrorNumber()));
        if (errorFrame.getMessage() != null) {
            out.writeByte(' ');
            writeString(out, errorFrame.getMessage());
        }
        break;
    case GREETING:
        final GreetingFrame greetingFrame = (GreetingFrame) frame;
        out.writeByte(FrameType.GREETING.getOpcode());
        out.writeByte(' ');
        writeString(out, Integer.toString(greetingFrame.getVersion()));
        out.writeByte(' ');
        writeString(out, greetingFrame.getAgent());
        out.writeByte(' ');
        writeString(out, Long.toString(greetingFrame.getId()));
        break;
    case PING:
        out.writeByte(FrameType.PING.getOpcode());
        break;
    case PONG:
        out.writeByte(FrameType.PONG.getOpcode());
        break;
    case PUBLISH:
        final PublishFrame publishFrame = (PublishFrame) frame;
        out.writeByte(FrameType.PUBLISH.getOpcode());
        out.writeByte(' ');
        writeString(out, publishFrame.getSubject().toString());
        if (publishFrame.getReplySubject() != null) {
            out.writeByte(' ');
            writeString(out, publishFrame.getReplySubject().toString());
        }
        out.writeByte(' ');
        final ByteBuf body = Unpooled.wrappedBuffer(publishFrame.getBody().getBytes(CharsetUtil.UTF_8));
        writeString(out, Integer.toString(body.readableBytes()));
        out.writeBytes(Codec.DELIMITER);
        out.writeBytes(body);
        break;
    case SERVER_READY:
        out.writeByte(FrameType.SERVER_READY.getOpcode());
        break;
    case SUBSCRIBE:
        final SubscribeFrame subscribeFrame = (SubscribeFrame) frame;
        out.writeByte(FrameType.SUBSCRIBE.getOpcode());
        out.writeByte(' ');
        writeString(out, subscribeFrame.getSubject().toString());
        break;
    case UNSUBSCRIBE:
        final UnsubscribeFrame unsubscribeFrame = (UnsubscribeFrame) frame;
        out.writeByte(FrameType.UNSUBSCRIBE.getOpcode());
        out.writeByte(' ');
        writeString(out, unsubscribeFrame.getSubject().toString());
        break;
    default:
        throw new EncodingException("Don't know how to encode message of type " + frame.getClass().getName());
    }
    out.writeBytes(Codec.DELIMITER);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoDigestService.java

public String sha512Base64(String dataB64) {
    validateB64(dataB64);
    return Base64.encodeBase64String(DigestUtils.sha512(Base64.decodeBase64(dataB64)));
}

From source file:com.muk.ext.security.impl.DefaultNonceService.java

@Override
public String encode(byte[] byteArray) {
    return Base64.encodeBase64String(byteArray);
}

From source file:com.apipulse.bastion.modules.foundational.ScriptableTransformer.java

public ScriptableTransformer(int index, StageConfig config) {
    super(index, config);
    scriptId = config.getString("scriptId");
    script = config.getString("script");
    if (scriptId == null && script != null)
        scriptId = Base64.encodeBase64String(Md5Crypt.md5Crypt(script.getBytes()).getBytes());
    shell = new GroovyShell();
}

From source file:com.linecorp.platform.channel.sample.BusinessConnect.java

public boolean validateBCRequest(String httpRequestBody, String channelSecret, String channelSignature) {
    if (httpRequestBody == null || channelSecret == null || channelSignature == null) {
        return false;
    }/* w w w .ja v a 2  s  .c o  m*/

    String signature;
    SecretKeySpec key = new SecretKeySpec(channelSecret.getBytes(), "HmacSHA256");
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(key);
        byte[] source = httpRequestBody.getBytes("UTF-8");
        signature = Base64.encodeBase64String(mac.doFinal(source));
    } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
        e.printStackTrace();
        return false;
    }
    return channelSignature.equals(signature);
}

From source file:co.cask.cdap.security.auth.AccessTokenTransformer.java

/**
 *
 * @param accessToken is the access token from Authorization header in HTTP Request
 * @return the serialized access token identifer
 * @throws IOException//w w w.  j  a  va  2  s  .c  o  m
 */
public AccessTokenIdentifierPair transform(String accessToken) throws IOException {
    byte[] decodedAccessToken = Base64.decodeBase64(accessToken);
    AccessToken accessTokenObj = accessTokenCodec.decode(decodedAccessToken);
    AccessTokenIdentifier accessTokenIdentifierObj = accessTokenObj.getIdentifier();
    byte[] encodedAccessTokenIdentifier = accessTokenIdentifierCodec.encode(accessTokenIdentifierObj);
    return new AccessTokenIdentifierPair(Base64.encodeBase64String(encodedAccessTokenIdentifier).trim(),
            accessTokenIdentifierObj);
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.MessageCipher.java

public String encrypt(String source, String password) {

    source = source.trim();// w ww  .j a v  a 2s. c  om
    byte[] plaintext;

    plaintext = Compression.compress(source.getBytes(Charsets.UTF_8));

    AESEncryptedMessage encryptedMessage;

    try {
        encryptedMessage = aes.encrypt(plaintext, password);
    } catch (EncryptionException ex) {
        return null;
    }

    String salt = encryptedMessage.getSalt();
    byte[] iv = encryptedMessage.getIv();
    byte[] ciphertext = encryptedMessage.getCiphertext();

    StringBuilder message = new StringBuilder();

    String iv_base64 = Base64.encodeBase64String(iv);
    String ciphertext_base64 = new String(Base64.encodeBase64(ciphertext, true));

    message.append(MESSAGE_HEADER).append(';').append(salt).append(';').append(iv_base64).append(';')
            .append('\n').append(ciphertext_base64);

    return message.toString();

}

From source file:cn.lynx.emi.license.GenerateLicense.java

private static final String encrypt(String key, String data) {
    byte[] corekey = Base64.decodeBase64(key);

    PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey);

    try {//from   ww  w  . j  a va  2  s  .co m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key privateKey = keyFactory.generatePrivate(pkspec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
        System.out.println("after encrypt, len=" + encData.length);
        return Base64.encodeBase64String(encData);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilderTest.java

@Test
public void likeShouldCreateCompleteResponseDefinitionCopy() throws Exception {
    ResponseDefinition originalResponseDefinition = ResponseDefinitionBuilder.responseDefinition()
            .withStatus(200).withStatusMessage("OK").withBody("some body")
            .withBase64Body(Base64.encodeBase64String("some body".getBytes(Charsets.UTF_8)))
            .withBodyFile("some_body.json").withHeader("some header", "some value").withFixedDelay(100)
            .withUniformRandomDelay(1, 2).withChunkedDribbleDelay(1, 1000).withFault(Fault.EMPTY_RESPONSE)
            .withTransformers("some transformer").withTransformerParameter("some param", "some value").build();

    ResponseDefinition copiedResponseDefinition = ResponseDefinitionBuilder.like(originalResponseDefinition)
            .build();//from w w  w  . jav a 2  s.c  o  m

    assertThat(copiedResponseDefinition, is(originalResponseDefinition));
}