Skip to content

Instantly share code, notes, and snippets.

@hootyjeremy
Created September 25, 2022 19:52
Show Gist options
  • Save hootyjeremy/54f43cb5c5886999a7fa255c13be10f9 to your computer and use it in GitHub Desktop.
Save hootyjeremy/54f43cb5c5886999a7fa255c13be10f9 to your computer and use it in GitHub Desktop.
import 'dart:ffi';
import 'dart:typed_data';
import 'package:sodium/sodium.dart';
void main() async {
// INCLUDE THE CORRECT PATH...
final libsodium = DynamicLibrary.open('path\\to\\libsodium.dll');
// initialize the sodium APIs
final sodium = await SodiumInit.init(libsodium);
final stopWatchForEncrypt = Stopwatch();
// The message to be encrypted, converted to an unsigned char array.
const String message = 'my very secret message';
Uint8List inputBytes = Uint8List.fromList(List.filled(100000000, 0));
// A randomly generated nonce
final nonce = sodium.randombytes.buf(
sodium.crypto.secretBox.nonceBytes,
);
// Generate a secret key
final SecureKey key = sodium.crypto.secretBox.keygen();
// Encrypt the data
print('--------------------------------------------');
print('Beginning sodium.crypto.secretBox.easy() ...');
stopWatchForEncrypt.start();
final encryptedData = sodium.crypto.secretBox.easy(
message: inputBytes,
nonce: nonce,
key: key,
);
stopWatchForEncrypt.stop;
print(
'duration for sodium.crypto.secretBox.easy: ${stopWatchForEncrypt.elapsed}');
print('------------------------------------------');
print('Beginning sodium.crypto.aead.encrypt() ...');
stopWatchForEncrypt.reset();
stopWatchForEncrypt.start();
final encryptedData2 = sodium.crypto.aead.encrypt(
message: inputBytes, //messageBytes,
nonce: nonce,
key: key,
);
stopWatchForEncrypt.stop();
print(
'duration for sodium.crypto.aead.encrypt: ${stopWatchForEncrypt.elapsed}');
// after you are done:
key.dispose();
print('done');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment