如何理解签名体制包含3个算法:KeyGen(密钥生成算法),Sign(签名算法),Verify(验证算法);

以及以下话:

“公钥加密,私钥解密是密送,保证消息即使公开也只有私钥持有者能读懂。
私钥加密,公钥解密是签名,保证消息来源是私钥持有者。”

什么是公钥,什么是私钥?长什么形状?

一、标准库的hash值

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
fn calculate_hash<T: Hash>(t: &T) -> u64 {let mut s = DefaultHasher::new();t.hash(&mut s);s.finish()
}
fn main() {let st = SystemTime::now();let text = "10086".as_bytes();println!("text:{:?}", text);let mut hasher1 = DefaultHasher::new();hasher1.write(text);println!("Hash1(低16位表示) is {:x}", hasher1.finish());println!("Hash1 is {}", hasher1.finish());let mut hasher2 = DefaultHasher::new();text.hash(&mut hasher2);println!("Hash2(低16位表示) is {:x}", hasher2.finish());println!("Hash2 is {}", hasher2.finish()); println!("calculate_hash:->Hash :{:?}", calculate_hash(&text));let mt1 = SystemTime::now();println!("is_fit_num time:{:?}", mt1.duration_since(st).unwrap());thread::sleep_ms(500000);
}

output:

text:[49, 48, 48, 56, 54]Hash1(低16位表示) is 85f84a89780553ce
Hash1 is 9653547755553248206Hash2(低16位表示) is 4b5a264ac7932dc5
Hash2 is 5429694403366301125calculate_hash:->Hash :5429694403366301125

思考 : hash1为什么和hash2不同?

二、关于keypair、signature、verify 三部曲

我们看rust-crypto库的原代码的例子
https://github.com/DaGenix/rust-crypto/blob/cc1a5fde1ce957bd1a8a2e30169443cdb4780111/src/ed25519.rs

use digest::Digest;
use sha2::{Sha512};
use curve25519::{GeP2, GeP3, ge_scalarmult_base, sc_reduce, sc_muladd, curve25519, Fe};
use util::{fixed_time_eq};
use std::ops::{Add, Sub, Mul};static L: [u8; 32] =[ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x14, 0xde, 0xf9, 0xde, 0xa2, 0xf7, 0x9c, 0xd6,0x58, 0x12, 0x63, 0x1a, 0x5c, 0xf5, 0xd3, 0xed ];pub fn keypair(seed: &[u8]) -> ([u8; 64], [u8; 32]) {let mut secret: [u8; 64] = {let mut hash_output: [u8; 64] = [0; 64];let mut hasher = Sha512::new();hasher.input(seed);hasher.result(&mut hash_output);hash_output[0] &= 248;hash_output[31] &= 63;hash_output[31] |= 64;hash_output};let a = ge_scalarmult_base(&secret[0..32]);let public_key = a.to_bytes();for (dest, src) in (&mut secret[32..64]).iter_mut().zip(public_key.iter()) {*dest = *src;}for (dest, src) in (&mut secret[0..32]).iter_mut().zip(seed.iter()) {*dest = *src;}(secret, public_key)
}pub fn signature(message: &[u8], secret_key: &[u8]) -> [u8; 64] {let seed = &secret_key[0..32];let public_key = &secret_key[32..64];let az: [u8; 64] = {let mut hash_output: [u8; 64] = [0; 64];let mut hasher = Sha512::new();hasher.input(seed);hasher.result(&mut hash_output);hash_output[0] &= 248;hash_output[31] &= 63;hash_output[31] |= 64;hash_output};let nonce = {let mut hash_output: [u8; 64] = [0; 64];let mut hasher = Sha512::new();hasher.input(&az[32..64]);hasher.input(message);hasher.result(&mut hash_output);sc_reduce(&mut hash_output[0..64]);hash_output};let mut signature: [u8; 64] = [0; 64];let r: GeP3 = ge_scalarmult_base(&nonce[0..32]);for (result_byte, source_byte) in (&mut signature[0..32]).iter_mut().zip(r.to_bytes().iter()) {*result_byte = *source_byte;}for (result_byte, source_byte) in (&mut signature[32..64]).iter_mut().zip(public_key.iter()) {*result_byte = *source_byte;}{let mut hasher = Sha512::new();hasher.input(signature.as_ref());hasher.input(message);let mut hram: [u8; 64] = [0; 64];hasher.result(&mut hram);sc_reduce(&mut hram);sc_muladd(&mut signature[32..64], &hram[0..32], &az[0..32], &nonce[0..32]);}signature
}fn check_s_lt_l(s: &[u8]) -> bool
{let mut c: u8 = 0;let mut n: u8 = 1;let mut i = 31;loop {c |= ((((s[i] as i32) - (L[i] as i32)) >> 8) as u8) & n;n &= (((((s[i] ^ L[i]) as i32)) - 1) >> 8) as u8;if i == 0 {break;} else {i -= 1;}}c == 0
}pub fn verify(message: &[u8], public_key: &[u8], signature: &[u8]) -> bool {if check_s_lt_l(&signature[32..64]) {return false;}let a = match GeP3::from_bytes_negate_vartime(public_key) {Some(g) => g,None => { return false; }};let mut d = 0;for pk_byte in public_key.iter() {d |= *pk_byte;}if d == 0 {return false;}let mut hasher = Sha512::new();hasher.input(&signature[0..32]);hasher.input(public_key);hasher.input(message);let mut hash: [u8; 64] = [0; 64];hasher.result(&mut hash);sc_reduce(&mut hash);let r = GeP2::double_scalarmult_vartime(hash.as_ref(), a, &signature[32..64]);let rcheck = r.to_bytes();fixed_time_eq(rcheck.as_ref(), &signature[0..32])
}pub fn exchange(public_key: &[u8], private_key: &[u8]) -> [u8; 32] {let ed_y = Fe::from_bytes(&public_key);// Produce public key in Montgomery form.let mont_x = edwards_to_montgomery_x(ed_y);// Produce private key from seed component (bytes 0 to 32)// of the Ed25519 extended private key (64 bytes).let mut hasher = Sha512::new();hasher.input(&private_key[0..32]);let mut hash: [u8; 64] = [0; 64];hasher.result(&mut hash);// Clamp the hash such that it is a valid private keyhash[0] &= 248;hash[31] &= 127;hash[31] |= 64;let shared_mont_x : [u8; 32] = curve25519(&hash, &mont_x.to_bytes()); // priv., pub.shared_mont_x
}

