Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lopesivan/10646457 to your computer and use it in GitHub Desktop.
Save lopesivan/10646457 to your computer and use it in GitHub Desktop.
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