# De-crypting Ruby password with php

**URL:** <https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337>\
**Category:** Rails\
**Created:** [May 10, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337 "2010-05-10T14:40:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/1 "2010-05-10T14:40:13Z")

</div>

Hello all, I have a client with an older Rails 1.8 app (was recently  
upgraded from 1.6) and I need to integrate a php site to use the same  
user login creds.

I’m not very versed with Ruby but I think this is the code that  
encrypts the password.

[code]

# Encrypts some data with the salt.

def self.encrypt(password, salt)  
Digest::SHA1.hexdigest("–#{salt}–#{password}–")  
end

# Encrypts the password with the user salt

def encrypt(password)  
self.class.encrypt(password, salt)  
end[/code]

So… I have full db access so I have the encrypted passwords and  
their associated salts. And I need a php script to verify users. Is  
there as way for me to un-encrypt this password via php and the  
database that Ruby is using?

Thanks in advance for any guidance or assistance you may be able to  
provide.

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/2 "2010-05-10T14:44:55Z")

</div>

You shouldn’t have to decrypt it. As with the login code in ruby you  
compare hash to hash.

Dieter L.  
[http://www.coder2000.ca](http://www.coder2000.ca)

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/3 "2010-05-10T14:52:33Z")

</div>

exactly, that’s what I meant, sorry. Is this just a sha1() hash? I  
guess I’m not fully understanding what Ruby is doing here.

Digest::SHA1.hexdigest("–#{salt}–#{password}–")

What is ‘hexdigest’?

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 2:54pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/4 "2010-05-10T14:54:18Z")

</div>

It is just an SHA1 hash. the hexdigest part is getting the hex  
representation of that hash.

Dieter L.  
[http://www.coder2000.ca](http://www.coder2000.ca)

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/5 "2010-05-10T15:21:15Z")

</div>

On 10 May 2010 14:04, rusty [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is Ruby seeing
> 
> This is really just my ignorance of how Ruby interprets variables in  
> strings. Sorry in advance for the dumb question.

Try it in the console and see. (ruby script/console)

Colin

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/6 "2010-05-10T15:07:11Z")

</div>

Ok, one last question and I think I’ve got this.

How is Ruby interpreting this string?

“–#{salt}–#{password}–”

For example let’s say…  
salt = 1234  
password = 5678

Is Ruby seeing

“–#{1234}–#{5678}–”

or

“–#1234–#5678–”

or

“–1234–5678–”

This is really just my ignorance of how Ruby interprets variables in  
strings. Sorry in advance for the dumb question.

Thanks for your assistance.

---

<div class="post-metadata">

**Author:** ![rusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f17d59/32.png) [@rusty](https://www.ruby-forum.com/u/rusty)\
**Post date:** [May 10, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/de-crypting-ruby-password-with-php/188337/7 "2010-05-10T16:26:13Z")

</div>

In case you do not have a working Ruby app with a console available  
the answer is: “–1234–5678–”

The document on the digest is avaialble here:  
[http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html)
