mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-07-28 09:24:12 -04:00
add lengths to typedkey
This commit is contained in:
parent
81134bad67
commit
6282bfd1c5
2 changed files with 60 additions and 0 deletions
|
@ -28,6 +28,9 @@ Uint8List cryptoKindToBytes(CryptoKind kind) {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CryptoKind cryptoKindFromBytes(Uint8List b) =>
|
||||||
|
ByteData.sublistView(b).getUint32(0);
|
||||||
|
|
||||||
CryptoKind cryptoKindFromString(String s) {
|
CryptoKind cryptoKindFromString(String s) {
|
||||||
if (s.codeUnits.length != 4) {
|
if (s.codeUnits.length != 4) {
|
||||||
throw const FormatException('malformed string');
|
throw const FormatException('malformed string');
|
||||||
|
@ -53,6 +56,11 @@ class Typed<V extends EncodedString> extends Equatable {
|
||||||
final value = EncodedString.fromString<V>(parts.sublist(1).join(':'));
|
final value = EncodedString.fromString<V>(parts.sublist(1).join(':'));
|
||||||
return Typed(kind: kind, value: value);
|
return Typed(kind: kind, value: value);
|
||||||
}
|
}
|
||||||
|
factory Typed.fromBytes(Uint8List b) {
|
||||||
|
final kind = cryptoKindFromBytes(b);
|
||||||
|
final value = EncodedString.fromBytes<V>(b.sublist(4));
|
||||||
|
return Typed(kind: kind, value: value);
|
||||||
|
}
|
||||||
factory Typed.fromJson(dynamic json) => Typed.fromString(json as String);
|
factory Typed.fromJson(dynamic json) => Typed.fromString(json as String);
|
||||||
final CryptoKind kind;
|
final CryptoKind kind;
|
||||||
final V value;
|
final V value;
|
||||||
|
@ -69,6 +77,32 @@ class Typed<V extends EncodedString> extends Equatable {
|
||||||
return b.toBytes();
|
return b.toBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int encodedLength<X>() {
|
||||||
|
switch (X) {
|
||||||
|
case const (Typed<FixedEncodedString32>):
|
||||||
|
return FixedEncodedString32.encodedLength() + 5;
|
||||||
|
case const (Typed<FixedEncodedString43>):
|
||||||
|
return FixedEncodedString43.encodedLength() + 5;
|
||||||
|
case const (Typed<FixedEncodedString86>):
|
||||||
|
return FixedEncodedString86.encodedLength() + 5;
|
||||||
|
default:
|
||||||
|
throw UnimplementedError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int decodedLength<X>() {
|
||||||
|
switch (X) {
|
||||||
|
case const (Typed<FixedEncodedString32>):
|
||||||
|
return FixedEncodedString32.decodedLength() + 4;
|
||||||
|
case const (Typed<FixedEncodedString43>):
|
||||||
|
return FixedEncodedString43.decodedLength() + 4;
|
||||||
|
case const (Typed<FixedEncodedString86>):
|
||||||
|
return FixedEncodedString86.decodedLength() + 4;
|
||||||
|
default:
|
||||||
|
throw UnimplementedError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String toJson() => toString();
|
String toJson() => toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,6 +52,32 @@ abstract class EncodedString extends Equatable {
|
||||||
@override
|
@override
|
||||||
String toString() => contents;
|
String toString() => contents;
|
||||||
|
|
||||||
|
static int encodedLength<T extends EncodedString>() {
|
||||||
|
switch (T) {
|
||||||
|
case FixedEncodedString32:
|
||||||
|
return FixedEncodedString32.encodedLength();
|
||||||
|
case FixedEncodedString43:
|
||||||
|
return FixedEncodedString43.encodedLength();
|
||||||
|
case FixedEncodedString86:
|
||||||
|
return FixedEncodedString86.encodedLength();
|
||||||
|
default:
|
||||||
|
throw UnimplementedError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int decodedLength<T extends EncodedString>() {
|
||||||
|
switch (T) {
|
||||||
|
case FixedEncodedString32:
|
||||||
|
return FixedEncodedString32.decodedLength();
|
||||||
|
case FixedEncodedString43:
|
||||||
|
return FixedEncodedString43.decodedLength();
|
||||||
|
case FixedEncodedString86:
|
||||||
|
return FixedEncodedString86.decodedLength();
|
||||||
|
default:
|
||||||
|
throw UnimplementedError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static T fromBytes<T extends EncodedString>(Uint8List bytes) {
|
static T fromBytes<T extends EncodedString>(Uint8List bytes) {
|
||||||
switch (T) {
|
switch (T) {
|
||||||
case FixedEncodedString32:
|
case FixedEncodedString32:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue