Created
March 18, 2013 21:24
-
-
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.
This file contains 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
# | |
# 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