validator_util.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:logging/logging.dart';
  4. import 'package:photos/models/key_attributes.dart';
  5. Logger _logger = Logger("Validator");
  6. void validatePreVerificationStateCheck(
  7. KeyAttributes? keyAttr,
  8. String? password,
  9. String? encryptedToken,
  10. ) {
  11. nullOrEmptyArgCheck(encryptedToken, "encryptedToken");
  12. nullOrEmptyArgCheck(password, "userPassword");
  13. if (keyAttr == null) {
  14. throw ArgumentError("key Attributes can not be null");
  15. }
  16. nullOrEmptyArgCheck(keyAttr.kekSalt, "keySalt");
  17. nullOrEmptyArgCheck(keyAttr.encryptedKey, "encryptedKey");
  18. nullOrEmptyArgCheck(keyAttr.keyDecryptionNonce, "keyDecryptionNonce");
  19. nullOrEmptyArgCheck(keyAttr.encryptedSecretKey, "encryptedSecretKey");
  20. nullOrEmptyArgCheck(
  21. keyAttr.secretKeyDecryptionNonce,
  22. "secretKeyDecryptionNonce",
  23. );
  24. nullOrEmptyArgCheck(keyAttr.publicKey, "publicKey");
  25. if (keyAttr.memLimit == null || keyAttr.opsLimit == null) {
  26. throw ArgumentError("Key mem/OpsLimit can not be null");
  27. }
  28. if (keyAttr.memLimit! <= 0 || keyAttr.opsLimit! <= 0) {
  29. throw ArgumentError("Key mem/OpsLimit can not be <0");
  30. }
  31. // check password encoding issues
  32. try {
  33. final Uint8List passwordL = utf8.encode(password!) as Uint8List;
  34. try {
  35. utf8.decode(passwordL);
  36. } catch (e) {
  37. _logger.severe("CRITICAL: password decode failed", e);
  38. rethrow;
  39. }
  40. } catch (e) {
  41. _logger.severe('CRITICAL: password encode failed');
  42. rethrow;
  43. }
  44. }
  45. void nullOrEmptyArgCheck(String? value, String name) {
  46. if (value == null || value.isEmpty) {
  47. throw ArgumentError("Critical: $name is nullOrEmpty");
  48. }
  49. }