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

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

Introduction

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

Prototype

public final byte[] decode(CharSequence chars) 

Source Link

Document

Decodes the specified character sequence, and returns the resulting byte[] .

Usage

From source file:com.spotify.crtauth.utils.TraditionalKeyParser.java

public static RSAPrivateKeySpec parsePemPrivateKey(String pemPrivateKey) throws InvalidKeyException {
    pemPrivateKey = pemPrivateKey.replace("\n", "");
    Matcher matcher = PRIVATE_KEY_PATTERN.matcher(pemPrivateKey);
    if (!matcher.matches()) {
        throw new InvalidKeyException();
    }//w  ww  . ja v  a  2s. c  o  m
    String pemKey = matcher.group(1);
    BaseEncoding encoding = BaseEncoding.base64();
    byte[] derKey = encoding.decode(pemKey);
    List<byte[]> fields;
    try {
        fields = parsePrivateKeyASN1(ByteBuffer.wrap(derKey));
    } catch (InvalidInputException e) {
        throw new InvalidKeyException(e);
    }
    BigInteger mod = new BigInteger(fields.get(1));
    BigInteger exp = new BigInteger(fields.get(3));
    return new RSAPrivateKeySpec(mod, exp);
}

From source file:com.eucalyptus.tokens.oidc.JsonWebSignatureVerifier.java

private static Pair<JoseHeader, byte[]> decode(@Nonnull final String jsonHeaderB64,
        @Nonnull final String jsonBodyB64, @Nonnull final String signatureB64)
        throws GeneralSecurityException, OidcParseException {
    final String jsonHeader;
    final byte[] signature;
    try {/* w w w.j  a va  2  s  . com*/
        final BaseEncoding b64Url = BaseEncoding.base64Url(); // allow padding?
        jsonHeader = new String(b64Url.decode(jsonHeaderB64), StandardCharsets.UTF_8);
        b64Url.decode(jsonBodyB64); // ensures valid b64url encoding
        signature = b64Url.decode(signatureB64);
    } catch (final IllegalArgumentException e) {
        throw new GeneralSecurityException("Unable to decode", e);
    }
    final JoseHeader header = JoseHeader.parse(jsonHeader);
    if (header.getCrit().map(List::size).getOrElse(0) > 0) {
        throw new GeneralSecurityException("Unsupported critical extension " + header.getCrit());
    }
    return Pair.pair(header, signature);
}

From source file:com.spotify.sshagentproxy.TraditionalKeyParser.java

public static RSAPrivateKeySpec parsePemPrivateKey(String pemPrivateKey) throws InvalidKeyException {
    pemPrivateKey = pemPrivateKey.replace("\n", "");
    Matcher matcher = PRIVATE_KEY_PATTERN.matcher(pemPrivateKey);
    if (!matcher.matches()) {
        throw new InvalidKeyException();
    }/* www  .ja  va 2s. c o m*/
    String pemKey = matcher.group(1);
    BaseEncoding encoding = BaseEncoding.base64();
    byte[] derKey = encoding.decode(pemKey);
    List<byte[]> fields;
    try {
        fields = parsePrivateKeyASN1(ByteBuffer.wrap(derKey));
    } catch (IllegalArgumentException e) {
        throw new InvalidKeyException(e);
    }
    BigInteger mod = new BigInteger(fields.get(1));
    BigInteger exp = new BigInteger(fields.get(3));
    return new RSAPrivateKeySpec(mod, exp);
}

From source file:com.spotify.sshagentproxy.TraditionalKeyParser.java

public static RSAPublicKeySpec parsePemPublicKey(String pemPublicKey) throws InvalidKeyException {
    Matcher matcher = PUBLIC_KEY_PATTERN.matcher(pemPublicKey);
    if (!matcher.matches()) {
        throw new InvalidKeyException();
    }//from w ww  . ja  v a  2  s . c  o m
    String pemKey = matcher.group(1);
    BaseEncoding encoding = BaseEncoding.base64();
    byte[] derKey = encoding.decode(pemKey);
    ByteBuffer byteBuffer = ByteBuffer.wrap(derKey);
    byteBuffer.order(ByteOrder.BIG_ENDIAN);
    byte[] typeBytes = readVariableLengthOpaque(byteBuffer);
    byte[] expBytes = readVariableLengthOpaque(byteBuffer);
    byte[] modBytes = readVariableLengthOpaque(byteBuffer);
    if (typeBytes == null || expBytes == null || modBytes == null) {
        throw new InvalidKeyException();
    }
    String type = new String(typeBytes, Charsets.US_ASCII);
    if (!type.equals(PUBLIC_KEY_TYPE)) {
        throw new InvalidKeyException();
    }
    BigInteger exp = new BigInteger(expBytes);
    BigInteger mod = new BigInteger(modBytes);
    return new RSAPublicKeySpec(mod, exp);
}

From source file:com.facebook.presto.kinesis.decoder.json.Base64ZLibJsonKinesisRowDecoder.java

/**
 * Decodes and decompressed a string of information that's encoded via base 64 and compressed via zlib
 *///from   www .  ja v a 2s  .  c o m
