smx_encrypt 0.0.1 copy "smx_encrypt: ^0.0.1" to clipboard
smx_encrypt: ^0.0.1 copied to clipboard

中国国家密码局开源的SM2,SM3,SM4系列算法实现.

example/lib/smx_encrypt_example.dart

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

import 'package:flutter/services.dart';
import 'package:smx_encrypt/smx_encrypt.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _smCryptoPlugin = SmxEncrypt();
  String? _sm2EncryptedData;

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _smCryptoPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> sm2EncryptTest() async {
    String result;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    var publicKey = "048e3902f9b50284732592b589030d477725067dba093e3100de8005c2463ad737e6438a9d579601343f7eafeb22fcf4acda52c74b5207b9689756fb93f82ae243";
    var data = "12345678";
    try {
      result =
          await _smCryptoPlugin.sm2EnCryptoUsePublicKey(publicKey, data) ?? 'empty encrypted data!';
      print("SM2 result: \n $result");
    } on PlatformException {
      result = 'Failed to use SM2EncryptData method.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _sm2EncryptedData = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              TextButton(onPressed: () {
                sm2EncryptTest();
              }, child: const Text("点击运行SM2公钥加密")),
              Text("SM2 for 12345678 加密结果: $_sm2EncryptedData"),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
135
points
14
downloads

Publisher

unverified uploader

Weekly Downloads

中国国家密码局开源的SM2,SM3,SM4系列算法实现.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on smx_encrypt

Packages that implement smx_encrypt