Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:23
Show Gist options
  • Select an option

  • Save dacr/b1057c9c201b29f5399384f40ce85520 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/b1057c9c201b29f5399384f40ce85520 to your computer and use it in GitHub Desktop.
small scala function to generate a basic authentication Authentication token / published by https://github.com/dacr/code-examples-manager #6d5cecb8-9159-40a5-9214-7bb752455e99/5c28112d7fb20d56baa227acff0c65336bfa5164
// summary : small scala function to generate a basic authentication Authentication token
// keywords : scala, token, credential, basic-auth, authentication, base64, encode, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 6d5cecb8-9159-40a5-9214-7bb752455e99
// created-on : 2020-11-10T06:22:34Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
// ---------------------
def basicAuthToken(username: String, password: String, charsetName:String="UTF-8"): String = {
val tokenBytes = s"$username:$password".getBytes(charsetName)
val tokenB64 = java.util.Base64.getEncoder.encodeToString(tokenBytes)
s"Basic $tokenB64"
}
val token = basicAuthToken("root","root")
assert(token == "Basic cm9vdDpyb290")
println(s"Authorization: $token")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment