Created
June 25, 2021 07:00
-
-
Save pinkeshdarji/656e6a62ad7dff6602f96c8b81494826 to your computer and use it in GitHub Desktop.
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: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