validator_util.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 ?? 0) <= 0 || (keyAttr.opsLimit ?? 0) <= 0) {
  26. throw ArgumentError("Key mem/OpsLimit can not be null or <0");
  27. }
  28. // check password encoding issues
  29. try {
  30. final Uint8List passwordL = utf8.encode(password);
  31. try {
  32. utf8.decode(passwordL);
  33. } catch (e) {
  34. _logger.severe("CRITICAL: password decode failed", e);
  35. rethrow;
  36. }
  37. } catch (e) {
  38. _logger.severe('CRITICAL: password encode failed');
  39. rethrow;
  40. }
  41. }
  42. void nullOrEmptyArgCheck(String value, String name) {
  43. if (value == null || value.isEmpty) {
  44. throw ArgumentError("Critical: $name is nullOrEmpty");
  45. }
  46. }