Example usage for com.google.common.io BaseEncoding base64Url

List of usage examples for com.google.common.io BaseEncoding base64Url

Introduction

In this page you can find the example usage for com.google.common.io BaseEncoding base64Url.

Prototype

public static BaseEncoding base64Url() 

Source Link

Document

The "base64url" encoding specified by <a href="http://tools.ietf.org/html/rfc4648#section-5">RFC 4648 section 5</a>, Base 64 Encoding with URL and Filename Safe Alphabet, also sometimes referred to as the "web safe Base64."

Usage

From source file:com.google.dosidos.util.EncryptionUtil.java

public static String encrypt(String plainText, String key) {
    try {/*from   w  w  w .j  a  v a 2  s . c  o  m*/
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, getAesKey(key));
        byte[] salt = new byte[8];
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.nextBytes(salt);
        cipher.update(salt);
        byte[] encrypted = cipher.doFinal(plainText.getBytes("ISO-8859-1"));
        return BaseEncoding.base64Url().encode(encrypted);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.template.soy.msgs.SoyMsgIdConverter.java

/**
 * Converts received message ID strings, as rendered by the Soy <code>msgId</code> function, into
 * a list of long message IDs./*w w w  .  j av a  2s . c  o  m*/
 *
 * <p>The message IDs are generated by Soy as Base64 web safe ("base64url")-encoded int64s.
 *
 * <p>For example, "URTo5tnzILU=" = [0x51 0x14 0xe8 0xe6 0xd9 0xf3 0x20 0xb5] = 0x5114e8e6d9f320b5
 * = 5842550694803087541.
 *
 * @throws IllegalArgumentException if any of the strings fails to decode into message IDs, is too
 *     long or too short.
 */
public static ImmutableList<Long> convertSoyMessageIdStrings(Iterable<String> encodedIds) {
    ImmutableList.Builder<Long> messageIds = ImmutableList.builder();
    for (String encodedId : encodedIds) {
        byte[] decodedBytes = BaseEncoding.base64Url().decode(encodedId);
        if (decodedBytes.length != Long.BYTES) {
            throw new IllegalArgumentException(
                    String.format("The message ID to decode ('%s') was of invalid size (%d != %d)", encodedId,
                            decodedBytes.length, Long.BYTES));
        }
        messageIds.add(Longs.fromByteArray(decodedBytes));
    }
    return messageIds.build();
}

From source file:me.j360.dubbo.modules.util.text.EncodeUtil.java

/**
 * Base64?, URL(Base64URL?'+''/''-''_', ?RFC3548).
 * //  ww  w.ja v a2 s .  c om
 * ??IllegalArgumentException
 */
public static byte[] decodeBase64UrlSafe(CharSequence input) {
    return BaseEncoding.base64Url().decode(input);
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_X.java

protected static <T> T base64UrlDecodeObject(String encoded, Class<T> type) {
    if (encoded == null) {
        return null;
    } else {/* ww w  .ja va2s.  co  m*/
        T deserialized = null;
        try {
            byte[] decoded = BaseEncoding.base64Url().decode(encoded);
            ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
            ObjectInputStream ois = new ObjectInputStream(bais);
            deserialized = type.cast(ois.readObject());
            ois.close();
            bais.close();
        } catch (Exception ex) {
            logger.error("Unable to decode object", ex);
        }
        return deserialized;
    }
}

From source file:org.opendedup.sdfs.filestore.cloud.utils.EncyptUtils.java

public static String decString(String fname, boolean enc) throws IOException {
    if (baseEncode)
        return fname;
    if (enc) {/*from w  w  w.  j  a  v a 2 s .  co m*/
        byte[] encH;
        if (baseEncode)
            encH = BaseEncoding.base64().decode(fname);
        else
            encH = BaseEncoding.base64Url().decode(fname);
        String st = new String(EncryptUtils.decryptCBC(encH));
        return st;
    } else {

        byte[] encH;
        encH = BaseEncoding.base64Url().decode(fname);
        return new String(encH);
    }
}

From source file:com.lithium.flow.util.BaseEncodings.java

@Nonnull
private static BaseEncoding getEncoding(@Nonnull String name) {
    switch (name) {
    case "base16":
        return BaseEncoding.base16();
    case "base32":
        return BaseEncoding.base32();
    case "base32Hex":
        return BaseEncoding.base32Hex();
    case "base64":
        return BaseEncoding.base64();
    case "base64Url":
        return BaseEncoding.base64Url();
    default://from  w w  w  .  j ava  2  s  . co  m
        throw new RuntimeException("unknown encoding: " + name);
    }
}

From source file:io.warp10.script.functions.TOB64URL.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object o = stack.pop();/*from   ww  w.  j  ava  2  s. c o m*/

    if (o instanceof String) {
        stack.push(BaseEncoding.base64Url().encode(o.toString().getBytes(Charsets.UTF_8)));
    } else if (o instanceof byte[]) {
        stack.push(BaseEncoding.base64Url().encode((byte[]) o));
    } else {
        throw new WarpScriptException(getName() + " operates on a String or a byte array.");
    }

    return stack;
}

From source file:io.warp10.script.functions.B64URLTO.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object o = stack.pop();//from   w w w .j a  v a  2s  . c om

    if (!(o instanceof String)) {
        throw new WarpScriptException(getName() + " operates on a String.");
    }

    stack.push(BaseEncoding.base64Url().decode(o.toString()));

    return stack;
}

From source file:io.macgyver.core.auth.ApiToken.java

public String getArmoredString() {
    return BaseEncoding.base64Url().encode(data.toString().getBytes());
}

From source file:com.google.util.EncryptionUtil.java

public static String decrypt(String encryptedText) {
    try {//from  www .  ja  va2 s .co  m
        byte[] encrypted = BaseEncoding.base64Url().decode(encryptedText);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, getAesKey());
        byte[] plain = cipher.doFinal(encrypted);
        if (plain == null || plain.length <= 8) {
            throw new RuntimeException("wrong encrypted text.");
        }
        byte[] data = new byte[plain.length - 8];
        System.arraycopy(plain, 8, data, 0, data.length);
        return new String(data, "ISO-8859-1");
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}