Module crypto

Module crypto 

Source
Expand description

This module provides cryptographic utilities and key pair structures for various cryptographic algorithms, including Ed25519, X25519, and SHA-256 hashing.

The module includes the following submodules:

  • [ed25519]: Provides Ed25519 key pair generation and signature functionality.
  • [x25519]: Provides X25519 key pair generation and Diffie-Hellman key exchange functionality.
  • [traits]: Defines common traits for cryptographic operations.
  • [mod@sha256_hash]: Provides functionality for SHA-256 hashing.

The module also re-exports key types and utilities for easier access.

§Example

# use did_utils::crypto::{Ed25519KeyPair, X25519KeyPair,
#                        sha256_hash,
#                        Generate, CoreSign, ECDH};

// Example usage of Ed25519 key pair
let keypair = Ed25519KeyPair::new()?;
let json_file = "test_resources/crypto_ed25519_test_sign_verify.json";
let json_data = std::fs::read_to_string(json_file)?;
let signature = keypair.sign(json_data.as_bytes());
// Verify the signature
let verified = keypair.verify(json_data.as_bytes(), &signature?);

// Example usage of X25519 key pair
let alice_seed = b"TMwLj2p2qhcuVhaFAj3QkkJGhK6pdyKx";
let bob_seed = b"NWB6DbnIlewWVp5jIJOSgyX8msXNPPAL";
let alice = X25519KeyPair::new_with_seed(alice_seed)?;
let bob = X25519KeyPair::new_with_seed(bob_seed)?;

let alice_shared_secret = alice.key_exchange(&bob);
let bob_shared_secret = bob.key_exchange(&alice);
assert_eq!(alice_shared_secret, bob_shared_secret);

// Example usage of SHA-256 hashing
let hash = sha256_hash(json_file.as_bytes());

Structs§

AsymmetricKey
A wrapper struct for an asymmetric key pair.

Enums§

Algorithm
Supported cryptographic algorithms.
Error
The set of errors that can occur during key operations.
PublicKeyFormat

Constants§

BYTES_LENGTH_32
The length of a 32-byte key material.

Traits§

CoreSign
A trait for types that support creating and verifying digital signatures.
ECDH
A trait for types that support ECDH key exchange operations.
Generate
A trait for types that support deterministic key generation.
KeyMaterial
A trait for types that hold key material bytes.
ToMultikey
ToPublic
A trait for converting a key to its public counterpart.

Functions§

sha256_hash
Compute the SHA256 hash of a given input.
sha256_multihash
Compute SHA256 hash of a given input and return it as a multibase-encoded string.

Type Aliases§

Ed25519KeyPair
X25519KeyPair