Flutter未知功能插件dart_sm的使用(注意:由于介绍为undefined,以下基于插件名称进行合理推测)

发布于 1周前 作者 wuwangju 来自 Flutter

Flutter未知功能插件dart_sm的使用

dart_sm

基于Dart语言实现的国密SM2、SM3、SM4算法。

实现内容

  • SM2
    • ✅ 非对称加密解密,支持C1C3C2和C1C2C3格式
    • ✅ 公钥压缩
    • ✅ 签名验签,包括纯签名、sm3杂凑(userId)、der编码
    • ❌ 密钥交换算法
  • SM3
    • ✅ 消息杂凑
    • ✅ hmac模式
  • SM4
    • ✅ ECB模式
    • ✅ CBC模式
    • ❌ CTR模式
    • ❌ GCM模式

安装

pubspec.yaml文件中添加依赖:

dependencies:
  dart_sm: ^0.1.4 

使用方法

SM2

生成密钥对

KeyPair keypair = SM2.generateKeyPair();
String privateKey = keypair.privateKey; // 私钥
String publicKey = keypair.publicKey; // 公钥

公钥压缩(可选)

// 66位压缩公钥
String compressedPublicKey = SM2.compressPublicKey(publicKey);
bool isEqual = SM2.comparePublicKey(compressedPublicKey, publicKey); // 判断公钥是否相等
bool isValid = SM2.verifyPublicKey(compressedPublicKey); // 验证公钥

加密解密

// 默认C1C3C2格式
String cipherText = SM2.encrypt(data, publicKey);
String plainText = SM2.decrypt(cipherText, privateKey);

// C1C2C3格式
String cipherText = SM2.encrypt(data, publicKey, cipherMode: C1C2C3);
String plainText = SM2.decrypt(cipherText, privateKey, cipherMode: C1C2C3);

签名验签

// 纯签名
String sigValue = SM2.signature(data, privateKey);
bool verifyValue = SM2.verifySignature(data, sigValue, publicKey);

// 纯签名,不做公钥推导
String sigValue = SM2.signature(data, privateKey, publicKey: publicKey);
bool verifyValue = SM2.verifySignature(data, sigValue, publicKey);

// sm3杂凑
String sigValue = SM2.signature(data, privateKey, publicKey: publicKey, hash: true, userId: 'userId');
bool verifyValue = SM2.verifySignature(data, sigValue, publicKey, hash: true, userId: 'userId');

// der编码
String sigValue = SM2.signature(data, privateKey, publicKey: publicKey, der: true);
bool verifyValue = SM2.verifySignature(data, sigValue, publicKey, der: true);

SM3

// 参数为字符串
String hashValue = SM3.hash(data);
// 参数为字节数组
String hashValue = SM3.hashBytes(data);

// hmac,key要求为16进制字符串
String hashValue = SM3.hash(data, key: '95cb90ad5ba0c7c0e2a556f0072626b3');
String hashValue = SM3.hashBytes(data, key: '95cb90ad5ba0c7c0e2a556f0072626b3');

SM4

设置全局密钥,效率比每次加解密时设置密钥高

SM4.setKey('0123456789abcdeffedcba9876543210');

加密解密

// 默认ECB模式
String cipherText = SM4.encrypt(data);
String plainText = SM4.encrypt(cipherText);

// CBC模式
String cipherText = SM4.encrypt(data, mode: SM4CryptoMode.CBC, iv: 'fedcba98765432100123456789abcdef');
String plainText = SM4.decrypt(encryptData, mode: SM4CryptoMode.CBC, iv: 'fedcba98765432100123456789abcdef');

// 单独指定密钥
String cipherText = SM4.encrypt(data, key: '0123456789abcdeffedcba9876543210');
String plainText = SM4.encrypt(cipherText, key: '0123456789abcdeffedcba9876543210');

致谢

协议

Copyright [luckykellan]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

更多关于Flutter未知功能插件dart_sm的使用(注意:由于介绍为undefined,以下基于插件名称进行合理推测)的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter未知功能插件dart_sm的使用(注意:由于介绍为undefined,以下基于插件名称进行合理推测)的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,遇到名为dart_sm的未知功能插件时,虽然官方文档或社区资源可能没有提供明确的使用说明,但我们可以根据插件名称进行一些合理的推测,并尝试编写一些可能的代码示例。请注意,以下代码仅基于插件名称dart_sm可能代表的功能进行假设,并不代表实际插件的真实功能。

假设dart_sm可能是一个与状态管理(State Management)相关的插件,类似于Provider、Riverpod或GetX等流行的状态管理库。基于这个假设,我们可以编写一些示例代码来展示如何在Flutter应用中使用这样的插件(尽管这些代码可能并不适用于实际的dart_sm插件,但可以作为理解状态管理插件使用的参考)。

假设的dart_sm插件使用示例

1. 安装插件

首先,我们需要在pubspec.yaml文件中添加对dart_sm插件的依赖(注意:这里的依赖项是假设的,实际使用时需要替换为真实的插件名和版本号):

dependencies:
  flutter:
    sdk: flutter
  dart_sm: ^0.0.1  # 假设的版本号,实际使用时需要替换为真实版本号

然后运行flutter pub get来安装插件。

2. 初始化状态管理

假设dart_sm提供了某种全局状态管理的功能,我们可以这样初始化它:

import 'package:flutter/material.dart';
import 'package:dart_sm/dart_sm.dart';  // 假设的导入路径

void main() {
  // 假设的初始化状态管理
  SM.init();

  runApp(MyApp());
}

3. 创建全局状态

假设dart_sm允许我们创建和访问全局状态:

class CounterState {
  int count = 0;
}

// 假设的初始化全局状态
SM.state<CounterState>(() => CounterState());

4. 在Widget中使用状态

假设dart_sm提供了某种上下文(context)无关的方式来访问全局状态:

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 假设的访问全局状态的方式
    CounterState state = SM.of<CounterState>();

    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${state.count}',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 假设的更新全局状态的方式
          SM.update<CounterState>((currentState) {
            currentState.count++;
          });
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

5. 运行应用

CounterPage作为应用的根Widget:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CounterPage(),
    );
  }
}

注意

  • 上述代码完全是基于假设编写的,并不代表dart_sm插件的实际功能或API。
  • 在实际使用未知插件时,建议查阅插件的官方文档或GitHub仓库以获取准确的使用说明和API参考。
  • 如果插件没有官方文档或社区支持,可能需要谨慎考虑是否使用该插件,或者尝试联系插件的维护者以获取更多信息。
回到顶部