当前位置:网站首页>Rust blockchain development - signature encryption and private key public key
Rust blockchain development - signature encryption and private key public key
2022-07-05 03:59:00 【He who knows what comes will go against it】
Signature and encryption
1. Use the private key and encrypt the information , Then decrypt the information with the public key , It's mathematical signature , The meaning of signature is to ensure that the source of information is the private key holder .
2. Use public key encryption , Private key decryption , Encrypted transmission , It is to ensure that only the private key holder can decrypt and read the information even if it is exposed .
3. To implement signature or encryption , There are generally three steps , Key pair generation algorithm , Private key signature algorithm , Public key verification algorithm .
Code example
1.Rust Provides crypto library , It realizes the symmetric password commonly used in cryptography 、 Public key password 、 One way hash function 、 Message authentication code 、 digital signature 、 Random number generator and other algorithms .
2. The signature here needs ed25519 This man's algorithm .
rely on
[dependencies]
rust-crypto = "^0.2"
secp256k1 = "0.23.1"
hex = "0.4.3"
sha256 = "1.0.3"
Sign information
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)
}
Verify the signature
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. Turn the signature into String Output .
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"
边栏推荐
- 25K 入职腾讯的那天,我特么哭了
- Assembly - getting started
- DFS and BFS concepts of trees and graphs
- Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
- ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
- NEW:Devart dotConnect ADO. NET
- How to make the listbox scroll automatically when adding a new item- How can I have a ListBox auto-scroll when a new item is added?
- Use object composition in preference to class inheritance
- [an Xun cup 2019] not file upload
- postman和postman interceptor的安装
猜你喜欢

UI automation test farewell to manual download of browser driver
![[wp]bmzclub writeup of several questions](/img/15/2838b93a605b09d3e2996f6067775c.png)
[wp]bmzclub writeup of several questions

Redis source code analysis: redis cluster

面试汇总:这是一份全面&详细的Android面试指南

UE4 DMX和grandMA2 onPC 3.1.2.5的操作流程
![Quick start of UI component development of phantom engine [umg/slate]](/img/8b/cee092ec1ab105a7e234143bd56861.jpg)
Quick start of UI component development of phantom engine [umg/slate]
![[luat-air105] 4.1 file system FS](/img/5e/7fdeedaef420736d761f4a681cd2d8.jpg)
[luat-air105] 4.1 file system FS

Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
![[数组]566. 重塑矩阵-简单](/img/3c/593156f5bde67bd56828106d7bed3c.png)
[数组]566. 重塑矩阵-简单

Containerd series - what is containerd?
随机推荐
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
Assembly - getting started
[software reverse analysis tool] disassembly and decompilation tool
花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
Containerd series - detailed explanation of plugins
Thread Basics
An elegant program for Euclid‘s algorithm
Interview summary: This is a comprehensive & detailed Android interview guide
[software reverse - basic knowledge] analysis method, assembly instruction architecture
一文带你了解BI的前世今身与企业数字化转型的关系
特殊版:SpreadJS v15.1 VS SpreadJS v15.0
Operation flow of UE4 DMX and grandma2 onpc 3.1.2.5
Multimedia query
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
Uni app common functions /api
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
输入的查询SQL语句,是如何执行的?
DFS and BFS concepts of trees and graphs
【软件逆向-分析工具】反汇编和反编译工具
在线文本行固定长度填充工具