public static byte[] decodeAndUncompress(byte[] data) throws IOException {
    String stringData = new String(data, "UTF-8");
    BaseEncoding decoder = BaseEncoding.base64().withSeparator("\n", 1);
    ByteArrayInputStream compressed = new ByteArrayInputStream(decoder.decode(stringData));
    InflaterInputStream decompressed = new InflaterInputStream(compressed);

    return ByteStreams.toByteArray(decompressed);
}

From source file:org.apache.gobblin.util.SerializationUtils.java

/**
 * Deserialize a String obtained via {@link #serialize(Serializable)} into an object, using the
 * given {@link BaseEncoding}, which must be the same {@link BaseEncoding} used to serialize the object.
 *
 * @param serialized The serialized String
 * @param clazz The class the deserialized object should be cast to.
 * @param enc The {@link BaseEncoding} used to decode the String.
 * @return The deserialized object/*from ww  w .  j a  v a  2  s.c o  m*/
 * @throws IOException if it fails to deserialize the object
 */
public static <T extends Serializable> T deserialize(String serialized, Class<T> clazz, BaseEncoding enc)
        throws IOException {
    return deserializeFromBytes(enc.decode(serialized), clazz);
}

From source file:org.tc33.jenigma.test.CookieEncrypt.java

public static String customDencrypt(String encrypted, String password) {

    if (password.length() != 8) {
        return null;
    }/*from w ww. ja  v  a 2 s. co  m*/
    BaseEncoding b64 = BaseEncoding.base64Url().omitPadding();
    byte[] encryptedBytes = b64.decode(encrypted);

    //System.out.println(Hex.toHexString(encryptedBytes));

    int encryptedLength = encryptedBytes.length;

    int blockSize = 4;
    if (encryptedLength % blockSize != 0) {
        return null;
    }

    int salt = 385751695;
    byte[] key1 = new byte[4];
    System.arraycopy(password.getBytes(), 0, key1, 0, 4);
    key1 = int2byte(byte2int(key1) ^ salt++);
    byte[] key2 = new byte[4];
    System.arraycopy(password.getBytes(), 4, key2, 0, 4);
    key2 = int2byte(byte2int(key2) ^ salt++);

    key1[0] = (byte) (key1[0] ^ key2[0] ^ key2[1]);
    key1[1] = (byte) (key1[1] ^ key2[0] ^ key2[2]);
    key1[2] = (byte) (key1[2] ^ key2[2] ^ key2[3]);
    key1[3] = (byte) (key1[3] ^ key2[1] ^ key2[3]);

    //System.out.println(Hex.toHexString(key1));
    //System.out.println(Hex.toHexString(key2));

    int k = 0;
    byte[] output = new byte[encryptedLength];
    while (k < encryptedLength / 4) {
        if (k % 2 == 0) {
            byte[] tmp1 = new byte[4];
            for (int j = 0; j < 4; j++) {
                tmp1[j] = (byte) (key1[j] ^ encryptedBytes[4 * k + j]);
            }
            System.arraycopy(tmp1, 0, output, 4 * k, 4);
        } else {
            byte[] tmp2 = new byte[4];
            for (int j = 0; j < 4; j++) {
                tmp2[j] = (byte) (key1[j] ^ key2[j] ^ encryptedBytes[4 * k + j]);
            }
            System.arraycopy(tmp2, 0, output, 4 * k, 4);
        }
        k++;
    }

    for (int i = 0; i < encryptedLength; i++) {
        output[i] = bswap(output[i]);

        if (i % 2 == 0) {
            output[i] ^= i;
        } else {
            output[i] ^= (encryptedLength - i);
        }

    }

    return new String(output);

}

From source file:org.jooby.internal.ssl.PemReader.java

static List<ByteBuffer> readCertificates(final File file) throws CertificateException, IOException {
    String content = Files.toString(file, StandardCharsets.US_ASCII);

    BaseEncoding base64 = base64();
    List<ByteBuffer> certs = new ArrayList<ByteBuffer>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    while (m.find(start)) {
        ByteBuffer buffer = ByteBuffer.wrap(base64.decode(m.group(1)));
        certs.add(buffer);//  ww  w  .java 2s  . c  om

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates: " + file);
    }

    return certs;
}

From source file:org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.SimpleBinaryAttributeReadingStrategy.java

@Override
protected Object postprocessParsedValue(String textContent) {
    BaseEncoding en = BaseEncoding.base64();
    byte[] decode = en.decode(textContent);
    List<String> parsed = Lists.newArrayListWithCapacity(decode.length);
    for (byte b : decode) {
        parsed.add(Byte.toString(b));
    }//w  w  w .  j av  a  2 s.c  o  m
    return parsed;
}

From source file:com.google.cloud.storage.spi.DefaultStorageRpc.java

private static void setEncryptionHeaders(HttpHeaders headers, String headerPrefix, Map<Option, ?> options) {
    String key = CUSTOMER_SUPPLIED_KEY.getString(options);
    if (key != null) {
        BaseEncoding base64 = BaseEncoding.base64();
        HashFunction hashFunction = Hashing.sha256();
        headers.set(headerPrefix + "algorithm", "AES256");
        headers.set(headerPrefix + "key", key);
        headers.set(headerPrefix + "key-sha256",
                base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
    }//w w  w  .  j av a  2s.  co m
}