crypto_util.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'dart:typed_data';
  2. import 'dart:io' as io;
  3. import 'package:aes_crypt/aes_crypt.dart';
  4. import 'package:computer/computer.dart';
  5. import 'package:encrypt/encrypt.dart';
  6. import 'dart:convert';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:steel_crypt/steel_crypt.dart' as steel;
  9. import 'package:uuid/uuid.dart';
  10. class CryptoUtil {
  11. static Uint8List getSecureRandomBytes({int length = 32}) {
  12. return SecureRandom(length).bytes;
  13. }
  14. static Uint8List scrypt(Uint8List plainText, Uint8List salt) {
  15. return steel.PassCrypt.scrypt()
  16. .hashBytes(salt: salt, input: plainText, len: 32);
  17. }
  18. static Uint8List aesEncrypt(
  19. Uint8List plainText, Uint8List key, Uint8List iv) {
  20. final encrypter = AES(Key(key), mode: AESMode.cbc);
  21. return encrypter
  22. .encrypt(
  23. plainText,
  24. iv: IV(iv),
  25. )
  26. .bytes;
  27. }
  28. static String decryptFromBase64(
  29. String base64CipherText, String base64Key, String base64IV) {
  30. final encrypter = AES(Key.fromBase64(base64Key), mode: AESMode.cbc);
  31. return utf8.decode(encrypter.decrypt(
  32. Encrypted.fromBase64(base64CipherText),
  33. iv: IV.fromBase64(base64IV),
  34. ));
  35. }
  36. static Future<String> encryptFileToFile(
  37. String sourcePath, String destinationPath, String key) async {
  38. final args = Map<String, String>();
  39. args["key"] = key;
  40. args["source"] = sourcePath;
  41. args["destination"] = destinationPath;
  42. return Computer().compute(runEncryptFileToFile, param: args);
  43. }
  44. static Future<String> encryptDataToFile(
  45. Uint8List source, String destinationPath, String base64EncodedKey) async {
  46. final args = Map<String, dynamic>();
  47. args["key"] = base64EncodedKey;
  48. args["source"] = source;
  49. args["destination"] = destinationPath;
  50. return Computer().compute(runEncryptDataToFile, param: args);
  51. }
  52. static Future<String> encryptDataToData(
  53. Uint8List source, String base64EncodedKey) async {
  54. final destinationPath =
  55. Configuration.instance.getTempDirectory() + Uuid().v4();
  56. return encryptDataToFile(source, destinationPath, base64EncodedKey)
  57. .then((value) {
  58. final file = io.File(destinationPath);
  59. final data = file.readAsBytesSync();
  60. file.deleteSync();
  61. return base64.encode(data);
  62. });
  63. }
  64. static Future<void> decryptFileToFile(String sourcePath,
  65. String destinationPath, String base64EncodedKey) async {
  66. final args = Map<String, String>();
  67. args["key"] = base64EncodedKey;
  68. args["source"] = sourcePath;
  69. args["destination"] = destinationPath;
  70. return Computer().compute(runDecryptFileToFile, param: args);
  71. }
  72. static Future<Uint8List> decryptFileToData(
  73. String sourcePath, String base64EncodedKey) {
  74. final args = Map<String, String>();
  75. args["key"] = base64EncodedKey;
  76. args["source"] = sourcePath;
  77. return Computer().compute(runDecryptFileToData, param: args);
  78. }
  79. static Future<Uint8List> decryptDataToData(
  80. Uint8List source, String base64EncodedKey) {
  81. final sourcePath = Configuration.instance.getTempDirectory() + Uuid().v4();
  82. final file = io.File(sourcePath);
  83. file.writeAsBytesSync(source);
  84. return decryptFileToData(sourcePath, base64EncodedKey).then((value) {
  85. file.deleteSync();
  86. return value;
  87. });
  88. }
  89. }
  90. Future<String> runEncryptFileToFile(Map<String, String> args) {
  91. final encrypter = getEncrypter(args["key"]);
  92. return encrypter.encryptFile(args["source"], args["destination"]);
  93. }
  94. Future<String> runEncryptDataToFile(Map<String, dynamic> args) {
  95. final encrypter = getEncrypter(args["key"]);
  96. return encrypter.encryptDataToFile(args["source"], args["destination"]);
  97. }
  98. Future<String> runDecryptFileToFile(Map<String, dynamic> args) async {
  99. final encrypter = getEncrypter(args["key"]);
  100. return encrypter.decryptFile(args["source"], args["destination"]);
  101. }
  102. Future<Uint8List> runDecryptFileToData(Map<String, dynamic> args) async {
  103. final encrypter = getEncrypter(args["key"]);
  104. return encrypter.decryptDataFromFile(args["source"]);
  105. }
  106. AesCrypt getEncrypter(String key) {
  107. final encrypter = AesCrypt(key);
  108. encrypter.aesSetMode(AesMode.cbc);
  109. encrypter.setOverwriteMode(AesCryptOwMode.on);
  110. return encrypter;
  111. }