Created
April 18, 2014 20:32
-
-
Save sstelfox/11063125 to your computer and use it in GitHub Desktop.
Calculate various key fingerprints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| # The key for the sample: | |
| private_key = OpenSSL::PKey::RSA.new(1024) | |
| # This method is for comparing an RSA key too an SSH server when it's using | |
| # MD5. You can pass either a public or private key here. | |
| def ssh_md5_fingerprint(key) | |
| OpenSSL::Digest::MD5.hexdigest( | |
| [7].pack("N") + 'ssh-rsa' + key.public_key.e.to_s(0) + key.public_key.n.to_s(0) | |
| ).scan(/../).join(':') | |
| end | |
| # This method is for comparing an RSA key too an SSH server when it's using | |
| # SHA1. You can pass either a public or private key here. | |
| def ssh_sha1_fingerprint(key) | |
| OpenSSL::Digest::SHA1.hexdigest( | |
| [7].pack("N") + 'ssh-rsa' + key.public_key.e.to_s(0) + key.public_key.n.to_s(0) | |
| ).scan(/../).join(':') | |
| end | |
| # This method is for comparing an RSA key to the hash identifier listed in x509 | |
| # (a lot of people refer to them as SSL) certificates. | |
| def x509_sha1_fingerprint(key) | |
| sequence = OpenSSL::ASN1::Sequence([ | |
| OpenSSL::ASN1::Integer.new(key.public_key.n), | |
| OpenSSL::ASN1::Integer.new(key.public_key.e) | |
| ]) | |
| OpenSSL::Digest::SHA1.hexdigest(sequence.to_der).scan(/../).join(':') | |
| end | |
| puts ssh_md5_fingerprint(private_key) | |
| puts ssh_md5_fingerprint(private_key.public_key) | |
| puts ssh_sha1_fingerprint(private_key) | |
| puts ssh_sha1_fingerprint(private_key.public_key) | |
| puts x509_sha1_fingerprint(private_key) | |
| puts x509_sha1_fingerprint(private_key.public_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment