Last active
June 27, 2024 04:03
-
-
Save brandonmwest/a2632d0a65088a20c00a to your computer and use it in GitHub Desktop.
Generating base64-encoded Authorization headers in a variety of languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpClient.DefaultRequestHeaders.Authorization = | |
new AuthenticationHeaderValue( | |
"Basic", | |
Convert.ToBase64String( | |
System.Text.ASCIIEncoding.ASCII.GetBytes( | |
string.Format("{0}:{1}", username, password)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$header = "Authorization: Basic " . base64_encode($username . ':' . $password); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') | |
header = ("Authorization: Basic %s" % base64string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$header = 'Authorization: Basic ' + Base64.encode64( username + ':' + password ).chomp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax | |
({ | |
type: "GET", | |
url: "https://www.example.com", | |
dataType: 'json', | |
headers: { | |
"Authorization", btoa(username + ":" + password) | |
}, | |
data: '{}', | |
success: function (){ | |
... | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var username = 'Test'; | |
var password = '123'; | |
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); | |
var header = {'Host': 'www.example.com', 'Authorization': auth}; | |
var request = client.request('GET', '/', header); |
PowerShell
$username = 'test' $password = 'password' $base64AuthInfo = [convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1} -f $username, $password"))) $base64AuthInfo
The code above doesn't work. Below is the fix.
$username = 'test'
$password = 'password'
$base64AuthInfo = [convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${username}:${password}")))
$base64AuthInfo
Scala:
val bytesFromString = (username ++ ":" ++ password).getBytes("UTF-8") val urlAuth = "Basic " ++ Base64.encode(bytesFromString)
The right scala implementation which run directly :
val tokenBytes = s"$username:$password".getBytes("UTF-8")
val tokenB64 = new String(java.util.Base64.getEncoder.encode(tokenBytes))
val token = s"Basic $tokenB64"
in JQuery, aren't we missing the 'Basic ' prefix?
in Rust;
use http_auth_basic::Credentials;
let credentials = Credentials::new("username", "password").as_http_header();
assert_eq!(String::from("Basic dXNlcm5hbWU6cGFzc3dvcmQ="), credentials);
In Elixir:
username = "username"
password = "password"
"#{username}:#{password}"
|> Base.encode64()
In kotlin:
val username = "username"
val password = "password
val userpass = username + ":" + password
val encodedAuth = encodeToString(userpass.toByteArray(), NO_WRAP)
val authHeaderValue = "Basic " + encodedAuth
httpURLConnection.setRequestProperty("Authorization", authHeaderValue)
In Salesforce APEX
HttpRequest request = new HttpRequest();
String username = 'test';
String password = 'test';
String accessToken = EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
request.setHeader('Authorization', 'Basic ' + accessToken);
In oracle pl/sql
set serveroutput on;
DECLARE
v_str VARCHAR2(1000);
BEGIN
--Create encoded value
v_str := utl_encode.text_encode('Testuser:verySecret','WE8ISO8859P1', UTL_ENCODE.BASE64);
dbms_output.put_line(v_str);
--Decode the value..
v_str := utl_encode.text_decode(v_str,'WE8ISO8859P1', UTL_ENCODE.BASE64);
dbms_output.put_line(v_str);
END;
/
or
select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('Testuser:verySecret')))
from dual;
/
in JQuery, aren't we missing the 'Basic ' prefix?
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 7 correction for jquery snippet:
"Authorization", "Basic " + btoa(username + ":" + password)