<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>www.weisserth.net : Tag rails, everything about Rails</title>
    <link>http://www.weisserth.net</link>
    <atom:link type="application/rss+xml" href="http://www.weisserth.net/tag/rails.rss" rel="self"/>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>on photography, gear and web technology</description>
    <item>
      <title>Using the MySQL encrypt function in Ruby</title>
      <description>&lt;p&gt;If you&amp;#8217;re dealing with a legacy MySQL database schema where your users&amp;#8217; passwords are encrypted using &lt;a href="http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html#function_encrypt"&gt;MySQL&amp;#8217;s encrypt function&lt;/a&gt; within your schema and you want to create new records like that using Ruby rather than calling the MySQL function, you can easily do that with Ruby. Ruby supplies a &lt;a href="http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.crypt"&gt;crypt function&lt;/a&gt; that does just what the MySQL encrypt function provides. Both implementations use the &lt;span class="caps"&gt;UNIX&lt;/span&gt; C function crypt(3) so you can replace the use of one with another.&lt;/p&gt;
&lt;p&gt;The function expects a salt value of two characters. If none is provided, a random string will be used. Take a look at this simple example:&lt;/p&gt;
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;  &lt;span class="c"&gt;#!/usr/bin/env ruby -wKU&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;mysql_encrypt&lt;/span&gt;(pw)
    
    &lt;span class="c"&gt;# compute a random salt value&lt;/span&gt;
    &lt;span class="c"&gt;# (will end up to be a Base64 encoded string &lt;/span&gt;
    &lt;span class="c"&gt;# of random characters)&lt;/span&gt;
    salt = [&lt;span class="co"&gt;Array&lt;/span&gt;.new(&lt;span class="i"&gt;2&lt;/span&gt;){rand(&lt;span class="i"&gt;256&lt;/span&gt;).chr}.join].pack(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;m&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).chomp
    &lt;span class="r"&gt;return&lt;/span&gt; pw.crypt(salt)
  &lt;span class="r"&gt;end&lt;/span&gt;

  encrypted_password = mysql_encrypt(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)

  puts &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Encrypted password could be &lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + encrypted_password

  &lt;span class="c"&gt;# we take the first two characters of the already&lt;/span&gt;
  &lt;span class="c"&gt;# encrypted password as salt value&lt;/span&gt;
  &lt;span class="c"&gt;# for the re-encryption so we end up with the same value&lt;/span&gt;
  
  compared_password = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;.crypt(encrypted_password[&lt;span class="i"&gt;0&lt;/span&gt;,&lt;span class="i"&gt;2&lt;/span&gt;])

  puts &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;The re-encrypted 'test' string is now &lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + compared_password&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Creating hashed passwords using crypt(3) cannot be regarded as secure though. Take a look at the &lt;a href="http://en.wikipedia.org/wiki/Crypt_(Unix)"&gt;Wikipedia entry on crypt&lt;/a&gt;. If you&amp;#8217;re implementing any kind of user authentication from scratch, consider using other means of hashing passwords.&lt;/p&gt;</description>
      <pubDate>Sun, 14 Feb 2010 17:48:00 -0000</pubDate>
      <guid isPermaLink="false">urn:uuid:69d660a3-e5cb-4166-8009-e2fe39b9cc87</guid>
      <comments>http://www.weisserth.net/2010/02/14/using-the-mysql-encrypt-function-in-ruby#comments</comments>
      <category>Software Development</category>
      <category>MySQL</category>
      <category>Rails</category>
      <category>Ruby</category>
      <category>encrypt</category>
      <category>hashing</category>
      <category>password</category>
      <category>passwords</category>
      <trackback:ping>http://www.weisserth.net/trackbacks?article_id=11</trackback:ping>
      <link>http://www.weisserth.net/2010/02/14/using-the-mysql-encrypt-function-in-ruby</link>
    </item>
  </channel>
</rss>

