2022-07-05 15:53:21 +03:00
|
|
|
import 'dart:convert';
|
2022-01-31 13:02:34 +03:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import '../models.dart';
|
|
|
|
import '../../core/models.dart';
|
|
|
|
|
|
|
|
/// Calculates the available space for issuer and account name.
|
|
|
|
///
|
|
|
|
/// Returns a [Pair] of the space available for the issuer and account name,
|
|
|
|
/// respectively, based on the current state of the credential.
|
|
|
|
Pair<int, int> getRemainingKeySpace(
|
|
|
|
{required OathType oathType,
|
|
|
|
required int period,
|
|
|
|
required String issuer,
|
|
|
|
required String name}) {
|
|
|
|
int remaining = 64; // The field is 64 bytes in total.
|
|
|
|
|
|
|
|
if (oathType == OathType.totp && period != defaultPeriod) {
|
|
|
|
// Non-standard TOTP periods are stored as part of this data, as a "D/"- prefix.
|
|
|
|
remaining -= '$period/'.length;
|
|
|
|
}
|
2022-07-05 15:53:21 +03:00
|
|
|
int issuerSpace = utf8.encode(issuer).length;
|
2022-01-31 13:02:34 +03:00
|
|
|
if (issuer.isNotEmpty) {
|
|
|
|
// Issuer is separated from name with a ":", if present.
|
|
|
|
issuerSpace += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pair(
|
|
|
|
// Always reserve at least one character for name
|
2022-07-05 15:53:21 +03:00
|
|
|
remaining - 1 - max(utf8.encode(name).length, 1),
|
2022-01-31 13:02:34 +03:00
|
|
|
remaining - issuerSpace,
|
|
|
|
);
|
|
|
|
}
|