Created
January 30, 2024 10:02
-
-
Save michielvaneerd/8bc971127ab521be08bea48d487c3f01 to your computer and use it in GitHub Desktop.
Encrypt and decrypt in Dart using the cryptography package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert' as convert; | |
import 'package:cryptography/cryptography.dart'; | |
class MyCrypt { | |
final AesGcm aesGcm; | |
MyCrypt() : aesGcm = AesGcm.with256bits(); | |
static const nonceLength = 12; | |
static const macLength = 16; | |
Future<SecretKey> getSecretKey(String key) async { | |
return await aesGcm.newSecretKeyFromBytes(convert.base64.decode(key)); | |
} | |
Future<String> generateSecretKey() async { | |
final secretKey = await aesGcm.newSecretKey(); | |
final bytes = await secretKey.extractBytes(); | |
final keyAsString = convert.base64.encode(bytes); | |
return keyAsString; | |
} | |
Future<String> encrypt(String text, String key) async { | |
final secretKey = await getSecretKey(key); | |
final secretBox = await aesGcm.encryptString( | |
text, | |
secretKey: secretKey, | |
); | |
// print( | |
// 'Nonce = ${secretBox.nonce.length} and mac = ${secretBox.mac.bytes.length}'); | |
return convert.base64.encode(secretBox.concatenation()); | |
} | |
Future<String> decrypt(String cipherText, String key) async { | |
final secretKey = await getSecretKey(key); | |
final data = convert.base64.decode(cipherText); | |
final secretBox = SecretBox.fromConcatenation(data, | |
nonceLength: nonceLength, macLength: macLength); | |
final clearText = await aesGcm.decryptString( | |
secretBox, | |
secretKey: secretKey, | |
); | |
return clearText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment