native_crypto 0.8.1 copy "native_crypto: ^0.8.1" to clipboard
native_crypto: ^0.8.1 copied to clipboard

Fast AES, MD5, SHA and HMAC cryptographic functions, using built-in system API, with hardware-acceleration (if supported).

example/lib/main.dart

import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:path_provider/path_provider.dart';
import 'package:native_crypto/native_crypto.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late int sumResult;
  late Future<int> sumAsyncResult;

  Future<void> downloadFile(String url, String savePath) async {
    var httpClient = HttpClient();
    IOSink? fileSink;
    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();

      // 建立檔案物件
      fileSink = File(savePath).openWrite();
      await response.pipe(fileSink);
      fileSink.close();
    } finally {
      httpClient.close();
      fileSink?.close();
    }
  }

  Future<void> testFileHashing(
    String filePath,
    NCryptoHashStream hash,
    String assertValue,
  ) async {
    await File(filePath).openRead().pipe(hash);
    log("hashing result: $hash");
    assert(hash.toString() == assertValue);
  }

  Future<void> test() async {
    var baseDir = await getApplicationDocumentsDirectory();
    var baseDirPath = Platform.isAndroid
        ? "${baseDir.path}/"
        : ""; //"d:/test/zip/";
    final String url =
        "https://github.com/jakky1/flutter_native_zip/releases/download/v1/native_zip_libs_android_min_v1.tar.gz";
    final String srcFile = "${baseDirPath}test.tar.gz";
    final String aesEncryptedFile = "${baseDirPath}test.tar.gz--aes-encrypted";
    final String aesDecryptedFile = "${baseDirPath}test.tar.gz--aes-decrypted";

    //File(srcFile).deleteSync();
    await downloadFile(url, srcFile);
    log("download file success: size = ${File(srcFile).lengthSync()}");

    await testFileHashing(
      srcFile,
      NCrypto.md5(),
      "16913d991805bdb654969be0a9716729",
    );
    await testFileHashing(
      srcFile,
      NCrypto.sha1(),
      "11107dad62edc630b33346fb12bf4aa4b45d4656",
    );
    if (!Platform.isWindows) {
      // Windows not support SHA224
      await testFileHashing(
        srcFile,
        NCrypto.sha224(),
        "61647969617cd0fff45dcd51f1d76fb93e372248a91c356b13f5d640",
      );
    }
    await testFileHashing(
      srcFile,
      NCrypto.sha256(),
      "44fca133cee30b10b8d7a8c51fc6ee2e03e16096a9bb224441a0643d0798784c",
    );
    await testFileHashing(
      srcFile,
      NCrypto.sha384(),
      "567749feb72aabcf76ab1a1d557e6488d375d0e04680158a9a85f10fb0a5d5a400bc65eef254a9d502f671511feac7e8",
    );
    await testFileHashing(
      srcFile,
      NCrypto.sha512(),
      "a7ed41732b650fc12f8bb3a51a44e2c735ddd7b551c940c7a3fb6075e2b6bff392114ea3133b43bb9cd67a0b79bb4b495735f557b629c13e35806c2d8ea97f7e",
    );

    await testFileHashing(
      srcFile,
      NCrypto.hmacMd5(hmacKey),
      "7bd26bc708e4eb3c06496ca07e5fd8aa",
    );
    await testFileHashing(
      srcFile,
      NCrypto.hmacSha1(hmacKey),
      "140d7e7d9ac0637dd7ddd1b2234576e8ffa88088",
    );
    if (!Platform.isWindows) {
      await testFileHashing(
        srcFile,
        NCrypto.hmacSha224(hmacKey),
        "8c4b1e8ab8aae7155adb403effae046025e20bbf73c3744bc97df5d3",
      );
    }
    await testFileHashing(
      srcFile,
      NCrypto.hmacSha256(hmacKey),
      "a336bc9bcb00f24a73f21faa696be6553facb2358ac4c73fea5b091c52cf90c5",
    );
    await testFileHashing(
      srcFile,
      NCrypto.hmacSha384(hmacKey),
      "591437357a6147d95bde98c9db2655a4694e217cdcac9819cf9d5a431e928c17c5da3aae557d5dc0be8b521d05d1d8e3",
    );
    await testFileHashing(
      srcFile,
      NCrypto.hmacSha512(hmacKey),
      "67d5f1e8ab311819a5c7b4985604b8a5b581154b4fc636c55794bc86b375b5fc06e3a80d9dc0464f5a0611849463f5d1886d47152ae8a98908d5615ca66bd1ee",
    );

    log("md5 convert A => ${NCrypto.md5().convert(utf8.encode("test"))}");
    log("md5 convert B => ${NCrypto.md5().convert(utf8.encode("abcd"))}");

    var cipher = NCrypto.aesCbc;
    var key = cipher.newKey256();
    var iv = cipher.newIV();
    //var iv = <int>[]; //aesMode.newIV();

    log("start encrypt file...");
    var encryptedFileSink = File(aesEncryptedFile).openWrite();
    var md5 = NCrypto.md5();
    await File(srcFile)
        .openRead()
        .transform(md5)
        .transform(cipher.encrypt(key, iv))
        .pipe(encryptedFileSink);
    await encryptedFileSink.close();
    log(
      "encrypted: file size = ${File(aesEncryptedFile).lengthSync()}, md5: $md5}",
    );
    assert(md5.hexString == '16913d991805bdb654969be0a9716729');

    log("start decrypt file...");
    var decryptedFileSink = File(aesDecryptedFile).openWrite();
    await File(
      aesEncryptedFile,
    ).openRead().transform(cipher.decrypt(key, iv)).pipe(decryptedFileSink);
    await decryptedFileSink.close();
    log("decrypted: file size = ${File(aesDecryptedFile).lengthSync()}");

    var sameA = await isFileEqual(srcFile, aesDecryptedFile);
    log("decrypted sameA: $sameA");
    assert(sameA);

    var sameB = await isFileEqual(srcFile, aesEncryptedFile);
    log("encrypted sameB: $sameB");
    assert(!sameB);
  }

  Future<bool> isFileEqual(String file1, String file2) async {
    final bytes1 = await File(file1).readAsBytes();
    final bytes2 = await File(file2).readAsBytes();

    if (bytes1.length != bytes2.length) return false;
    for (int i = 0; i < bytes1.length; i++) {
      var ch1 = bytes1[i];
      var ch2 = bytes2[i];
      if (ch1 != ch2) {
        return false;
      }
    }
    return true;
  }

  @override
  void initState() {
    super.initState();
  }

  Future<void> testMultiTimes() async {
    for (int i = 0; i < 100; i++) {
      await test();
    }
  }

  @override
  Widget build(BuildContext context) {
    //testMultiTimes();
    test();

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Native Packages')),
        body: Center(child: Text("ccc")),
      ),
    );
  }
}

