Skip to content

Instantly share code, notes, and snippets.

@pinkeshdarji
Created June 25, 2021 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkeshdarji/656e6a62ad7dff6602f96c8b81494826 to your computer and use it in GitHub Desktop.
Save pinkeshdarji/656e6a62ad7dff6602f96c8b81494826 to your computer and use it in GitHub Desktop.
import 'dart:typed_data';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webcrypto/webcrypto.dart';
class AppE2EE {
static final AppE2EE
= AppE2EE._internal();
factory AppE2EE() {
return
;
}
AppE2EE._internal();
KeyPair<EcdhPrivateKey, EcdhPublicKey> keyPair;
Uint8List derivedBits;
AesGcmSecretKey aesGcmSecretKey;
final Uint8List iv = Uint8List.fromList('Initialization Vector'.codeUnits);
Future<void> generateKeys() async {
final prefs = await SharedPreferences.
();
String derivedBitsString = (prefs.getString('derivedBits') ?? '');
if (derivedBitsString.isNotEmpty) {
derivedBits = Uint8List.fromList(derivedBitsString.codeUnits);
print('derivedBits present');
return;
}
// 1. Generate keys
keyPair = await EcdhPrivateKey.
(EllipticCurve.p256);
Map<String, dynamic> publicKeyJwk =
await keyPair.publicKey.exportJsonWebKey();
Map<String, dynamic> privateKeyJwk =
await keyPair.privateKey.exportJsonWebKey();
print('keypair $keyPair, $publicKeyJwk, $privateKeyJwk');
deriveBits();
}
Future<void> deriveBits() async {
// 2. Derive bits
derivedBits = await keyPair.privateKey.deriveBits(256, keyPair.publicKey);
final prefs = await SharedPreferences.
();
prefs.setString('derivedBits', String.fromCharCodes(derivedBits));
print('derivedBits $derivedBits');
}
Future<String> encrypt(String message) async {
// 3. Encrypt
aesGcmSecretKey = await AesGcmSecretKey.
(derivedBits);
List<int> list = message.codeUnits;
Uint8List data = Uint8List.fromList(list);
Uint8List encryptedBytes = await aesGcmSecretKey.encryptBytes(data, iv);
String encryptedString = String.fromCharCodes(encryptedBytes);
print('encryptedString $encryptedString');
return encryptedString;
}
Future<String> decrypt(String encryptedMessage) async {
// 4. Decrypt
aesGcmSecretKey = await AesGcmSecretKey.
(derivedBits);
List<int> message = Uint8List.fromList(encryptedMessage.codeUnits);
Uint8List decryptdBytes = await aesGcmSecretKey.decryptBytes(message, iv);
String decryptdString = String.fromCharCodes(decryptdBytes);
print('decryptdString $decryptdString');
return decryptdString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment