当前位置:网站首页>Rust区块琏开发——签名加密与私钥公钥
Rust区块琏开发——签名加密与私钥公钥
2022-07-05 03:40:00 【知来者逆】
签名与加密
1.使用私钥加加密信息,然后用公钥解密信息,就是数学签名,签名的意义是保证信息的来源是私钥持有者。
2.使用公钥加密,私钥解密,就是加密传输,是为保证信息即使暴露出来也只有私钥持有者才能解密读懂。
3.要实现签名或者加密,一般分为三个步骤,生成密钥对算法,私钥签名算法,公钥验证算法。
代码示例
1.Rust提供了crypto库,它实现了包含了密码学中常用的对称密码、公钥密码、单向散列函数、消息认证码、数字签名、随机数生成器等算法等等。
2.这里签名要用到ed25519这人算法。
依赖
[dependencies]
rust-crypto = "^0.2"
secp256k1 = "0.23.1"
hex = "0.4.3"
sha256 = "1.0.3"
对信息进行签名
fn sign(seed_key : String,content:String) -> ([u8;64],[u8;32],[u8;64])
{
let seed_bytes = digest_bytes(seed_key.as_bytes());
let seed_u8 = hex::decode(seed_bytes).expect("Invalid Hex String");
let mut seed: [u8;32] = [0u8;32];
let mut i = 0;
for val in seed_u8
{
seed[i] = val;
i = i+ 1;
}
let (private_key, public_key) = keypair(seed.as_ref()); //[U8,64]
let sig = signature(content.as_bytes(), &private_key); //[U8,64]
(private_key,public_key,sig)
}
验证签名
let seed = "3nuhx-7yzml-5uet3-7eplx-kq4u5-roww3-zf6ju-vdvaf-s7q4u".to_string();
let content = "13445927681169508792";
let (private_key,public_key,signature) = sign(seed, content.to_string());
print!("private key = {:?},\npublic key = {:?},\nsignature = {:?} \n",private_key,public_key,signature);
print!("verify = {:?}",verify(content.as_bytes(), &public_key, &signature));
private key = [189, 205, 57, 2, 52, 206, 172, 150, 110, 248, 19, 146, 86, 244, 107, 190, 49, 227, 130, 188, 139, 207, 157, 94, 226, 25, 27, 15, 172, 38, 160, 108, 19, 100, 112, 203, 240, 28, 155, 193, 252, 17, 194, 53, 43, 146, 165, 135, 131, 100, 154, 142, 226, 120, 180, 51, 203, 162, 168, 38, 156, 49, 115, 95],
public key = [19, 100, 112, 203, 240, 28, 155, 193, 252, 17, 194, 53, 43, 146, 165, 135, 131, 100, 154, 142, 226, 120, 180, 51, 203, 162, 168, 38, 156, 49, 115, 95],
signature = [222, 13, 90, 4, 73, 2, 58, 240, 183, 110, 19, 120, 251, 76, 219, 9, 125, 151, 156, 218, 3, 12, 237, 209, 3, 237, 176, 152, 42, 201, 186, 113, 79, 66, 6, 249, 245, 232, 102, 29, 54, 184, 249, 31, 49, 135, 226, 164, 158, 245, 76, 213, 36, 191, 161, 44, 122, 132, 47, 221, 172, 46, 144, 1]

3.把签名转成String输出。
fn au8_to_string(signature_code : [u8;64]) ->String
{
use std::fmt::Write;
let mut signature_string = String::new();
for a in signature_code.iter()
{
write!(signature_string, "{:02x}", a);
}
signature_string
}
"de0d5a0449023af0b76e1378fb4cdb097d979cda030cedd103edb0982ac9ba714f4206f9f5e8661d36b8f91f3187e2a49ef54cd524bfa12c7a842fddac2e9001"
边栏推荐
- Share the newly released web application development framework based on blazor Technology
- Deep learning - LSTM Foundation
- Yyds dry goods inventory embedded matrix
- [array]566 Reshape the matrix - simple
- Performance of calling delegates vs methods
- IronXL for .NET 2022.6
- An elegant program for Euclid‘s algorithm
- @The problem of cross database query invalidation caused by transactional annotation
- An elegant program for Euclid‘s algorithm
- MySQL winter vacation self-study 2022 11 (10)
猜你喜欢

线上故障突突突?如何紧急诊断、排查与恢复
![[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils](/img/ea/84e67a1fca0e12cc4452c744c242b4.png)
[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils

Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘

Redis source code analysis: redis cluster
![[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)](/img/99/bb05b6c48a9e70ca7ff77733d954b9.jpg)
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)

IronXL for .NET 2022.6
![[untitled]](/img/53/f67c53d4ada382ec42f97cf68d9c71.jpg)
[untitled]

函数基础学习02
![[untitled]](/img/e0/52467a7d7604c1ab77038c25608811.png)
[untitled]
![[software reverse - basic knowledge] analysis method, assembly instruction architecture](/img/97/8001db1c572495a115d32d9dd7360e.png)
[software reverse - basic knowledge] analysis method, assembly instruction architecture
随机推荐
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
Learning notes of raspberry pie 4B - IO communication (I2C)
我就一写代码的,王总整天和我谈格局...
企业级:Spire.Office for .NET:Platinum|7.7.x
[punch in questions] integrated daily 5-question sharing (phase III)
IPv6 experiment
测试开发是什么?为什么现在那么多公司都要招聘测试开发?
[move pictures up, down, left and right through the keyboard in JS]
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
speed or tempo in classical music
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)
Flex flexible layout
DMX parameter exploration of grandma2 onpc 3.1.2.5
About MySQL database connection exceptions
postman和postman interceptor的安装
[groovy] string (string injection function | asBoolean | execute | minus)
KVM virtualization
Quick start of UI component development of phantom engine [umg/slate]
【刷题】BFS题目精选
NEW:Devart dotConnect ADO. NET