Index: app/models/user.rb
===================================================================
--- app/models/user.rb	(revision 2778)
+++ app/models/user.rb	(working copy)
@@ -93,6 +93,19 @@
         return nil unless user.auth_source.authenticate(login, password)
       else
         # authentication with local password
+
+        # Backwards compatibility: if the stored password is
+        # hex-encoded, convert it to base64 prepended with '{SHA}'
+        # to make it compatible with Apache.
+        if user.hashed_password[0,5] != '{SHA}'
+          pw_s = ""
+          user.hashed_password.unpack(
+                  'a2'*(user.hashed_password.length / 2)).collect do |x|
+            pw_s << x.hex
+          end
+          user.hashed_password = '{SHA}' + Base64.encode64(pw_s).chomp
+        end
+
         return nil unless User.hash_password(password) == user.hashed_password        
       end
     else
@@ -274,8 +287,10 @@
   
 private
   # Return password digest
+  # Prefix with {SHA} and use base64 encoding to be compatible with
+  # Apache basic authentication with mod_authn_dbd.
   def self.hash_password(clear_password)
-    Digest::SHA1.hexdigest(clear_password || "")
+    '{SHA}' + Base64.encode64(Digest::SHA1.digest(clear_password || "")).chomp
   end
 end
 
Index: db/migrate/102_pw_hash_apache_compat.rb
===================================================================
--- db/migrate/102_pw_hash_apache_compat.rb	(revision 0)
+++ db/migrate/102_pw_hash_apache_compat.rb	(revision 0)
@@ -0,0 +1,31 @@
+class PwHashApacheCompat < ActiveRecord::Migration
+  def self.up
+    users = User.find(:all)
+    users.each do |user|
+      next if user.hashed_password.blank? or
+              (user.hashed_password[0,5] == '{SHA}')
+
+      # If the stored password is hex-encoded, convert it to base64
+      # prepended with '{SHA}' to make it compatible with Apache.
+      pw_s = ""
+      user.hashed_password.unpack(
+              'a2'*(user.hashed_password.length / 2)).collect do |x|
+        pw_s << x.hex
+      end
+      user.hashed_password = '{SHA}' + Base64.encode64(pw_s).chomp
+      user.save
+
+    end
+  end
+
+  def self.down
+    users = User.find(:all)
+    users.each do |user|
+      next if user.hashed_password.blank? or
+              (user.hashed_password[0,5] != '{SHA}')
+      pw_s = Base64.decode64(user.hashed_password[5..-1])
+      user.hashed_password = pw_s.unpack('H*').to_s
+      user.save
+    end
+  end
+end
