# Mysql encrypt on rails?

**URL:** <https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970>\
**Category:** Rails\
**Created:** [March 14, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970 "2008-03-14T20:20:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 14, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970/1 "2008-03-14T20:20:45Z")

</div>

Hi sorry, I’m a noob and my first language is not English… I just  
started with ruby/rails 2 weeks a go, and this is the first time I am  
working with my mysql db.  
I have a mysql database were I store all the postfix/courier-imap,etc  
authentication’s for this we use the mysql function encrypt()  
the problem is that I am making a front end on rails, and I have  
everything ready but when I enter the password field on the form created  
by rails, it will not encrypted of course because is not using the  
encrypt mysql function… my question is…

1. mysql/unix encrypt on rails/ruby? is String#crypt  
[http://corelib.rubyonrails.org/classes/String.html#M001529](http://corelib.rubyonrails.org/classes/String.html#M001529) ?
2. I used ./script/generate scaffold users users so I can edit the  
files but were I should look to just modify one entry of the form? the  
password field, I need for the form to enter the data on encrypt form  
into the mysql database so postfix/courier can read it.
3. can I just pass a sql statement? if so how do I tell rails that  
everything like it is is ok but just this part of the form needs to be  
passed to encrypt before it writes it to the database?

better of all I been looking and Google for long before coming to ask  
the stupid questions here but all I can find is examples with SHA1  
and yes I know encrypt is not good but for the use and compatibility  
with my mail I must use it.

Thanks and sorry for the noob questions.

[http://corelib.rubyonrails.org/classes/String.html#M001529](http://corelib.rubyonrails.org/classes/String.html#M001529)

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 14, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970/2 "2008-03-14T21:54:02Z")

</div>

now I am stuck…  
Considering my encrypt mysql column is named crypt…

class Users \< ActiveRecord::Base

```
    validates_presence_of :email, :crypt, :homedir, :maildir

    attr_reader :crypt

    def crypt=(pw)
    @crypt = pw
    salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp # 2^48

```

combos  
self.crypt = User.crypt.crypt(salt)  
end

end

What I am doing wrong here? anyone have a howto to an example with  
crypt() ?

ReK2  
escribió:\> Hi sorry, I’m a noob and my first language is not English… I just

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 15, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970/3 "2008-03-15T16:46:34Z")

</div>

> class Users \< ActiveRecord::Base
> 
> ```
> validates_presence_of :email, :crypt, :homedir, :maildir
> 
> attr_reader :crypt
> 
> ```

This is probably the first part of the problem. It tends to mess with  
the accessor methods that rails generates for attributes.

> ```
> def crypt=(pw)
> @crypt = pw
> salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp # 2^48
> 
> ```
> 
> combos  
> self.crypt = User.crypt.crypt(salt)  
> end

This is your second method - Your crypt= method is calling itself.  
Your probably want to write self.write\_attribute(:crypt,  
pw.crypt(salt)). But then again you haven’t said what exactly isn’t  
working so I’m only guessing.

Fred

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 16, 2008, 1:07am UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970/4 "2008-03-16T01:07:06Z")

</div>

Hey I was working around with what you give me and I was getting a error  
so I changed to this:

class Users \< ActiveRecord::Base

```
    validates_presence_of :email, :crypt, :homedir, :maildir

    def crypt=(pw)
            salt =

```

[Array.new(6){rand(256).chr}.join].pack(“m”).chomp # 2^48 combos  
self[:crypt] = pw.crypt(salt)

```
    end

```

end

This looks like it does the trick! thanks so much. even do I will like  
if you can point me to a good doc about the problem I was having since I  
really found things related but nothing really like I had, I don’t just  
want to copy and paste and play with it I will like to understand why I  
had it wrong and why this works.

Thanks

ReK2  
escribió:\> Hi, thanks the response I was starting to feel helpless 🙂

---

<div class="post-metadata">

**Author:** ![ReK2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@ReK2](https://www.ruby-forum.com/u/ReK2)\
**Post date:** [March 16, 2008, 12:54am UTC](https://www.ruby-forum.com/t/mysql-encrypt-on-rails/131970/5 "2008-03-16T00:54:47Z")

</div>

Hi, thanks the response I was starting to feel helpless 🙂

> > ```
> > attr_reader :crypt
> > 
> > ```
> 
> This is probably the first part of the problem. It tends to mess with  
> the accessor methods that rails generates for attributes.

hmm, the only reason I used attr\_readed is because is the only way I  
found to  
“steal” the content of the form(that ROR created based on the info my my  
database using scaffold :users )  
I have not found a better way yet(but I am a noob so no surprise) is  
there a better way to gain control of the form?  
and this will respond your question below.  
my intention is so the password field will write to my mysql database  
with the unix encrypt since that is how postifx and the other non RoR  
applications will read it… if not I will have use SHA1 that seens like  
it have lot of examples already.  
like I mention in my other email, I told RoR about my mysql database, he  
created a schema and that looks ok…  
then with the scafford: I was very impresed how fast RoR will create  
forms around my DB content… that is good… the down side is  
that how do I gain back “posesion” of at least the crypt column content  
so RoR will encrypted before it writes it to the DB…  
that was my first question I found attr\_reader so far. the other part is  
to use a unix encrypt to do so. and found  
.crypt(salt) so far googling around.  
so so far I can’t write to the DB encrypted thats my problem.

Thanks and hope people can chip in their two cents.
