Java XML Base64 Encode Decode base64EncodeBasicCredentials(String username, String password)

Here you can find the source of base64EncodeBasicCredentials(String username, String password)

Description

Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy authorization.

License

Apache License

Parameter

Parameter Description
username username to encode
password password to encode

Return

a base-64 encoded string containing username:password

Declaration

public static String base64EncodeBasicCredentials(String username, String password) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.xml.bind.DatatypeConverter;

import java.nio.charset.StandardCharsets;

public class Main {
    /**//from ww  w  .j  ava 2 s .c  om
     * Base64-encodes the specified username and password for Basic Authorization for HTTP requests or upstream proxy
     * authorization. The format of Basic auth is "username:password" as a base64 string.
     *
     * @param username username to encode
     * @param password password to encode
     * @return a base-64 encoded string containing <code>username:password</code>
     */
    public static String base64EncodeBasicCredentials(String username, String password) {
        String credentialsToEncode = username + ':' + password;
        // using UTF-8, which is the modern de facto standard, and which retains compatibility with US_ASCII for ASCII characters,
        // as required by RFC 7616, section 3: http://tools.ietf.org/html/rfc7617#section-3
        byte[] credentialsAsUtf8Bytes = credentialsToEncode.getBytes(StandardCharsets.UTF_8);
        return DatatypeConverter.printBase64Binary(credentialsAsUtf8Bytes);
    }
}

Related

  1. base64Encode(byte[] bytes)
  2. base64Encode(byte[] bytes)
  3. base64Encode(byte[] bytes)
  4. base64encode(byte[] bytes, boolean pad)
  5. base64Encode(String data)
  6. base64FromBinary(byte[] value)
  7. base64FromString(String value)
  8. base64ToByte(String data)
  9. byteArrayToBase64String(byte[] data)