mirror of
https://github.com/Auties00/Reboot-Launcher.git
synced 2026-01-14 11:39:17 +01:00
<feat: New project structure>
<feat: New release>
This commit is contained in:
52
gui/lib/src/util/cryptography.dart
Normal file
52
gui/lib/src/util/cryptography.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
import 'package:bcrypt/bcrypt.dart';
|
||||
import 'package:pointycastle/export.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
const int _ivLength = 16;
|
||||
const int _keyLength = 32;
|
||||
|
||||
String hashPassword(String plaintext) => BCrypt.hashpw(plaintext, BCrypt.gensalt());
|
||||
|
||||
bool checkPassword(String password, String hashedText) => BCrypt.checkpw(password, hashedText);
|
||||
|
||||
String aes256Encrypt(String plainText, String password) {
|
||||
final random = Random.secure();
|
||||
final iv = Uint8List.fromList(List.generate(_ivLength, (index) => random.nextInt(256)));
|
||||
final keyDerivationData = Uint8List.fromList(utf8.encode(password));
|
||||
final derive = PBKDF2KeyDerivator(HMac(SHA256Digest(), _ivLength * 8));
|
||||
var params = Pbkdf2Parameters(iv, _ivLength * 8, _keyLength);
|
||||
derive.init(params);
|
||||
final key = derive.process(keyDerivationData);
|
||||
final cipherParams = PaddedBlockCipherParameters(
|
||||
KeyParameter(key),
|
||||
null,
|
||||
);
|
||||
final aes = AESEngine();
|
||||
final paddingCipher = PaddedBlockCipherImpl(PKCS7Padding(), aes);
|
||||
paddingCipher.init(true, cipherParams);
|
||||
final plainBytes = Uint8List.fromList(utf8.encode(plainText));
|
||||
final encryptedBytes = paddingCipher.process(plainBytes);
|
||||
return base64.encode([...iv, ...encryptedBytes]);
|
||||
}
|
||||
|
||||
String aes256Decrypt(String encryptedText, String password) {
|
||||
final encryptedBytes = base64.decode(encryptedText);
|
||||
final salt = encryptedBytes.sublist(0, _ivLength);
|
||||
final payload = encryptedBytes.sublist(_ivLength);
|
||||
final keyDerivationData = Uint8List.fromList(utf8.encode(password));
|
||||
final derive = PBKDF2KeyDerivator(HMac(SHA256Digest(), _ivLength * 8));
|
||||
var params = Pbkdf2Parameters(salt, _ivLength * 8, _keyLength);
|
||||
derive.init(params);
|
||||
final key = derive.process(keyDerivationData);
|
||||
final cipherParams = PaddedBlockCipherParameters(
|
||||
KeyParameter(key),
|
||||
null,
|
||||
);
|
||||
final aes = AESEngine();
|
||||
final paddingCipher = PaddedBlockCipherImpl(PKCS7Padding(), aes);
|
||||
paddingCipher.init(false, cipherParams);
|
||||
final decryptedBytes = paddingCipher.process(payload);
|
||||
return utf8.decode(decryptedBytes);
|
||||
}
|
||||
Reference in New Issue
Block a user