[API Proposal]: String-to-String Base64 Encode/Decode

Đang mở
#136,711 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
api

Hướng nghiên cứu

Start by reviewing the proposed APIs for System.Convert, System.Buffers.Text.Base64, and Base64Url, along with the existing Base64 APIs they extend. The issue does not name files or tests; the first step is to determine whether the proposed signatures and behavior fit the project’s API conventions. Done means the proposal has a clear design decision, including the Encoding parameters.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

api-suggestion area-System.Runtime untriaged

Background and Motivation

Today, .NET's Base64 APIs have a gap: there is no direct way to encode a string to a Base64 string, or decode a Base64 string back to a string. Every existing method either accepts byte[] / ReadOnlySpan<byte> as input (encode side) or returns byte[] (decode side).

This pattern is extremely common across the .NET ecosystem. It appears in CI/CD tooling, configuration loaders, certificate handling, token encoding, and countless other packages. In many cases the Base64-encoded values are stored as strings in configuration files, databases, or API responses and need to be decoded back to strings when consumed later.

var certPem = Encoding.UTF8.GetString(Convert.FromBase64String(configuration.ClientCertificateData));

var releaseBuildBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(releaseBuild));

This is verbose, does not have easy discoverability and allocates an intermediate byte[] that could be avoided internally.

Proposed API

All methods accept ReadOnlySpan<char> rather than string — callers can pass a string directly thanks to the implicit conversion.

namespace System;

public static partial class Convert
{
    public static string ToBase64String(ReadOnlySpan<char> source, Encoding encoding);

    public static string ToBase64String(ReadOnlySpan<char> source, Encoding encoding, Base64FormattingOptions options);
}

namespace System.Buffers.Text;

public static partial class Base64
{
    public static string EncodeToString(ReadOnlySpan<char> source, Encoding encoding);

    public static string DecodeToString(ReadOnlySpan<char> source, Encoding encoding);
}

public static partial class Base64Url
{
    public static string EncodeToString(ReadOnlySpan<char> source, Encoding encoding);

    public static string DecodeToString(ReadOnlySpan<char> source, Encoding encoding);
}

API Usage

// Encode a connection string for safe storage in a config file
string encoded = Convert.ToBase64String("Server=prod;Database=App;User=admin;", Encoding.UTF8);

// Decode it back when reading the config
string connectionString = Convert.FromBase64ToString(encoded, Encoding.UTF8);

Edit: Added Encoding parameters

Ngôn ngữ chính
C#
Star
18.4k
Fork
5.8k
Merge trung bình
2 ngày 12 giờ
Pull request đã merge (30 ngày)
581

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/runtime

Tất cả issue của dotnet/runtime

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.