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

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

Introduction

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

Prototype

public static String encodeBase64URLSafeString(final byte[] binaryData) 

Source Link

Document

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.

Usage

From source file:io.stallion.utils.GeneralUtils.java

/**
 * Generates a random string using the psuedo-random module, of the given length,
 * using URL safe base64 characters//from w  ww  . j  a v  a 2s  .c o  m
 *
 * @param length
 * @return
 */
public static String randomToken(int length) {
    byte[] r = new byte[256]; //Means 2048 bit
    new Random().nextBytes(r);
    String s = Base64.encodeBase64URLSafeString(r).substring(0, length);
    return s;
}

From source file:com.smartitengineering.cms.spi.impl.events.EventConsumerTest.java

@Test
public void testContentTypeConsumptionWithInvalidMessage() {
    mockery.checking(new Expectations() {

        {/*from   www. j a v a 2  s.  c  om*/
        }
    });
    EventConsumer consumer = injector.getInstance(EventConsumer.class);
    consumer.consume(MSG_TYPE, "CONTENT_TYPE\nCREATE\n" + Base64
            .encodeBase64URLSafeString(StringUtils.getBytesUtf8("random string\nfor test\nof content type")));
    mockery.assertIsSatisfied();
}

From source file:com.tremolosecurity.unison.google.u2f.U2FServerUnison.java

@Override
public U2fSignRequest getSignRequest(String accountName, String appId) throws U2FException {
    if (log.isDebugEnabled()) {
        log.debug(">> getSignRequest " + accountName);
    }/*from   w  w  w  .  j  av a 2  s . c om*/

    List<SecurityKeyData> securityKeyDataList = dataStore.getSecurityKeyData(accountName);

    byte[] challenge = challengeGenerator.generateChallenge(accountName);
    String challengeBase64 = Base64.encodeBase64URLSafeString(challenge);

    ImmutableList.Builder<RegisteredKey> registeredKeys = ImmutableList.builder();
    if (log.isDebugEnabled()) {
        log.debug("  challenge: " + Hex.encodeHexString(challenge));
    }

    for (SecurityKeyData securityKeyData : securityKeyDataList) {
        SignSessionData sessionData = new SignSessionData(accountName, appId, challenge,
                securityKeyData.getPublicKey());
        String sessionId = dataStore.storeSessionData(sessionData);

        byte[] keyHandle = securityKeyData.getKeyHandle();
        List<Transports> transports = securityKeyData.getTransports();
        if (log.isDebugEnabled()) {
            log.debug("-- Output --");
            log.debug("  sessionId: " + sessionId);
            log.debug("  keyHandle: " + Hex.encodeHexString(keyHandle));
        }
        String keyHandleBase64 = Base64.encodeBase64URLSafeString(keyHandle);

        if (log.isDebugEnabled()) {
            log.debug("<< getRegisteredKey " + accountName);
        }

        registeredKeys.add(new RegisteredKey(U2FConsts.U2F_V2, keyHandleBase64, transports, appId, sessionId));
    }

    return new U2fSignRequest(challengeBase64, registeredKeys.build());
}

From source file:er.extensions.crypting.ERXCrypto.java

/**
 * Base64url encodes the passed in byte[]
 * //from   w  w w . j av a  2 s.  com
 * @param byteArray the byte array to URL encode
 * @return the encoded string
 */
