pin_service.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'dart:convert';
  2. import 'dart:math';
  3. import 'dart:typed_data';
  4. import 'package:crypto/crypto.dart';
  5. import 'secure_store.dart';
  6. /// Stores a salted PBKDF2-HMAC-SHA256 hash of the four-digit PIN in
  7. /// Keystore-backed secure storage. The PIN itself is never persisted.
  8. class PinService {
  9. PinService(this._store, {this.iterations = 12000});
  10. final SecureStore _store;
  11. final int iterations;
  12. static const _saltKey = 'cardash.pin.salt';
  13. static const _hashKey = 'cardash.pin.hash';
  14. static const _iterationsKey = 'cardash.pin.iterations';
  15. static final RegExp _validPin = RegExp(r'^\d{4}$');
  16. Future<bool> isPinSet() async {
  17. final hash = await _store.read(_hashKey);
  18. return hash != null && hash.isNotEmpty;
  19. }
  20. Future<void> setPin(String pin) async {
  21. if (!_validPin.hasMatch(pin)) {
  22. throw ArgumentError('The PIN must be exactly four digits.');
  23. }
  24. final salt = _randomBytes(16);
  25. final derived = _derive(pin, salt, iterations);
  26. await _store.write(_saltKey, base64Encode(salt));
  27. await _store.write(_hashKey, base64Encode(derived));
  28. await _store.write(_iterationsKey, '$iterations');
  29. }
  30. Future<bool> verify(String pin) async {
  31. if (!_validPin.hasMatch(pin)) return false;
  32. final saltRaw = await _store.read(_saltKey);
  33. final hashRaw = await _store.read(_hashKey);
  34. final iterationsRaw = await _store.read(_iterationsKey);
  35. if (saltRaw == null || hashRaw == null) return false;
  36. final salt = base64Decode(saltRaw);
  37. final expected = base64Decode(hashRaw);
  38. final rounds = int.tryParse(iterationsRaw ?? '') ?? iterations;
  39. final actual = _derive(pin, salt, rounds);
  40. return _constantTimeEquals(expected, actual);
  41. }
  42. Future<void> clearPin() async {
  43. await _store.delete(_saltKey);
  44. await _store.delete(_hashKey);
  45. await _store.delete(_iterationsKey);
  46. }
  47. Uint8List _derive(String pin, List<int> salt, int rounds) {
  48. final hmac = Hmac(sha256, utf8.encode(pin));
  49. var block = Uint8List.fromList([...salt, 0, 0, 0, 1]);
  50. var u = Uint8List.fromList(hmac.convert(block).bytes);
  51. final result = Uint8List.fromList(u);
  52. for (var round = 1; round < rounds; round++) {
  53. u = Uint8List.fromList(hmac.convert(u).bytes);
  54. for (var i = 0; i < result.length; i++) {
  55. result[i] ^= u[i];
  56. }
  57. }
  58. return result;
  59. }
  60. Uint8List _randomBytes(int length) {
  61. final random = Random.secure();
  62. return Uint8List.fromList(
  63. List<int>.generate(length, (_) => random.nextInt(256)),
  64. );
  65. }
  66. bool _constantTimeEquals(List<int> a, List<int> b) {
  67. if (a.length != b.length) return false;
  68. var difference = 0;
  69. for (var i = 0; i < a.length; i++) {
  70. difference |= a[i] ^ b[i];
  71. }
  72. return difference == 0;
  73. }
  74. }