Note that there are some explanatory texts on larger screens.

plurals
  1. PORails truncates hash on save:
    text
    copied!<p>I have a rails model with a hashed password field in it (surprise, surprise), which after some manipulation, is 40 characters long. I generate a user in script/console and it appears as follows:</p> <pre><code>#&lt;User id: 1, firstname: "true", lastname: "false", username: "chaines51", hashed_password: "2Gr0GWvPunB3x5jomRTSTZJRIelC2RW103d7f3db"&gt; </code></pre> <p>I then run user_instance.save, which returns true, and the user then looks like this:</p> <pre><code>#&lt;User id: 1, firstname: "true", lastname: "false", username: "chaines51", hashed_password: "103d7f3db"&gt; </code></pre> <p>Any idea what is happening to the other 30+ characters? I changed the field in the migration from string to text, but it still gets truncated</p> <p>EDIT: The model code is:</p> <pre><code>require 'digest/sha1' class User &lt; ActiveRecord::Base validates_presence_of :username, :password, :password_confirmation, :firstname, :lastname validates_length_of :username, :within =&gt; 3..40 validates_length_of :password, :within =&gt; 5..40 validates_uniqueness_of :username validates_confirmation_of :password belongs_to :school attr_protected :id, :salt attr_accessor :password, :password_confirmation def self.random_string(len) #generate a random salt consisting of digits and letters. chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a salt = "" 1.upto(len) { |i| salt &lt;&lt; chars[rand(chars.size-1)] } return salt end def password=(pass) @password=pass @salt = User.random_string(40-pass.length) self.hashed_password = User.encrypt(@password, @salt) end def self.encrypt(pass, salt) hash = Digest::SHA1.hexdigest(pass+salt) hash.slice!(0..(40-pass.length-1)) hash = salt+hash; end def self.checkhash(pass, hash) salt = hash.slice!(0..40-pass.length-1) rehash = User.encrypt(pass, salt) return rehash == (salt+hash) end def self.authenticate(login, pass) u = User.find_by_username(login) return nil if u.nil? return u if User.checkhash(pass, u.hashed_password) nil end end </code></pre> <p>and the db/schema.rb is:</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20100127034504) do create_table "categories", :force =&gt; true do |t| t.string "title" end create_table "questions", :force =&gt; true do |t| t.string "question" t.string "a" t.string "b" t.string "c" t.string "d" t.string "e" t.datetime "created_at" t.datetime "updated_at" end create_table "questions_quizzes", :id =&gt; false, :force =&gt; true do |t| t.integer "app_id" t.integer "category_id" t.datetime "created_at" t.datetime "updated_at" end create_table "quizzes", :force =&gt; true do |t| t.string "title" t.integer "category_id" end create_table "schools", :force =&gt; true do |t| t.string "name" t.integer "coach_id" end create_table "users", :force =&gt; true do |t| t.string "firstname", :null =&gt; false t.string "lastname", :null =&gt; false t.string "username", :null =&gt; false t.boolean "needs_pass", :default =&gt; false t.integer "school_id" t.datetime "created_at" t.datetime "updated_at" t.boolean "confirmed", :default =&gt; false t.text "hashed_password" end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload