added str_and_salt2sha1.rb for hashing couchdb pw
authorvarac <varacanero@zeromail.org>
Tue, 23 Apr 2013 22:13:56 +0000 (00:13 +0200)
committervarac <varacanero@zeromail.org>
Tue, 23 Apr 2013 22:13:56 +0000 (00:13 +0200)
lib/puppet/parser/functions/str_and_salt2sha1.rb [new file with mode: 0644]

diff --git a/lib/puppet/parser/functions/str_and_salt2sha1.rb b/lib/puppet/parser/functions/str_and_salt2sha1.rb
new file mode 100644 (file)
index 0000000..71d69cf
--- /dev/null
@@ -0,0 +1,32 @@
+#
+# str_and_salt2sha1.rb 
+#
+
+module Puppet::Parser::Functions
+  newfunction(:str_and_salt2sha1, :type => :rvalue, :doc => <<-EOS
+This converts a string to an array containing the salted SHA1 password hash in
+the first field, and the salt itself in second field of the returned array. 
+This combination is used i.e. for couchdb passwords.
+    EOS
+  ) do |arguments|
+    require 'digest/sha1'
+
+    raise(Puppet::ParseError, "str_and_salt2sha1(): Wrong number of arguments " +
+      "passed (#{arguments.size} but we require 1)") if arguments.size != 1
+
+    str_and_salt = arguments[0]
+
+    unless str_and_salt.is_a?(Array)
+      raise(Puppet::ParseError, 'str_and_salt2sha1(): Requires a ' +
+        "Array argument, you passed: #{password.class}")
+    end
+
+    str  = str_and_salt[0]
+    salt = str_and_salt[1]
+    sha1 = Digest::SHA1.hexdigest(str+ salt)
+
+    return sha1
+  end
+end
+
+# vim: set ts=2 sw=2 et :