google / webcrypto.dart

Cross-platform implementation of Web Cryptography APIs
https://pub.dev/packages/webcrypto
Apache License 2.0
81 stars 48 forks source link
crypto cryptography dart flutter webcrypto

Cross-Platform Web Cryptography Implemenation

This package provides a cross-platform implementation of the Web Cryptograph API.

Disclaimer: This is not an officially supported Google product.

This packages provides an implementation of the Web Cryptograph API across multiple platforms. Outside the browser, this package features a native implementation embedding BoringSSL using dart:ffi. When used inside a web browser this package wraps the window.crypto APIs and providing the same Dart API as the native implementation.

This way, package:webcrypto provides the same crypto API on Android, iOS, Web, Windows, Linux and Mac.

Example

import 'dart:convert' show base64, utf8;
import 'package:webcrypto/webcrypto.dart';

Future<void> main() async {
  final digest = await Hash.sha256.digestBytes(utf8.encode('Hello World'));
  print(base.encode(digest));
}

Coverage Status

Features:

Missing:

For a discussion of the API design of this package, see doc/design-rationale-md.

Use with flutter test

Unlike most plugins it is possible to run code that uses package:webcrypto with flutter test. For this to work the native library must be built in the application folder where flutter test is called. This can be done with:

# Only necessary when package:webcrypto is used from 'flutter test'
# This is not necessary for development with 'flutter run' and hot-reload
$ flutter pub run webcrypto:setup

# Now it's possible to run tests that uses package:webcrypto
$ flutter test test/my_test_file_using_webcrypto.dart

This requires:

The native library will be stored in .dart_tool/webcrypto/ which should not be under source control.

It is also possible to run tests with Flutter Web using flutter test -p chrome, this does not require any additional setup steps.

Limitations

This package has a few limitations compared to the Web Cryptograph API. For a discussion of parity with Web Cryptography APIs see doc/webcrypto-parity.md.

Compatibility notes

This package has many tests cases to asses compatibility across the native implementation using BoringSSL and various browser implementations of the Web Cryptography APIs.

At the moment compatibility testing is limited to native implementation, Chrome, Firefox and Safari.

Known Issues:

References