Rust block chain development - signature encryption and private key public key

Signature and Encryption

1. Use the private key to add encrypted information, and then use the public key to decrypt the information, which is a mathematical signature. The meaning of the signature is to ensure that the source of the information is the holder of the private key.
2. Encryption with the public key and decryption with the private key is encrypted transmission to ensure that even if the information is exposed, only the holder of the private key can decrypt and understand it.
3. To implement signature or encryption, it is generally divided into three steps, generating key pair algorithm, private key signature algorithm, and public key verification algorithm.

code example

1. Rust provides the crypto library, which implements algorithms commonly used in cryptography, including symmetric cryptography, public key cryptography, one-way hash functions, message authentication codes, digital signatures, random number generators, etc.
insert image description here
2. The signature here uses the ed25519 algorithm.

rely

[dependencies]
rust-crypto = "^0.2"
secp256k1  = "0.23.1"
hex = "0.4.3"
sha256 = "1.0.3"

sign the message

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 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] 

insert image description here
3. Convert the signature to 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"

Guess you like

Origin blog.csdn.net/matt45m/article/details/125556712