Forked from meineerde/hash_hostname_for_ssh_known_hosts.rb
Created
April 14, 2014 13:10
-
-
Save lopesivan/10646457 to your computer and use it in GitHub Desktop.
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