Skip to content

Instantly share code, notes, and snippets.

@arnobroekhof
Created March 18, 2013 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnobroekhof/5190953 to your computer and use it in GitHub Desktop.
Save arnobroekhof/5190953 to your computer and use it in GitHub Desktop.
str2sha512password This is a puppet function that converts a string to a salted-SHA512 password hash for linux and unix systems given any simple string, you will get a sha512 hash that can be insert directly into the shadow file.
#
# str2sha512password.rb
#
module Puppet::Parser::Functions
newfunction(:str2sha512password, :type => :rvalue, :doc => <<-EOS
This converts a string to a salted-SHA512 password hash for linux and unix systems
given any simple string, you will get a sha512 hash that can be insert directly into
the shadow file.
EOS
) do |arguments|
require 'digest/sha2'
raise(Puppet::ParseError, "str2sha512password(): Wrong number of arguments " +
"passed (#{arguments.size} but we require 1)") if arguments.size != 1
password = arguments[0]
unless password.is_a?(String)
raise(Puppet::ParseError, 'str2sha512password(): Requires a ' +
"String argument, you passed: #{password.class}")
end
salt = rand(36**8).to_s(36)
shadow_hash = password.crypt("$6$" + salt)
return shadow_hash
end
end
# vim: set ts=2 sw=2 et :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment