Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Jan 17, 2025
1 parent a22d882 commit f5fcb83
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/examples/ecc_raw_private_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ int main() {
Botan::hex_decode_locked("D2AC61C35CAEE918E47B0BD5E61DA9B3A5C2964AB317647DEF6DFC042A06C829");

const auto domain = Botan::EC_Group::from_name(curve_name);
const auto private_scalar = Botan::BigInt(private_scalar_bytes);

// This function will return nullopt if the value is not in the valid range
// for the group. Note this includes the case where the bytestring is not
// exactly of length equal to the group order, here 32 bytes.
const auto private_scalar = Botan::EC_Scalar::deserialize(domain, private_scalar_bytes);

if(!private_scalar) {
std::cerr << "Private key is invalid\n";
return 1;
}

// This loads the private scalar into an ECDH_PrivateKey. Creating an
// ECDSA_PrivateKey would work the same way.
const auto private_key = Botan::ECDH_PrivateKey(domain, private_scalar);
const auto private_key = Botan::ECDH_PrivateKey(domain, private_scalar.value());
const auto public_key = private_key.public_key();

std::cout << "Private Key (PEM):\n\n" << Botan::PKCS8::PEM_encode(private_key) << '\n';
std::cout << "Public Key (PEM):\n\n" << Botan::X509::PEM_encode(*public_key) << '\n';
return 0;
}

0 comments on commit f5fcb83

Please sign in to comment.