flutter_rust_encrypt 2.0.0-dev.0 copy "flutter_rust_encrypt: ^2.0.0-dev.0" to clipboard
flutter_rust_encrypt: ^2.0.0-dev.0 copied to clipboard

A Flutter wrapper for the dart_rust_encrypt package.

example/lib/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rust_encrypt/flutter_rust_encrypt.dart';
import 'package:flutter_rust_encrypt_example/constants.dart';
import 'package:pointycastle/digests/sha3.dart';

final Uint8List testInput = Uint8List.fromList(utf8.encode(testText));

void main() async {
  debugPrint('Hash test input size: ${testText.length / pow(1024, 2)} MB');
  FlutterRustEncrypt.init();
  runApp(const MaterialApp(home: ComparePage()));
}

class ComparePage extends StatefulWidget {
  const ComparePage({super.key});

  @override
  ComparePageState createState() => ComparePageState();
}

class ComparePageState extends State<ComparePage> {
  Stopwatch rustStopwatch = Stopwatch();
  late Timer rustTimer;

  Stopwatch dartStopwatch = Stopwatch();
  late Timer dartTimer;

  bool timersStarted = false; // To track if timers are started

  @override
  Widget build(BuildContext context) {
    final Color rustColor = rustStopwatch.isRunning ? Colors.red : Colors.green;
    final Color dartColor = dartStopwatch.isRunning ? Colors.red : Colors.green;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Compare Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Test ${testText.length / pow(1024, 2)}MB text',
              style: const TextStyle(
                fontSize: 25,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 75),
            Text(
              'Elapsed Time for Rust Hash:\n ${rustStopwatch.elapsed.inSeconds} Sec/milliSec: ${rustStopwatch.elapsed.inMilliseconds}',
              style: TextStyle(
                fontSize: 25,
                color: rustColor,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 50),
            Text(
              'Elapsed Time for Dart Hash:\n ${dartStopwatch.elapsed.inSeconds} Sec/milliSec: ${dartStopwatch.elapsed.inMilliseconds}',
              style: TextStyle(fontSize: 25, color: dartColor),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 50),
            ElevatedButton(
              onPressed: !timersStarted ? startTimers : null,
              child: const Text('Start'),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> startTimers() async {
    dartStopwatch.reset();
    rustStopwatch.reset();

    setState(() {
      timersStarted = true;
    });

    // Dart related actions
    dartStopwatch.start();
    dartTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      setState(() {});
    });

    // Doing it in another isolate so the UI thread is not blocked
    unawaited(
      compute(dartHash, null).then((_) {
        dartStopwatch.stop();
        dartTimer.cancel();
        setState(() {});
      }),
    );

    // Rust related actions
    rustStopwatch.start();
    rustTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      setState(() {});
    });
    // Rust doesn't block the UI thread (:))
    unawaited(
      rustHash().then((_) {
        rustStopwatch.stop();
        rustTimer.cancel();
        setState(() {});
      }),
    );

    timersStarted = false;
  }

  static Future<void> dartHash(void _) async {
    final sha3digest = SHA3Digest(256)..reset();
    final result = sha3digest.process(testInput);
    debugPrint('Dart result: $result');
    debugPrint('Dart hash completed');
  }

  Future<void> rustHash() async {
    final result = await DartRustEncryptAsync.sha3_256(testInput);
    debugPrint('Rust result: $result');
    debugPrint('Rust hash completed');
  }
}
5
likes
150
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter wrapper for the dart_rust_encrypt package.

Documentation

API reference

License

MIT (license)

Dependencies

dart_rust_encrypt, flutter

More

Packages that depend on flutter_rust_encrypt