<commit_before>
module PostfixAdmin
  module PHP

    def mt_rand(min, max)
      require 'openssl'

      diff = max - min
      bytes = (diff.to_s(16).length.to_f / 2).ceil
      min + ( OpenSSL::Random.random_bytes(bytes).unpack('C' * bytes).join.to_i % diff )
    end

    def generate_setup_password_salt
      require 'digest/md5'

      salt = "#{Time.new().to_i}*#{node['fqdn']}*#{mt_rand(0, 60000)}"
      ::Digest::MD5.hexdigest(salt)
    end

    def encrypt_setup_password(password, salt)
      require 'digest/sha1'

      "#{salt}:#{::Digest::SHA1.hexdigest("#{salt}:#{password}")}"
    end

    def self.array(ary)

      template =
'array(<%=
  list = [ @list ].flatten
  list.map do |item|
    @PostfixAdmin_Conf.value(item)
  end.join(", ")
%>)'

      eruby = Erubis::Eruby.new(template)
      eruby.evaluate(
        :list => ary,
        :PostfixAdmin_Conf => PostfixAdmin::Conf<commit_msg>PostfixAdmin::PHP avoid the use of Array#flatten due to a Ruby bug<commit_after>
module PostfixAdmin
  module PHP

    def mt_rand(min, max)
      require 'openssl'

      diff = max - min
      bytes = (diff.to_s(16).length.to_f / 2).ceil
      min + ( OpenSSL::Random.random_bytes(bytes).unpack('C' * bytes).join.to_i % diff )
    end

    def generate_setup_password_salt
      require 'digest/md5'

      salt = "#{Time.new().to_i}*#{node['fqdn']}*#{mt_rand(0, 60000)}"
      ::Digest::MD5.hexdigest(salt)
    end

    def encrypt_setup_password(password, salt)
      require 'digest/sha1'

      "#{salt}:#{::Digest::SHA1.hexdigest("#{salt}:#{password}")}"
    end

    def self.array(ary)

      template =
'array(<%=
  list = [ @list ].flatten
  list = @list.kind_of?(Array) ? @list : [ @list ]
  list.map do |item|
    @PostfixAdmin_Conf.value(item)
  end.join(", ")
%>)'

      eruby = Erubis::Eruby.new(template)
      eruby.evaluate(
        :list => ary,
        :PostfixAdmin_Conf => PostfixAdmin::Conf
{'fraction_non_alphanumeric': 0.16458982346832815, 'fraction_numerical': 0.013499480789200415, 'mean_word_length': 2.9406952965235176, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6491170', 'n_tokens_mistral': 751, 'n_tokens_neox': 707, 'n_words': 125}