diff --git a/ring/Cargo.lock b/ring/Cargo.lock index 7d3cfea..1721424 100644 --- a/ring/Cargo.lock +++ b/ring/Cargo.lock @@ -18,6 +18,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" + [[package]] name = "js-sys" version = "0.3.39" @@ -76,6 +82,7 @@ dependencies = [ name = "ring" version = "0.1.0" dependencies = [ + "hex", "ring 0.16.13", ] diff --git a/ring/Cargo.toml b/ring/Cargo.toml index 82bcdf4..6367a41 100644 --- a/ring/Cargo.toml +++ b/ring/Cargo.toml @@ -8,4 +8,4 @@ edition = "2018" [dependencies] ring = "0.16.13" - +hex = "0.4.2" diff --git a/ring/src/main.rs b/ring/src/main.rs index 7b4d1d0..b801a47 100644 --- a/ring/src/main.rs +++ b/ring/src/main.rs @@ -1,8 +1,11 @@ use ring::{ + signature::{ KeyPair, Ed25519KeyPair, UnparsedPublicKey, ED25519 }, hmac, rand, error::Unspecified, digest, }; +const SEED_LEN: usize = 32; + fn main() -> Result<(), Unspecified> { { println!("{} HHmac {}", "-".repeat(10), "-".repeat(10)); @@ -21,6 +24,17 @@ fn main() -> Result<(), Unspecified> { let sha256 = digest::digest(&digest::SHA256, b"hello, world"); println!("{:?}", sha256); } + { + println!("{} EdDSA {}", "-".repeat(10), "-".repeat(10)); + let rng = rand::SystemRandom::new(); + let seed: [u8; SEED_LEN] = rand::generate(&rng)?.expose(); + let key_pair = Ed25519KeyPair::from_seed_unchecked(&seed)?; + let sig = key_pair.sign(&"hello world".as_bytes()); + println!("{}", hex::encode(sig.as_ref())); + let public_key = key_pair.public_key().as_ref(); + let verify_result = UnparsedPublicKey::new(&ED25519, &public_key).verify(&"hello world".as_bytes(), sig.as_ref()); + println!("verify: {}", verify_result.is_ok()); + } Ok(()) }