forDevice static method
Future<TomAesGcmCacheCipher>
forDevice(
- TomCacheKeyStore secrets, {
- String handle = keyHandle,
- Random? random,
The cipher for this device: over the key secrets holds under handle,
minted first when it holds none.
A key that is missing — a wiped keychain, a restored machine — is replaced rather than reported. What was sealed under the old one no longer opens, which the cache reads as a miss; nothing is lost, because nothing in a cache is the only copy.
Implementation
static Future<TomAesGcmCacheCipher> forDevice(
TomCacheKeyStore secrets, {
String handle = keyHandle,
Random? random,
}) async {
final stored = await secrets.read(handle);
if (stored != null) {
try {
final key = base64.decode(stored);
if (key.length == _keyBytes) {
return TomAesGcmCacheCipher(key, random: random);
}
} on FormatException {
// Not a key; replaced below.
}
}
final source = random ?? Random.secure();
final key = Uint8List.fromList([
for (var i = 0; i < _keyBytes; i++) source.nextInt(256),
]);
await secrets.write(handle, base64.encode(key));
return TomAesGcmCacheCipher(key, random: random);
}