BaiomyPasswordEncryption class
A singleton for two-way AES-256-CBC encryption and decryption.
Use this when you need to recover the original value later — for example, encrypting a password before storing it in Firestore and decrypting it when you need to authenticate against a service.
If you only need to verify a password at login (and never read it back), use PasswordHasher instead — one-way hashing is more secure for that case.
Firestore workflow
// ── On registration (void main) ──────────────────────────────────────────────────────
await BaiomyPasswordEncryption.instance.configure(keyPhrase: 'load-from-secure-vault');
final payload = BaiomyPasswordEncryption.instance.encrypt(passwordCtrl.text);
await FirebaseFirestore.instance.collection('users').doc(uid).set({
'password': payload.combined, // "ivBase64:ciphertextBase64"
});
// ── On login / when you need the original password ───────────────────────
final doc = await FirebaseFirestore.instance.collection('users').doc(uid).get();
final plain = BaiomyPasswordEncryption.instance.decrypt(doc['password'] as String);
Key management
Call configure once before first use (e.g. in main()), passing a
passphrase you load from a secure vault, environment variable, or
flutter_secure_storage. Never hardcode it.
await BaiomyPasswordEncryption.instance.configure(keyPhrase: 'from-your-vault');
If configure is never called, defaultKeyPhrase is used automatically so development keeps working — but always override it before shipping.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
configure(
{required String keyPhrase}) → void -
Derives and caches the AES-256 key from
keyPhrase. -
decrypt(
String combined) → String -
Decrypts a
combinedstring produced by encrypt. -
encrypt(
String plainText) → EncryptedPayload -
Encrypts
plainTextwith AES-256-CBC and a fresh random 16-byte IV. -
encryptToString(
String plainText) → String -
Convenience: encrypts
plainTextand returns the combined string directly. -
isValidPayload(
String value) → bool -
Returns
trueifvalueis a validivBase64:ciphertextBase64string. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → BaiomyPasswordEncryption
-
The single instance.
final
Constants
- defaultKeyPhrase → const String
- Fallback key phrase used when configure has never been called.