三、如何用?

看一个私钥签名,公钥解密的例子。

extern crate crypto;
use crypto::*;
use ed25519::{keypair, signature, verify, exchange};
use curve25519::{curve25519_base, curve25519};
use digest::Digest;
use sha2::Sha512;
fn main() {let seed: &[u8] = &[0x26, 0x27, 0xf6, 0x85, 0x97, 0x15, 0xad, 0x1d, 0xd2, 0x94, 0xdd, 0xc4, 0x76, 0x19, 0x39, 0x31, 0xf1, 0xad, 0xb5, 0x58, 0xf0, 0x93, 0x97, 0x32, 0x19, 0x2b, 0xd1, 0xc0, 0xfd, 0x16, 0x8e, 0x4e];//32位// KEYGEN let (private_key, public_key) = keypair(seed); //[U8,64]let message = b"This is my message!";//私钥签名let sig = signature(message, &private_key); //[U8,64]//private_keyprintln!("private_key: {:?} ", private_key.to_vec());println!("private_key_len :{:? }", private_key.len());// public_keyprintln!("public_key :{:?}", public_key.to_vec());println!("public_key_len :{:?}", public_key.len());//signatureprintln!("signature:{:?}", sig.to_vec());println!("signature_len:{:?}", sig.len());// verifyprintln!("验证是否成功:{:?} ",verify(message, &public_key, &sig));
}
private_key: [38, 39, 246, 133, 151, 21, 173, 29, 210, 148, 221, 196, 118, 25, 57, 49, 241, 173, 181, 88, 240, 147, 151, 50, 25, 43, 209, 192, 253, 22, 142, 78, 93, 109, 35,107, 82, 209, 142, 58, 182, 214, 7, 47, 182, 228, 199, 212, 107, 213, 154, 217, 204, 25, 71, 38, 95, 0, 183, 32, 250, 44, 143, 102]
private_key_len :64public_key :[93, 109, 35, 107, 82, 209, 142, 58, 182, 214, 7, 47, 182, 228, 199, 212, 107, 213, 154, 217, 204, 25, 71, 38, 95, 0, 183, 32, 250, 44, 143, 102]
public_key_len :32
signature:[163, 41, 56, 4, 173, 251, 241, 219, 149, 58, 146, 122, 98, 42, 27, 108, 25, 3, 130, 238, 94, 244, 235, 181, 70, 68, 226, 117, 98, 88, 135, 182, 110, 110, 122, 203
, 190, 66, 6, 139, 6, 242, 94, 98, 107, 99, 144, 208, 145, 218, 120, 236, 223, 60, 87, 19, 245, 249, 177, 246, 212, 160, 10, 10]
signature_len:64验证是否成功:true

