diff options
author | Jack Lloyd <[email protected]> | 2017-12-14 15:45:55 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-12-14 16:07:53 -0500 |
commit | 1b6c50860a1cf7776de448f24814f01a700ef2e9 (patch) | |
tree | 84ac7840e2bf5fe94d0816cccbfcd7130aec0e6e /src/lib/pubkey/pk_keys.cpp | |
parent | 5c7f3b45198655bd0fae435c428b1f9256610ac5 (diff) |
Add ability to fingerprint public keys
Diffstat (limited to 'src/lib/pubkey/pk_keys.cpp')
-rw-r--r-- | src/lib/pubkey/pk_keys.cpp | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/src/lib/pubkey/pk_keys.cpp b/src/lib/pubkey/pk_keys.cpp index 52304eb03..cdf8ae8ff 100644 --- a/src/lib/pubkey/pk_keys.cpp +++ b/src/lib/pubkey/pk_keys.cpp @@ -14,6 +14,27 @@ namespace Botan { +std::string create_hex_fingerprint(const uint8_t bits[], + size_t bits_len, + const std::string& hash_name) + { + std::unique_ptr<HashFunction> hash_fn(HashFunction::create_or_throw(hash_name)); + const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len)); + + std::string fprint; + + for(size_t i = 0; i != hex_hash.size(); i += 2) + { + if(i != 0) + fprint.push_back(':'); + + fprint.push_back(hex_hash[i]); + fprint.push_back(hex_hash[i+1]); + } + + return fprint; + } + std::vector<uint8_t> Public_Key::subject_public_key() const { return DER_Encoder() @@ -52,27 +73,19 @@ secure_vector<uint8_t> Private_Key::private_key_info() const } /* -* Hash of the PKCS #8 encoding for this key object +* Hash of the X.509 subjectPublicKey encoding */ -std::string Private_Key::fingerprint(const std::string& alg) const +std::string Public_Key::fingerprint_public(const std::string& hash_algo) const { - secure_vector<uint8_t> buf = private_key_bits(); - std::unique_ptr<HashFunction> hash(HashFunction::create(alg)); - hash->update(buf); - const auto hex_print = hex_encode(hash->final()); - - std::string formatted_print; - - for(size_t i = 0; i != hex_print.size(); i += 2) - { - formatted_print.push_back(hex_print[i]); - formatted_print.push_back(hex_print[i+1]); - - if(i != hex_print.size() - 2) - formatted_print.push_back(':'); - } + return create_hex_fingerprint(subject_public_key(), hash_algo); + } - return formatted_print; +/* +* Hash of the PKCS #8 encoding for this key object +*/ +std::string Private_Key::fingerprint_private(const std::string& hash_algo) const + { + return create_hex_fingerprint(private_key_bits(), hash_algo); } std::unique_ptr<PK_Ops::Encryption> |