25 lines
632 B
Dart
25 lines
632 B
Dart
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));
|
||
}
|
||
}
|