Last active
December 29, 2015 16:09
-
-
Save meineerde/7695964 to your computer and use it in GitHub Desktop.
A method to generate a hashed hostname for inclusion in a known_hosts file of OpenSSH.
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' | |
require 'base64' | |
require 'securerandom' | |
def hash_hostname(hostname, salt_b64=nil) | |
if salt_b64 | |
salt = Base64.decode64(salt_b64) | |
else | |
salt = SecureRandom.random_bytes(20) | |
salt_b64 = Base64.encode64(salt).strip | |
end | |
sha1_digest = OpenSSL::Digest::Digest.new('sha1') | |
hostname_b64 = Base64.encode64(OpenSSL::HMAC.digest(sha1_digest, salt, hostname)).strip | |
"|1|#{salt_b64}|#{hostname_b64}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment