2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-01-31 13:02:34 +03:00
|
|
|
import 'dart:math';
|
|
|
|
|
2022-07-06 16:22:15 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2022-01-31 13:02:34 +03:00
|
|
|
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-06 16:22:15 +03:00
|
|
|
int issuerSpace = byteLength(issuer);
|
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-06 16:22:15 +03:00
|
|
|
remaining - 1 - max(byteLength(name), 1),
|
2022-01-31 13:02:34 +03:00
|
|
|
remaining - issuerSpace,
|
|
|
|
);
|
|
|
|
}
|