cake_wallet/lib/entities/encrypt.dart

78 lines
2.6 KiB
Dart
Raw Normal View History

2023-12-15 19:01:37 +00:00
import 'dart:convert';
import 'dart:typed_data';
2020-01-04 19:31:52 +00:00
import 'package:encrypt/encrypt.dart';
2022-10-12 17:09:57 +00:00
// import 'package:password/password.dart';
2020-01-04 19:31:52 +00:00
import 'package:cake_wallet/.secrets.g.dart' as secrets;
2023-12-14 16:55:41 +00:00
import 'package:dargon2_flutter/dargon2_flutter.dart';
2023-12-15 19:01:37 +00:00
import 'package:cake_backup/backup.dart' as cake_backup;
2023-12-14 16:55:41 +00:00
Future<String> argon2Hash({required String password}) async {
final result = await argon2.hashPasswordString(password, salt: Salt.newSalt());
// the salt is stored within the encoded string:
return result.encodedString;
}
Future<bool> verifyArgon2Hash({required String password, required String hash}) async {
return argon2.verifyHashString(password, hash);
}
2023-12-15 19:01:37 +00:00
Future<String> encodeWalletPasswordV2({required String password}) async {
final _key = secrets.shortKey + secrets.walletSalt;
return utf8.decode(await cake_backup.encrypt(password, Uint8List.fromList(utf8.encode(_key))));
}
Future<String> decodeWalletPasswordV2({required String password}) async {
final _key = secrets.shortKey + secrets.walletSalt;
return utf8.decode(await cake_backup.decrypt(password, Uint8List.fromList(utf8.encode(_key))));
}
2023-12-14 16:55:41 +00:00
// @@@@@@@@@@@@@@@ OLD (kept for reference purposes, do not use!) @@@@@@@@@@@@@
2020-01-04 19:31:52 +00:00
String encrypt({required String source, required String key}) {
2020-01-04 19:31:52 +00:00
final _key = Key.fromUtf8(key);
final iv = IV.allZerosOfLength(16);
2020-01-04 19:31:52 +00:00
final encrypter = Encrypter(AES(_key));
final encrypted = encrypter.encrypt(source, iv: iv);
return encrypted.base64;
}
String decrypt({required String source, required String key}) {
2020-01-04 19:31:52 +00:00
final _key = Key.fromUtf8(key);
final iv = IV.allZerosOfLength(16);
2020-01-04 19:31:52 +00:00
final encrypter = Encrypter(AES(_key));
final decrypted = encrypter.decrypt64(source, iv: iv);
return decrypted;
}
2022-10-12 17:09:57 +00:00
String hash({required String source}) {
// FIX-ME: Uninplemented
throw Exception('Unimplemented');
// final algorithm = PBKDF2();
// final hash = Password.hash(source, algorithm);
2020-01-04 19:31:52 +00:00
2022-10-12 17:09:57 +00:00
// return hash;
2020-01-04 19:31:52 +00:00
}
2022-10-12 17:09:57 +00:00
String encodedPinCode({required String pin}) {
2020-01-04 19:31:52 +00:00
final source = '${secrets.salt}$pin';
return encrypt(source: source, key: secrets.key);
}
2022-10-12 17:09:57 +00:00
String decodedPinCode({required String pin}) {
2020-01-04 19:31:52 +00:00
final decrypted = decrypt(source: pin, key: secrets.key);
return decrypted.substring(secrets.key.length, decrypted.length);
}
2023-12-15 19:01:37 +00:00
String encodeWalletPasswordV1({required String password}) {
2020-01-04 19:31:52 +00:00
final source = password;
final _key = secrets.shortKey + secrets.walletSalt;
return encrypt(source: source, key: _key);
}
2023-12-15 19:01:37 +00:00
String decodeWalletPasswordV1({required String password}) {
2020-01-04 19:31:52 +00:00
final source = password;
final _key = secrets.shortKey + secrets.walletSalt;
return decrypt(source: source, key: _key);
}