🛠️ToolsShed

HTTP Basic Auth Encoder

Encode and decode HTTP Basic Authentication credentials. Generate the Authorization header value from username and password.

Pertanyaan yang Sering Diajukan

Implementasi Kode

import base64

def encode_basic_auth(username: str, password: str) -> str:
    credentials = f"{username}:{password}"
    encoded = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
    return f"Basic {encoded}"

def decode_basic_auth(header_value: str) -> tuple[str, str]:
    b64 = header_value.removeprefix("Basic ").strip()
    decoded = base64.b64decode(b64).decode("utf-8")
    username, _, password = decoded.partition(":")
    return username, password

header = encode_basic_auth("admin", "secret")
print(header)  # Basic YWRtaW46c2VjcmV0

user, pwd = decode_basic_auth(header)
print(f"Username: {user}, Password: {pwd}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.