Files
tuiche/lib/common/util/base64Tool.dart
2026-01-31 14:44:55 +08:00

25 lines
632 B
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
class Base64Tool {
/// 编码为 URL 安全的 Base64网络请求用这个就够了
static String encode(String text) {
final base64Str = base64.encode(utf8.encode(text));
return base64Str
.replaceAll('+', '-')
.replaceAll('/', '_')
.replaceAll('=', '');
}
/// 解码 URL 安全的 Base64
static String decode(String urlSafeBase64) {
var base64Str = urlSafeBase64.replaceAll('-', '+').replaceAll('_', '/');
// 补充等号
while (base64Str.length % 4 != 0) {
base64Str += '=';
}
return utf8.decode(base64.decode(base64Str));
}
}