Rust :公钥、私钥与keypair、signature、verify 三部曲相关推荐

  1. 分享一个RSA加解密工具类,公钥加密私钥解密、私钥加密公钥解密、私钥签名公钥验签、生成公钥私钥

    测试: public static void main(String[] args) {try {//生成公钥私钥Map<String, Object> map = RSAUtil.ini ...

  2. rsa公钥私钥生成工具

    rsa公钥私钥生成工具-java代码如下: /*** * @Description: RSA工具类,支持长度为2048的秘钥*/ @Slf4j public class RSAUtils {/*** ...

  3. java 公钥 验证_Java代码签名验证公钥私钥

    Java验证公钥私钥签名认证示例如下 importsecurity.KeyPair; importsecurity.KeyPairGenerator; importsecurity.NoSuchAlg ...

  4. 叙述无保密机制的rsa签名过程_安全系列之——RSA的公钥私钥有多少人能分的清楚?RSA的签名验签与加密解密如何使用公私钥?...

    在对接很多的互联网公司的开发平台时,这些互联网公司未来自身平台的安全,都会需要调用方签名确认调用方的身份是合法的,同时未来信息网络传输的安全可能还需要加密解密.比如对接支付宝.微信开放平台时,需要配置 ...

  5. 数字证书原理,公钥私钥加密原理

    文中首先解释了加密解密的一些基础知识和概念,然后通过一个加密通信过程的例子说明了加密算法的作用,以及数字证书的出现所起的作用.接着对数字证书做一个详细的解释,并讨论一下windows中数字证书的管理, ...

  6. 非对称加密 公钥私钥_选择Java加密算法第3部分–公钥/私钥非对称加密

    非对称加密 公钥私钥 抽象 这是涵盖Java加密算法的三部分博客系列的第3部分. 该系列涵盖如何实现以下功能: 使用SHA–512散列 使用AES–256的单密钥对称加密 RSA–4096 这第三篇文 ...

  7. 选择Java加密算法第3部分–公钥/私钥非对称加密

    抽象 这是涵盖Java加密算法的三部分博客系列的第3部分. 本系列介绍如何实现以下目标: 使用SHA–512散列 使用AES–256的单密钥对称加密 RSA–4096 这第三篇文章详细介绍了如何实现非 ...

  8. 如何:创建公钥/私钥对

    要使用强名称为程序集签名,必须具有公钥/私钥对.  这一对加密公钥和加密私钥用于在编译过程中创建强名称程序集.  您可以使用强名称工具 (Sn.exe) 来创建密钥对.  密钥对文件通常具有 .snk ...

  9. http_认证机制https加密TLSSSL密钥对(公钥私钥)

    文章目录 http_认证机制&https加密&TLS&SSL&密钥对(公钥&私钥) references 更多详情(MDN::HTTP) session& ...

  10. 非对称加密实战(一):JDK生成keystore获取公钥私钥及代码验证【附源码】

    目录 使用说明 非对称加密 生成keystore文件 公钥私钥互相解密 获取fd-alias.keystore中的公钥私钥 使用生成公钥私钥进行解密 源码地址 使用说明 非对称加密 非对称加密算法主要 ...

最新文章

  1. Computed property XXX was assigned to but it has no setter
  2. 12 种方式轻松实现 Ruby 调用
  3. Spring之作用域
  4. python简笔画绘制 数据驱动绘图恐龙_使用python turtle绘制简笔画大白-Go语言中文社区...
  5. linux内核多种进程间通信机制
  6. MOSS/Sharepoint RBS概念以及运用
  7. 数据结构-树的基础代码
  8. OAuth2.0授权码模式原理与实战
  9. 11g R2 RAC客户端负载均衡配置
  10. Ember controller
  11. 情人节程序员用HTML网页表白【表白对话】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
  12. Spring -> Spring中Bean是线程安全的吗
  13. 我的去 Google 化的安卓之旅
  14. hive中的distribute by
  15. 基于云的文档管理软件/文档管理系统(DMS)
  16. 如今引流横行的时代,你还缺乏流量吗?
  17. 贝壳找房户外拓展(中等) 扫描线
  18. 个人总结出来的git仓库迁移方案
  19. 51Nod - 1247 找规律
  20. IBM ServerGuide 8.41

热门文章

  1. SaltStack Runners
  2. 简单之美 | ZooKeeper应用案例
  3. 部分Excel函数的使用
  4. 面向对象课程第一次博客总结
  5. Python学习之not,and,or篇
  6. 《了不起的NodeJS》书籍笔记一
  7. psql 命令行使用
  8. Swift开发经验——外部参数名
  9. C# 线程调用主线程中的控件
  10. 打开WORD 2003时提示发现错误,需要关闭,还要发送错误报告给Microsoft 解决方案...