public static String base64urlEncode(byte[] byteArray) {
    return Base64.encodeBase64URLSafeString(byteArray);
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public String getEncodedMimeMessage() throws IOException, MessagingException {

    ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
    getMimeMessage().writeTo(buffer);/*from  ww w.jav  a 2 s.  c  om*/

    String encodedEmail = Base64.encodeBase64URLSafeString(buffer.toByteArray());

    return encodedEmail;
}

From source file:net.sourceforge.docfetcher.util.AppUtil.java

private static String encodeBase64(String input) {
    String encodedBytes = Base64.encodeBase64URLSafeString(input.getBytes());
    return new String(encodedBytes);
}

From source file:net.oauth.jsontoken.JsonTokenTest.java

public void testPublicKey() throws Exception {

    RsaSHA256Signer signer = new RsaSHA256Signer("google.com", "key1", privateKey);

    JsonToken token = new JsonToken(signer, clock);
    token.setParam("bar", 15);
    token.setParam("foo", "some value");
    token.setExpiration(clock.now().withDurationAdded(60, 1));

    String tokenString = token.serializeAndSign();

    assertNotNull(token.toString());//from  w w w  .  ja v a2 s .  co m

    JsonTokenParser parser = new JsonTokenParser(clock, locators, new IgnoreAudience());
    token = parser.verifyAndDeserialize(tokenString);
    assertEquals("google.com", token.getIssuer());
    assertEquals(15, token.getParamAsPrimitive("bar").getAsLong());
    assertEquals("some value", token.getParamAsPrimitive("foo").getAsString());

    // now test what happens if we tamper with the token
    JsonObject payload = new JsonParser()
            .parse(StringUtils.newStringUtf8(Base64.decodeBase64(tokenString.split(Pattern.quote("."))[1])))
            .getAsJsonObject();
    payload.remove("bar");
    payload.addProperty("bar", 14);
    String payloadString = new Gson().toJson(payload);
    String[] parts = tokenString.split("\\.");
    parts[1] = Base64.encodeBase64URLSafeString(payloadString.getBytes());
    assertEquals(3, parts.length);

    String tamperedToken = parts[0] + "." + parts[1] + "." + parts[2];

    try {
        token = parser.verifyAndDeserialize(tamperedToken);
        fail("verification should have failed");
    } catch (SignatureException e) {
        // expected
    }
}

From source file:com.fujitsu.dc.common.utils.DcCoreUtils.java

/**
 * Base64url??.// www  .  j  av a  2 s  .c o  m
 * ??RFC648??????????????? ---------------------------------------------------
 * http://tools.ietf.org/html/rfc4648 --------------------------------------------------- 5. Base 64 Encoding with
 * URL and Filename Safe Alphabet The Base 64 encoding with an URL and filename safe alphabet has been used in [12].
 * ????????[12]? ???? An alternative alphabet has been suggested that would use "~" as the
 * 63rd character. Since the "~" character has special meaning in some file system environments, the encoding
 * described in this section is recommended instead. The remaining unreserved URI character is ".", but some file
 * system environments do not permit multiple "." in a filename, thus making the "." character unattractive as well.
 * ???????"~"???? ???"~"?????????? ??????????????????
 * ????"."???????"." ????"."???? The pad character "=" is typically percent-encoded
 * when used in an URI [9], but if the data length is known implicitly, this can be avoided by skipping the padding;
 * see section 3.2. [9]???????"="???? ????????????????
 * ??????????.??? This encoding may be referred to as "base64url". This encoding should not be regarded
 * as the same as the "base64" encoding and should not be referred to as only "base64". Unless clarified otherwise,
 * "base64" refers to the base 64 in the previous section. ????"base64url"??????????
 * "base64"??????????????????? ?????????????????"base64"? ?????? This encoding is
 * technically identical to the previous one, except for the 62:nd and 63:rd alphabet character, as indicated in
 * Table 2. ?????????? ???????????????????? Table 2: The "URL and Filename safe" Base
 * 64 Alphabet ?????? Value Encoding Value Encoding Value Encoding Value Encoding 0 A 17 R
 * 34 i 51 z 1 B 18 S 35 j 52 0 2 C 19 T 36 k 53 1 3 D 20 U 37 l 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6 G 23 X
 * 40 o 57 5 7 H 24 Y 41 p 58 6 8 I 25 Z 42 q 59 7 9 J 26 a 43 r 60 8 10 K 27 b 44 s 61 9 11 L 28 c 45 t 62 -
 * (minus) 12 M 29 d 46 u 63 _ 13 N 30 e 47 v (underline) 14 O 31 f 48 w 15 P 32 g 49 x 16 Q 33 h 50 y (pad) =
 * @param in ???byte
 * @return ?????
 */
public static String encodeBase64Url(final byte[] in) {
    return Base64.encodeBase64URLSafeString(in);
}

From source file:com.fujitsu.dc.common.utils.DcCoreUtils.java

/**
 * Base64url??.//from   ww  w  . j  ava2 s . c  o m
 * @param inStr 
 * @return 
 */
public static String encodeBase64Url(final InputStream inStr) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int l = 0;
    try {
        while ((l = inStr.read()) != -1) {
            baos.write(l);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return Base64.encodeBase64URLSafeString(baos.toByteArray());
}

From source file:com.datatorrent.stram.webapp.StramWebServices.java

private static String getTupleRecordingId() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
    String val = sdf.format(new Date());
    val += "-";
    byte[] r = new byte[4];
    new Random().nextBytes(r);
    val += Base64.encodeBase64URLSafeString(r);
    return val;
}