2023-05-29 15:24:57 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
String base64UrlNoPadEncode(List<int> bytes) {
|
|
|
|
var x = base64Url.encode(bytes);
|
|
|
|
while (x.endsWith('=')) {
|
|
|
|
x = x.substring(0, x.length - 1);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint8List base64UrlNoPadDecode(String source) {
|
|
|
|
source = base64.normalize(source);
|
|
|
|
return base64.decode(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint8List base64UrlNoPadDecodeDynamic(dynamic source) {
|
|
|
|
source = source as String;
|
|
|
|
source = base64.normalize(source);
|
|
|
|
return base64.decode(source);
|
|
|
|
}
|
|
|
|
|
2023-07-05 23:53:08 -04:00
|
|
|
class Uint8ListJsonConverter implements JsonConverter<Uint8List, String> {
|
|
|
|
const Uint8ListJsonConverter();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Uint8List fromJson(String json) => base64UrlNoPadDecode(json);
|
|
|
|
@override
|
|
|
|
String toJson(Uint8List data) => base64UrlNoPadEncode(data);
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
@immutable
|
|
|
|
abstract class EncodedString extends Equatable {
|
|
|
|
final String contents;
|
|
|
|
@override
|
|
|
|
List<Object> get props => [contents];
|
2023-05-29 15:24:57 -04:00
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
const EncodedString(String s) : contents = s;
|
2023-05-29 15:24:57 -04:00
|
|
|
|
|
|
|
Uint8List decode() {
|
|
|
|
return base64UrlNoPadDecode(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-05 18:48:06 -04:00
|
|
|
String toString() => contents;
|
2023-05-29 15:24:57 -04:00
|
|
|
|
2023-07-12 21:28:00 -04:00
|
|
|
static T fromBytes<T extends EncodedString>(Uint8List bytes) {
|
|
|
|
switch (T) {
|
|
|
|
case FixedEncodedString32:
|
|
|
|
return FixedEncodedString32.fromBytes(bytes) as T;
|
|
|
|
case FixedEncodedString43:
|
|
|
|
return FixedEncodedString43.fromBytes(bytes) as T;
|
|
|
|
case FixedEncodedString86:
|
|
|
|
return FixedEncodedString86.fromBytes(bytes) as T;
|
|
|
|
default:
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
static T fromString<T extends EncodedString>(String s) {
|
|
|
|
switch (T) {
|
|
|
|
case FixedEncodedString32:
|
2023-07-05 18:48:06 -04:00
|
|
|
return FixedEncodedString32.fromString(s) as T;
|
2023-05-29 15:24:57 -04:00
|
|
|
case FixedEncodedString43:
|
2023-07-05 18:48:06 -04:00
|
|
|
return FixedEncodedString43.fromString(s) as T;
|
2023-05-29 15:24:57 -04:00
|
|
|
case FixedEncodedString86:
|
2023-07-05 18:48:06 -04:00
|
|
|
return FixedEncodedString86.fromString(s) as T;
|
2023-05-29 15:24:57 -04:00
|
|
|
default:
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
@immutable
|
2023-05-29 15:24:57 -04:00
|
|
|
class FixedEncodedString32 extends EncodedString {
|
2023-07-05 18:48:06 -04:00
|
|
|
const FixedEncodedString32._(String s) : super(s);
|
|
|
|
static int encodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
static int decodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 24;
|
|
|
|
}
|
|
|
|
|
2023-07-12 21:28:00 -04:00
|
|
|
factory FixedEncodedString32.fromBytes(Uint8List bytes) {
|
|
|
|
if (bytes.length != decodedLength()) {
|
|
|
|
throw Exception("length ${bytes.length} should be ${decodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString32._(base64UrlNoPadEncode(bytes));
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
factory FixedEncodedString32.fromString(String s) {
|
|
|
|
var d = base64UrlNoPadDecode(s);
|
|
|
|
if (d.length != decodedLength()) {
|
|
|
|
throw Exception("length ${s.length} should be ${encodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString32._(s);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
String toJson() => toString();
|
|
|
|
factory FixedEncodedString32.fromJson(dynamic json) =>
|
|
|
|
FixedEncodedString32.fromString(json as String);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
@immutable
|
2023-05-29 15:24:57 -04:00
|
|
|
class FixedEncodedString43 extends EncodedString {
|
2023-07-05 18:48:06 -04:00
|
|
|
const FixedEncodedString43._(String s) : super(s);
|
|
|
|
static int encodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 43;
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
static int decodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2023-07-12 21:28:00 -04:00
|
|
|
factory FixedEncodedString43.fromBytes(Uint8List bytes) {
|
|
|
|
if (bytes.length != decodedLength()) {
|
|
|
|
throw Exception("length ${bytes.length} should be ${decodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString43._(base64UrlNoPadEncode(bytes));
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
factory FixedEncodedString43.fromString(String s) {
|
|
|
|
var d = base64UrlNoPadDecode(s);
|
|
|
|
if (d.length != decodedLength()) {
|
|
|
|
throw Exception("length ${s.length} should be ${encodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString43._(s);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
String toJson() => toString();
|
|
|
|
factory FixedEncodedString43.fromJson(dynamic json) =>
|
|
|
|
FixedEncodedString43.fromString(json as String);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
@immutable
|
2023-05-29 15:24:57 -04:00
|
|
|
class FixedEncodedString86 extends EncodedString {
|
2023-07-05 18:48:06 -04:00
|
|
|
const FixedEncodedString86._(String s) : super(s);
|
|
|
|
static int encodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 86;
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
static int decodedLength() {
|
2023-05-29 15:24:57 -04:00
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
String toJson() {
|
|
|
|
return toString();
|
|
|
|
}
|
|
|
|
|
2023-07-12 21:28:00 -04:00
|
|
|
factory FixedEncodedString86.fromBytes(Uint8List bytes) {
|
|
|
|
if (bytes.length != decodedLength()) {
|
|
|
|
throw Exception("length ${bytes.length} should be ${decodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString86._(base64UrlNoPadEncode(bytes));
|
|
|
|
}
|
|
|
|
|
2023-07-05 18:48:06 -04:00
|
|
|
factory FixedEncodedString86.fromString(String s) {
|
|
|
|
var d = base64UrlNoPadDecode(s);
|
|
|
|
if (d.length != decodedLength()) {
|
|
|
|
throw Exception("length ${s.length} should be ${encodedLength()}");
|
|
|
|
}
|
|
|
|
return FixedEncodedString86._(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
factory FixedEncodedString86.fromJson(dynamic json) =>
|
|
|
|
FixedEncodedString86.fromString(json as String);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|