CryptUtil class
Utilitário de criptografia com suporte a múltiplos algoritmos.
Implementação Dart pura, zero dependências externas. Todos os algoritmos operam sobre EncryptedPayload, que serializa chave, nonce, ciphertext, tag e algoritmo em JSON/Base64.
Algoritmos disponíveis
| Algoritmo | Segurança | Método |
|---|---|---|
| ChaCha20-Poly1305 (padrão) | AEAD ✓ | encryptBytes / decryptBytes |
| AES-256-GCM | AEAD ✓ | encryptAesGcm / decryptAesGcm |
| AES-CBC + PKCS#7 | Não autenticado ⚠️ | encryptAesCbc / decryptAesCbc |
| AES-CTR | Não autenticado ⚠️ | encryptAesCtr / decryptAesCtr |
Use decryptAny para decifrar qualquer EncryptedPayload independente do algoritmo utilizado na cifragem.
Uso rápido — ChaCha20-Poly1305 (recomendado)
final key = CryptUtil.generateKey(); // 32 bytes
final payload = CryptUtil.encryptText('segredo', key: key);
final texto = CryptUtil.decryptText(payload); // 'segredo'
AES-256-GCM (compatibilidade com sistemas AES)
final key = CryptUtil.generateKey(); // 32 bytes → AES-256
final nonce = CryptUtil.generateNonce(); // 12 bytes
final payload = CryptUtil.encryptAesGcm(dados, key: key, nonce: nonce);
final dados = CryptUtil.decryptAesGcm(payload);
Dispatch automático
// Decifra qualquer payload, independente do algoritmo
final plain = CryptUtil.decryptAny(payload);
Serialização
final encoded = payload.toBase64();
final restored = EncryptedPayload.fromBase64(encoded);
final texto = CryptUtil.decryptText(restored);
Segurança
- Nunca reutilize o mesmo par (key, nonce/iv) para mensagens diferentes.
- Armazene a chave em local seguro (
flutter_secure_storage, keychain, etc.). - Prefira AEAD (ChaCha20-Poly1305 ou AES-GCM) — detectam adulteração.
- CBC e CTR não são autenticados — combine com HMAC-SHA256 se necessário.
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
-
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 Methods
-
decryptAesCbc(
EncryptedPayload payload) → List< int> -
Decriptografa
payload(AES-CBC) e retorna os bytes sem padding. -
decryptAesCtr(
EncryptedPayload payload) → List< int> -
Decriptografa
payload(AES-CTR) e retorna os bytes originais. -
decryptAesGcm(
EncryptedPayload payload) → List< int> -
Decriptografa
payload(AES-GCM) e retorna os bytes originais. -
decryptAny(
EncryptedPayload payload) → List< int> -
Decifra
payloaddespachando para o algoritmo correto automaticamente. -
decryptBytes(
EncryptedPayload payload) → List< int> -
Decriptografa
payload(ChaCha20-Poly1305) e retorna os bytes originais. -
decryptFromBase64(
String encoded) → String - Decriptografa um payload base64 gerado por encryptToBase64.
-
decryptText(
EncryptedPayload payload) → String -
Decriptografa
payload(ChaCha20-Poly1305) e retorna a string original. -
encryptAesCbc(
List< int> bytes, {Uint8List? key, Uint8List? iv}) → EncryptedPayload -
Criptografa
bytescom AES-CBC + PKCS#7 e retorna um EncryptedPayload. -
encryptAesCtr(
List< int> bytes, {Uint8List? key, Uint8List? initialCounterBlock}) → EncryptedPayload -
Criptografa
bytescom AES-CTR e retorna um EncryptedPayload. -
encryptAesGcm(
List< int> bytes, {Uint8List? key, Uint8List? nonce, List<int> ? aad}) → EncryptedPayload -
Criptografa
bytescom AES-GCM e retorna um EncryptedPayload. -
encryptBytes(
List< int> bytes, {Uint8List? key, Uint8List? nonce, List<int> ? aad}) → EncryptedPayload -
Criptografa
bytescom ChaCha20-Poly1305 e retorna um EncryptedPayload. -
encryptText(
String text, {Uint8List? key, Uint8List? nonce, List< int> ? aad}) → EncryptedPayload -
Criptografa
textcom ChaCha20-Poly1305 e retorna um EncryptedPayload. -
encryptToBase64(
String text, {Uint8List? key, Uint8List? nonce, List< int> ? aad}) → String -
Criptografa
texte retorna o payload como string base64. -
generateIv(
) → Uint8List - Gera um IV criptograficamente seguro de 16 bytes (AES-CBC / AES-CTR).
-
generateKey(
) → Uint8List - Gera uma chave criptograficamente segura de 32 bytes (AES-256 / ChaCha20).
-
generateKey128(
) → Uint8List - Gera uma chave criptograficamente segura de 16 bytes (AES-128).
-
generateNonce(
) → Uint8List - Gera um nonce criptograficamente seguro de 12 bytes (ChaCha20 / AES-GCM).