feat: age

This commit is contained in:
2023-10-05 18:11:31 +08:00
parent 05afe67de7
commit 55e5b732f6
3 changed files with 18 additions and 0 deletions

View File

@@ -1,5 +1,13 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]]
name = "bech32"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.9.0" version = "0.9.0"
@@ -37,6 +45,7 @@ checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
name = "curve25519" name = "curve25519"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bech32",
"curve25519-dalek", "curve25519-dalek",
"ed25519-dalek", "ed25519-dalek",
"hex", "hex",

View File

@@ -12,3 +12,4 @@ rand = "0.7.3"
x25519-dalek = "1.1.0" x25519-dalek = "1.1.0"
ed25519-dalek = "1.0.1" ed25519-dalek = "1.0.1"
curve25519-dalek = "3.0.0" curve25519-dalek = "3.0.0"
bech32 = "0.9.1"

View File

@@ -1,4 +1,5 @@
use std::convert::TryInto; use std::convert::TryInto;
use bech32::{ToBase32, Variant};
use ed25519_dalek::{Keypair, Signer, Verifier}; use ed25519_dalek::{Keypair, Signer, Verifier};
use rand::rngs::OsRng; use rand::rngs::OsRng;
@@ -37,6 +38,13 @@ fn main() {
let public_key_bytes: [u8; 32] = public_key_bytes.try_into().unwrap(); let public_key_bytes: [u8; 32] = public_key_bytes.try_into().unwrap();
let public_key_card = PublicKey::from(public_key_bytes); let public_key_card = PublicKey::from(public_key_bytes);
println!("Public key card: {}", hex::encode(public_key_card.as_bytes())); println!("Public key card: {}", hex::encode(public_key_card.as_bytes()));
let age_addr = bech32::encode(
"age",
public_key_card.as_bytes().to_base32(),
Variant::Bech32,
).unwrap();
println!("Public key card as age address: {}", age_addr);
let new_secret = EphemeralSecret::new(OsRng); let new_secret = EphemeralSecret::new(OsRng);
let new_public = PublicKey::from(&new_secret); let new_public = PublicKey::from(&new_secret);
println!("New public key: {}", hex::encode(new_public.as_bytes())); println!("New public key: {}", hex::encode(new_public.as_bytes()));