Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:org.jclouds.encryption.bouncycastle.BouncyCastleEncryptionService.java

License:Apache License

public String toBase64String(byte[] resBuf) {
    return new String(Base64.encode(resBuf));
}

From source file:org.jclouds.http.HttpUtils.java

License:Apache License

public static String toBase64String(byte[] resBuf) {
    return new String(Base64.encode(resBuf));
}

From source file:org.jclouds.joyent.blobstore.JoyentBlobRequestSigner.java

License:Apache License

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    LOG.debug("signing request: " + request.getHeaders());

    if (keyPair_ == null) {
        synchronized (this) {
            if (keyPair_ == null) {
                try {
                    keyPair_ = getKeyPair(certClasspath_);
                } catch (IOException e) {
                    LOG.error(e);/*from  www . j a va2  s  . c  o  m*/
                    throw new HttpException("Can't load key pair", e);
                }
            }
        }
    }

    HttpRequest res = request;

    String reqURL = request.getEndpoint().toString();
    String path = request.getEndpoint().getPath();
    String storPath = "/" + identity + "/" + JoyentConstants.STOR_PATH;
    if (!path.startsWith(storPath)) {
        int pathInd = reqURL.lastIndexOf(path);
        String newUrl = reqURL.substring(0, pathInd) + storPath + reqURL.substring(pathInd);
        res = res.toBuilder().endpoint(newUrl).build();
    }

    String date = request.getFirstHeaderOrNull(HttpHeaders.DATE);
    if (date == null) {
        Date now = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
        date = DATE_FORMAT.format(now);
        LOG.debug("setting date header: " + date);
        res = res.toBuilder().addHeader(HttpHeaders.DATE, date).build();
    }
    try {
        Signature sig = Signature.getInstance(SIGNING_ALGORITHM);
        sig.initSign(keyPair_.getPrivate());
        String signingString = String.format(AUTHZ_SIGNING_STRING, date);
        sig.update(signingString.getBytes("UTF-8"));
        byte[] signedDate = sig.sign();
        byte[] encodedSignedDate = Base64.encode(signedDate);
        String authzHeader = String.format(AUTHZ_HEADER, identity, fingerPrint_, new String(encodedSignedDate));
        res = res.toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authzHeader).build();
    } catch (NoSuchAlgorithmException e) {
        throw new HttpException("invalid algorithm", e);
    } catch (InvalidKeyException e) {
        throw new HttpException("invalid key", e);
    } catch (SignatureException e) {
        throw new HttpException("invalid signature", e);
    } catch (UnsupportedEncodingException e) {
        throw new HttpException("invalid encoding", e);
    }

    return res;
}

From source file:org.jdal.auth.AuthHashMD5.java

License:Apache License

/**
 * crypt a password /*from  w  w w. j  av  a2  s.  c  o  m*/
 * @param suppliedPassword
 * @return crypted password
 * @throws NoSuchAlgorithmException
 */
public String crypt(String suppliedPassword) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(suppliedPassword.getBytes());
    String encriptedPassword = null;
    try {
        encriptedPassword = new String(Base64.encode(md.digest()), "ASCII");
    } catch (UnsupportedEncodingException e) {
    }
    return encriptedPassword;
}

From source file:org.jose4j.base64url.internal.apache.commons.codec.binary.Base64Test.java

License:Apache License

/**
 * Test the Base64 implementation//from  w w  w.j  a va 2s.  c om
 */
@Test
public void testBase64() {
    final String content = "Hello World";
    String encodedContent;
    byte[] encodedBytes = Base64.encodeBase64(StringUtil.getBytesUtf8(content));
    encodedContent = StringUtil.newStringUtf8(encodedBytes);
    assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);

    Base64 b64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, null); // null lineSeparator same as saying no-chunking
    encodedBytes = b64.encode(StringUtil.getBytesUtf8(content));
    encodedContent = StringUtil.newStringUtf8(encodedBytes);
    assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);

    b64 = new Base64(0, null); // null lineSeparator same as saying no-chunking
    encodedBytes = b64.encode(StringUtil.getBytesUtf8(content));
    encodedContent = StringUtil.newStringUtf8(encodedBytes);
    assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);

    // bogus characters to decode (to skip actually) {e-acute*6}
    final byte[] decode = b64.decode("SGVsbG{\u00e9\u00e9\u00e9\u00e9\u00e9\u00e9}8gV29ybGQ=");
    final String decodeString = StringUtil.newStringUtf8(decode);
    assertEquals("decode hello world", "Hello World", decodeString);
}

From source file:org.jose4j.base64url.internal.apache.commons.codec.binary.Base64Test.java

License:Apache License

@Test
public void testConstructor_Int_ByteArray_Boolean() {
    final Base64 base64 = new Base64(65, new byte[] { '\t' }, false);
    final byte[] encoded = base64.encode(Base64TestData.DECODED);
    String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
    expectedResult = expectedResult.replace('\n', '\t');
    final String result = StringUtil.newStringUtf8(encoded);
    assertEquals("new Base64(65, \\t, false)", expectedResult, result);
}

From source file:org.jose4j.base64url.internal.apache.commons.codec.binary.Base64Test.java

License:Apache License

@Test
public void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
    // url-safe variation
    final Base64 base64 = new Base64(64, new byte[] { '\t' }, true);
    final byte[] encoded = base64.encode(Base64TestData.DECODED);
    String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
    expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no == padding.
    expectedResult = expectedResult.replace('\n', '\t');
    expectedResult = expectedResult.replace('+', '-');
    expectedResult = expectedResult.replace('/', '_');
    final String result = StringUtil.newStringUtf8(encoded);
    assertEquals("new Base64(64, \\t, true)", result, expectedResult);
}

From source file:org.jose4j.base64url.internal.apache.commons.codec.binary.Base64Test.java

License:Apache License

@Test
public void testObjectEncode() throws Exception {
    final Base64 b64 = new Base64();
    assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(StandardCharsets.UTF_8))));
}

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

private void writeEncoded(BufferedWriter out, byte[] bytes) throws IOException {
    char[] buf = new char[64];
    bytes = Base64.encode(bytes);
    for (int i = 0; i < bytes.length; i += buf.length) {
        int index = 0;

        while (index != buf.length) {
            if ((i + index) >= bytes.length) {
                break;
            }/*  w w w.jav a 2s.co m*/
            buf[index] = (char) bytes[i + index];
            index++;
        }
        out.write(buf, 0, index);
        out.newLine();
    }
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static void writeEncoded(BufferedWriter out, byte[] bytes) throws IOException {
    char[] buf = new char[64];
    bytes = Base64.encode(bytes);
    for (int i = 0; i < bytes.length; i += buf.length) {
        int index = 0;

        while (index != buf.length) {
            if ((i + index) >= bytes.length) {
                break;
            }/* w w  w.ja  v  a  2s  .  com*/
            buf[index] = (char) bytes[i + index];
            index++;
        }
        out.write(buf, 0, index);
        out.newLine();
    }
}