parseSteganographyKey function
Parses a 32-byte steganography key from hex, a key file, or a 32-char UTF-8 string.
Implementation
Uint8List parseSteganographyKey(String value) {
final trimmed = value.trim();
if (trimmed.isEmpty) {
throw ArgumentError('Key must not be empty');
}
if (_looksLikeHexKey(trimmed)) {
return _decodeHexKey(trimmed);
}
final file = File(trimmed);
if (file.existsSync()) {
final bytes = file.readAsBytesSync();
if (bytes.length != 32) {
throw ArgumentError(
'Key file must contain exactly 32 bytes, got ${bytes.length}',
);
}
return Uint8List.fromList(bytes);
}
final utf8Key = utf8.encode(trimmed);
if (utf8Key.length == 32) {
return Uint8List.fromList(utf8Key);
}
throw ArgumentError(
'Key must be 64 hex characters, a path to a 32-byte file, '
'or a UTF-8 string of exactly 32 characters',
);
}