<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.weisserth.net,2005:/tag/mysql</id>
  <link type="text/html" href="http://www.weisserth.net" rel="alternate"/>
  <link type="application/atom+xml" href="http://www.weisserth.net/tag/mysql.atom" rel="self"/>
  <title>www.weisserth.net : Tag mysql, everything about MySQL</title>
  <subtitle type="html">on photography, gear and web technology</subtitle>
  <updated>2011-04-16T21:18:11Z</updated>
  <generator version="5.x" uri="http://www.typosphere.org">Typo</generator>
  <entry>
    <id>tag:www.weisserth.net,2005:Article/11</id>
    <published>2010-02-14T17:48:18Z</published>
    <updated>2011-04-16T21:18:11Z</updated>
    <link type="text/html" href="http://www.weisserth.net/2010/02/14/using-the-mysql-encrypt-function-in-ruby" rel="alternate"/>
    <author>
      <name>polarapfel</name>
    </author>
    <title type="html">Using the MySQL encrypt function in Ruby</title>
    <category label="Software Development" scheme="http://www.weisserth.net/category/software-development" term="software-development"/>
    <category scheme="http://www.weisserth.net/tag/mysql" term="MySQL"/>
    <category scheme="http://www.weisserth.net/tag/rails" term="Rails"/>
    <category scheme="http://www.weisserth.net/tag/ruby" term="Ruby"/>
    <category scheme="http://www.weisserth.net/tag/encrypt" term="encrypt"/>
    <category scheme="http://www.weisserth.net/tag/hashing" term="hashing"/>
    <category scheme="http://www.weisserth.net/tag/password" term="password"/>
    <category scheme="http://www.weisserth.net/tag/passwords" term="passwords"/>
    <content type="html">&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;</content>
  </entry>
</feed>

