Example usage for java.util Base64 getDecoder

List of usage examples for java.util Base64 getDecoder

Introduction

In this page you can find the example usage for java.util Base64 getDecoder.

Prototype

public static Decoder getDecoder() 

Source Link

Document

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

Usage

From source file:org.wso2.msf4j.security.basic.AbstractBasicAuthSecurityInterceptor.java

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo)
        throws Exception {
    String authHeader = request.getHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION);
    if (authHeader != null) {
        String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
        String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
        if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
            byte[] decodedByte = authEncoded.getBytes(Charset.forName(CHARSET_UTF_8));
            String authDecoded = new String(Base64.getDecoder().decode(decodedByte),
                    Charset.forName(CHARSET_UTF_8));
            String[] authParts = authDecoded.split(":");
            String username = authParts[0];
            String password = authParts[1];
            if (authenticate(username, password)) {
                return true;
            }/* w w w.j a v  a2 s.  c  om*/
        }

    }
    responder.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
    responder.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
    responder.send();
    return false;
}

From source file:de.sainth.recipe.backend.security.AuthFilter.java

AuthFilter(RecipeManagerProperties properties, UserRepository userRepository) {
    this.userRepository = userRepository;
    byte[] decodedKey = Base64.getDecoder().decode(properties.getEncodedKey());
    key = new SecretKeySpec(decodedKey, 0, decodedKey.length, SignatureAlgorithm.HS256.getValue());
}

From source file:org.apache.accumulo.test.util.SerializationUtil.java

public static Object deserializeBase64(String str) {
    byte[] b = Base64.getDecoder().decode(str);
    return deserialize(b);
}

From source file:bit.changepurse.wdk.bip.TestGithubUpdates.java

private String toString(JSONObject file) {
    String header = file.toString();
    HTTPResponse response = doGet(file.getString("url"));
    JSONObject json = new JSONObject(response.getBody());
    String body = json.getString("content");
    if ("base64".equals(json.getString("encoding"))) {
        body = new String(Base64.getDecoder().decode(body.replace("\n", "")));
    }/* w  w  w .j a v  a  2  s  .c  om*/
    return header + "\n" + body;
}

From source file:edu.sjsu.cohort6.openstack.server.filter.BasicAuthenticationFilter.java

private String decodeHeader(final String encodedHeader) {
    return new String(Base64.getDecoder().decode(encodedHeader));
}

From source file:com.bekwam.resignator.util.CryptUtils.java

public String decrypt(String encrypted, String passPhrase)
        throws IOException, PGPException, NoSuchProviderException {

    if (StringUtils.isBlank(encrypted)) {
        throw new IllegalArgumentException("encrypted text is blank");
    }//from   w  ww .ja  va  2 s .c  o  m

    if (StringUtils.isBlank(passPhrase)) {
        throw new IllegalArgumentException("passPhrase is required");
    }

    byte[] ciphertext;
    try {
        ciphertext = Base64.getDecoder().decode(encrypted.getBytes(StandardCharsets.ISO_8859_1));
    } catch (IllegalArgumentException exc) {
        if (logger.isWarnEnabled()) {
            if (logger.isWarnEnabled()) {
                logger.warn(
                        "Field could not be decoded. (Config file modified outside of app?)  Returning input bytes as encrypted bytes.");
            }
        }
        return encrypted;
    }

    byte[] cleartext = decrypt(ciphertext, passPhrase.toCharArray());
    return new String(cleartext, StandardCharsets.ISO_8859_1);
}

From source file:org.wso2.carbon.mss.example.hook.AbstractBasicAuthInterceptor.java

@Override
public boolean preCall(HttpRequest request, HttpResponder responder, ServiceMethodInfo serviceMethodInfo) {
    HttpHeaders headers = request.headers();
    if (headers != null) {
        String authHeader = headers.get(HttpHeaders.Names.AUTHORIZATION);
        if (authHeader != null) {
            String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
            String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
            if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
                byte[] decodedByte = authEncoded.getBytes(Charset.forName("UTF-8"));
                String authDecoded = new String(Base64.getDecoder().decode(decodedByte),
                        Charset.forName("UTF-8"));
                String[] authParts = authDecoded.split(":");
                String username = authParts[0];
                String password = authParts[1];
                if (authenticate(username, password)) {
                    return true;
                }/*  w ww  .j  ava  2  s .  c o m*/
            }

        }
    }
    Multimap<String, String> map = ArrayListMultimap.create();
    map.put(HttpHeaders.Names.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
    responder.sendStatus(HttpResponseStatus.UNAUTHORIZED, map);
    return false;
}

From source file:fi.vm.kapa.identification.shibboleth.extauthn.util.CertificateUtil.java

public static boolean checkSignature(String data, String signature, X509Certificate cert) {
    boolean result = false;
    try {//from  ww  w. j a va 2s .  c  o m
        logger.debug("checkSignature: data={}, signature={}, cert={}", data, signature, cert.toString());
        byte[] sigToVerify = Base64.getDecoder().decode(signature);
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initVerify(cert);
        sig.update(Base64.getDecoder().decode(data));
        result = sig.verify(sigToVerify);
    } catch (Exception e) {
        logger.warn("checkSignature: Got exception " + e.getClass(), e);
    }
    return result;
}

From source file:majordodo.worker.TaskModeAwareExecutorFactory.java

@Override
public TaskExecutor createTaskExecutor(String taskType, Map<String, Object> parameters) {
    String mode = (String) parameters.getOrDefault("mode", Task.MODE_DEFAULT);
    if (mode.equals(Task.MODE_EXECUTE_FACTORY)) {
        return inner.createTaskExecutor(taskType, parameters);
    }//from  ww  w .j a v  a 2 s.c o m
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        String parameter = (String) parameters.getOrDefault("parameter", "");
        if (parameter.startsWith("base64:")) {
            parameter = parameter.substring("base64:".length());
            byte[] serializedObjectData = Base64.getDecoder().decode(parameter);
            ByteArrayInputStream ii = new ByteArrayInputStream(serializedObjectData);
            ObjectInputStream is = new ObjectInputStream(ii) {
                @Override
                public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                    try {
                        return tccl.loadClass(desc.getName());
                    } catch (Exception e) {
                    }
                    return super.resolveClass(desc);
                }

            };
            TaskExecutor res = (TaskExecutor) is.readUnshared();
            return res;
        } else if (parameter.startsWith("newinstance:")) {
            parameter = parameter.substring("newinstance:".length());
            URLClassLoader cc = (URLClassLoader) tccl;
            Class clazz = Class.forName(parameter, true, tccl);
            TaskExecutor res = (TaskExecutor) clazz.newInstance();
            return res;
        } else {
            throw new RuntimeException("bad parameter: " + parameter);
        }
    } catch (Exception err) {
        return new TaskExecutor() {
            @Override
            public String executeTask(Map<String, Object> parameters) throws Exception {
                throw err;
            }

        };
    }

}

From source file:org.apache.hadoop.hive.metastore.messaging.json.gzip.DeSerializer.java

private static String deCompress(String messageBody) {
    byte[] decodedBytes = Base64.getDecoder().decode(messageBody.getBytes(StandardCharsets.UTF_8));
    try (ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
            GZIPInputStream is = new GZIPInputStream(in)) {
        byte[] bytes = IOUtils.toByteArray(is);
        return new String(bytes, StandardCharsets.UTF_8);
    } catch (IOException e) {
        LOG.error("cannot decode the stream", e);
        LOG.debug("base64 encoded String", messageBody);
        throw new RuntimeException("cannot decode the stream ", e);
    }/* w  w w  .  j a v a2 s.  c o  m*/
}