Skip to content

Commit 16bd041

Browse files
WhitePeonyBwzl521JqLiu123longJJ2016yekai1003
committed
Task37 提供国密算法使用相关的工具和示例合约 (#130)
* 自定义任务40-基于区块链的分级诊疗数据共享案例分析 (#83) * DevSecretBallot (#86) * longjj2016 (#78) 1.修改之前问题,改为以下方法 10进制ascii转字符串 16进制ascii转字符串 2.文档描述更新 * 添加剪刀石头布的合约玩法 (#79) * add solidity file * add tutorial * add basic solidity tutorials * add assets * divergence * add divergence game * 增加func_call_contract.sol 及doc (#80) * Base64编解码库 (#81) * Counter: SimpleCounter; MultiSigCounter (#82) * 32 医疗卫生合约通用模版 (#84) * #32 医疗卫生合约模版 * 完善#32 医疗合约模版 * Solidity开发教程进阶版 (#85) * add solidity file * add tutorial * add basic solidity tutorials * add assets * divergence * add divergence game * advance solidity tutorial init * solidity tutorial advance Co-authored-by: longJJ2016 <1017303686@qq.com> Co-authored-by: 高野 <yekai1003@163.com> Co-authored-by: itherunder <61953384+itherunder@users.noreply.github.com> Co-authored-by: Kesling <43846775+Kesling515@users.noreply.github.com> Co-authored-by: kimroniny <1301862177@qq.com> Co-authored-by: Ferwoo <wufeiphil@163.com> * fix redundant return values * one (#96) * Task 37 提供国密算法使用相关的工具和示例合约 --------- Co-authored-by: wzl521 <63525530+wzl521@users.noreply.github.com> Co-authored-by: JqLiu123 <85717832+JqLiu123@users.noreply.github.com> Co-authored-by: longJJ2016 <1017303686@qq.com> Co-authored-by: 高野 <yekai1003@163.com> Co-authored-by: itherunder <61953384+itherunder@users.noreply.github.com> Co-authored-by: Kesling <43846775+Kesling515@users.noreply.github.com> Co-authored-by: kimroniny <1301862177@qq.com> Co-authored-by: Ferwoo <wufeiphil@163.com> Co-authored-by: luswar <luswar@163.com> Co-authored-by: dalaocu <njumjy06@126.com> Co-authored-by: zye-xdu <92678671+zye-xdu@users.noreply.github.com>
1 parent 95e5f44 commit 16bd041

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Task 37 提供国密算法使用相关的工具和示例合约
2+
3+
## 国密算法接口分析:
4+
5+
```solidity
6+
contract Crypto
7+
{
8+
//这个函数的作用是对输入的数据进行SM3哈希运算,并返回一个256位的哈希值。SM3是国内推出的一种密码散列函数标准,类似于SHA-256,但在某些方面略有不同。
9+
function sm3(bytes memory data) public view returns(bytes32){}
10+
//这个函数的作用是对输入的数据进行Keccak-256哈希运算,并返回一个256位的哈希值。Keccak-256是一种常用的加密哈希函数。
11+
function keccak256Hash(bytes memory data) public view returns(bytes32){}
12+
//这个函数的作用是使用SM2椭圆曲线数字签名算法对输入的消息进行验证。它需要提供消息内容、公钥、以及两个与该消息相关的参数r和s。如果验证成功,该函数将返回true以及用于签名的地址,否则返回false。
13+
function sm2Verify(bytes32 message, bytes memory publicKey, bytes32 r, bytes32 s) public view returns(bool, address){}
14+
//这个函数的作用是使用Curve25519-VRF算法对输入的字符串进行验证。它需要提供输入的字符串、VRF公钥和VRF证明。如果验证成功,该函数将返回true以及一个256位的随机数,否则返回false。Curve25519-VRF是一种用于生成可验证的随机数的算法,具有高度安全性和隐私保护性。
15+
function curve25519VRFVerify(string memory input, string memory vrfPublicKey, string memory vrfProof) public view returns(bool,uint256){}
16+
}
17+
```
18+
19+
国密算法示例:
20+
21+
```solidity
22+
pragma solidity ^0.8.0;
23+
24+
contract SM2Example {
25+
// 引入 Crypto 合约
26+
ICrypto constant Crypto = ICrypto(address(0x5006));
27+
28+
// 使用 SM2 签名方法对数据进行签名
29+
function sm2Sign(bytes memory data, bytes memory privateKey) public view returns(bytes32 r, bytes32 s) {
30+
bytes32 messageHash = crypto.sm3(data);
31+
(r, s) = sm2.sign(privateKey, messageHash);
32+
}
33+
34+
// 使用 SM2 验证签名是否有效
35+
function sm2Verify(bytes memory data, bytes memory publicKey, bytes32 r, bytes32 s) public view returns(bool) {
36+
bytes32 messageHash = crypto.sm3(data);
37+
bool result;
38+
address recoveredAddr;
39+
(result, recoveredAddr) = crypto.sm2Verify(messageHash, publicKey, r, s);
40+
return result;
41+
}
42+
43+
// 使用 SM3 计算数据的哈希值
44+
function sm3Hash(bytes memory data) public view returns(bytes32) {
45+
return crypto.sm3(data);
46+
}
47+
}
48+
49+
```
50+
51+
## 工具和示例示例:
52+
53+
本示例合约演示了如何使用国密算法 SM2 进行数字签名和验签。示例是通过 FISCO BCOS 的预编译合约实现。 ICrypto.sol 文件声明需要调用的预编译合约接口 通过 `ICrypto constant Crypto = ICrypto(address(0x5006))` 完成接口的注册和使用
54+
55+
```solidity
56+
pragma solidity ^0.8.0;
57+
58+
// 导入预编译合约
59+
import "./ICrypto.sol";
60+
61+
contract SM2Demo {
62+
ICrypto constant Crypto = ICrypto(address(0x5006));
63+
64+
// 生成 SM2 密钥对
65+
function generateSM2KeyPair() public view returns (bytes memory, bytes memory) {
66+
return Crypto.generateSM2KeyPair();
67+
}
68+
69+
// SM2 数字签名
70+
function sm2Sign(string memory _message, bytes memory _privateKey) public view returns (bytes32, bytes32) {
71+
bytes32 messageHash = Crypto.sm3(bytes(_message));
72+
(bytes32 r, bytes32 s) = Crypto.sm2Sign(messageHash, _privateKey);
73+
return (r, s);
74+
}
75+
76+
// SM2 验证数字签名
77+
function sm2Verify(string memory _message, bytes memory _publicKey, bytes32 _r, bytes32 _s) public view returns (bool, address) {
78+
bytes32 messageHash = Crypto.sm3(bytes(_message));
79+
return Crypto.sm2Verify(messageHash, _publicKey, _r, _s);
80+
}
81+
}
82+
83+
```
84+
85+
该示例合约中定义了三个函数:
86+
87+
- generateSM2KeyPair(): 用于生成 SM2 密钥对。
88+
- sm2Sign(string memory _message, bytes memory _privateKey): 使用私钥对消息进行数字签名,返回签名结果 r 和 s。
89+
- sm2Verify(string memory _message, bytes memory _publicKey, bytes32 _r, bytes32 _s): 使用公钥验证数字签名,返回验证结果和签名者地址。
90+
91+
## 注意点
92+
93+
在演示数字签名时,先使用 SM3 计算消息哈希值,然后再调用预编译合约中提供的 sm2Sign() 函数进行数字签名。而在验签时,则需要使用相同的 SM3 哈希算法计算消息哈希值,并调用预编译合约中提供的 sm2Verify() 函数进行验签。

0 commit comments

Comments
 (0)