var hmacKey = [
  0x34,
  0x41,
  0xD2,
  0x60,
  0x11,
  0x97,
  0xCE,
  0xCE,
  0x50,
  0x6C,
  0x68,
  0xD5,
  0x63,
  0x42,
  0xC2,
  0xD5,
  0x83,
  0x1D,
  0x27,
  0xC5,
  0x08,
  0xCC,
  0x0C,
  0x07,
  0x2F,
  0xCE,
  0x7E,
  0x3D,
  0x6D,
  0x42,
  0x3A,
  0x99,
];

/*
var key = [
  0x34,
  0x41,
  0xD2,
  0x60,
  0x11,
  0x97,
  0xCE,
  0xCE,
  0x50,
  0x6C,
  0x68,
  0xD5,
  0x63,
  0x42,
  0xC2,
  0xD5,
  0x83,
  0x1D,
  0x27,
  0xC5,
  0x08,
  0xCC,
  0x0C,
  0x07,
  0x2F,
  0xCE,
  0x7E,
  0x3D,
  0x6D,
  0x42,
  0x3A,
  0x99,
];

var iv = [
  0xC4,
  0xC6,
  0x1B,
  0x40,
  0x19,
  0x07,
  0x1C,
  0xA3,
  0x78,
  0x6C,
  0x87,
  0xB5,
  0x93,
  0x69,
  0x42,
  0x45,
];
*/
0
likes
150
points
139
downloads

Publisher

unverified uploader

Weekly Downloads

Fast AES, MD5, SHA and HMAC cryptographic functions, using built-in system API, with hardware-acceleration (if supported).

Repository (GitHub)
View/report issues

Topics

#aes #md5 #sha #crypto #ffi

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

ffi, jni

More

Packages that depend on native_crypto

Packages that implement native_crypto