# Blowfish hash

**URL:** <https://rubytalk.org/t/blowfish-hash/60630>\
**Category:** ruby-talk\
**Created:** [27 October 2010 21:50 UTC](https://rubytalk.org/t/blowfish-hash/60630 "2010-10-27T21:50:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Keener](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@James\_Keener](https://rubytalk.org/u/James_Keener)\
**Post date:** [27 October 2010 21:50 UTC](https://rubytalk.org/t/blowfish-hash/60630/1 "2010-10-27T21:50:22Z")

</div>

With the PHP Crypt function  
[[http://php.net/manual/en/function.crypt.php\](http://php.net/manual/en/function.crypt.php%5C)] I can use blowfish to  
create a hash. Is there an equivalent function for ruby? Preferably  
native code, as the cost parameter we use is high (more than my example  
below) and takes about 3 seconds in native code to run.

Before you reply:  
1) Block ciphers can be used as one-way hash functions and vice-verse  
2) The salt does not need to be formatted the same, just the output of  
hashing itself (excluding the salt)

So, for example:

> echo crypt('this is a test', '$2a$12$abcdefghijklmnopqrstuv');

$2a$12$abcdefghijklmnopqrstuulXXDmWA0ghEUfxodWoZCvd8NILoFYg2

Where:  
'12' is the 'base-2 logarithm of the iteration count for the underlying  
Blowfish-based hashing algorithmeter'  
'abcdefghijklmnopqrstuv' is the IV (I think?) in base64 (I'm not 100%  
sure why it only uses to u but fails w/o the v)  
'ulXXDmWA0ghEUfxodWoZCvd8NILoFYg2' is the output of the hashing  
algorithm

> echo crypt('\/\/007 my example rocks because it\'s amazing!!!!!!',

'$2a$12$wellwhatdoyaknowimhere');  
$2a$12$wellwhatdoyaknowimheremd2ausy8yN9LSOrhdYQXUwYi1oQIEzi

Where:  
'12' is the 'base-2 logarithm of the iteration count for the underlying  
Blowfish-based hashing algorithmeter'  
'wellwhatdoyaknowimhere' is the IV (I think?) in base64  
'md2ausy8yN9LSOrhdYQXUwYi1oQIEzi' is the output of the hashing algorithm

Thanks,  
Jim

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Luis1](https://rubytalk.org/u/Luis1)\
**Post date:** [28 October 2010 03:15 UTC](https://rubytalk.org/t/blowfish-hash/60630/2 "2010-10-28T03:15:03Z")

</div>

Have you seen [Simple Blowfish encryption with Ruby](http://philtoland.com/post/807114394/simple-blowfish-encryption-with-ruby)  
or this [http://crypt.rubyforge.org/blowfish.html](http://crypt.rubyforge.org/blowfish.html) ?

> **···**
>
> On Wed, Oct 27, 2010 at 6:50 PM, James Keener \<jimktrains@gmail.com\> wrote:
> 
> > With the PHP Crypt function  
> > [[http://php.net/manual/en/function.crypt.php\](http://php.net/manual/en/function.crypt.php%5C)] I can use blowfish to  
> > create a hash. Is there an equivalent function for ruby? Preferably  
> > native code, as the cost parameter we use is high (more than my example  
> > below) and takes about 3 seconds in native code to run.
> > 
> > Before you reply:  
> > 1) Block ciphers can be used as one-way hash functions and vice-verse  
> > 2) The salt does not need to be formatted the same, just the output of  
> > hashing itself (excluding the salt)
> > 
> > So, for example:  
> > \> echo crypt('this is a test', '$2a$12$abcdefghijklmnopqrstuv');  
> > $2a$12$abcdefghijklmnopqrstuulXXDmWA0ghEUfxodWoZCvd8NILoFYg2
> > 
> > Where:  
> > '12' is the 'base-2 logarithm of the iteration count for the underlying  
> > Blowfish-based hashing algorithmeter'  
> > 'abcdefghijklmnopqrstuv' is the IV (I think?) in base64 (I'm not 100%  
> > sure why it only uses to u but fails w/o the v)  
> > 'ulXXDmWA0ghEUfxodWoZCvd8NILoFYg2' is the output of the hashing  
> > algorithm
> > 
> > \> echo crypt('\/\/007 my example rocks because it\'s amazing!!!!!!',  
> > '$2a$12$wellwhatdoyaknowimhere');  
> > $2a$12$wellwhatdoyaknowimheremd2ausy8yN9LSOrhdYQXUwYi1oQIEzi
> > 
> > Where:  
> > '12' is the 'base-2 logarithm of the iteration count for the underlying  
> > Blowfish-based hashing algorithmeter'  
> > 'wellwhatdoyaknowimhere' is the IV (I think?) in base64  
> > 'md2ausy8yN9LSOrhdYQXUwYi1oQIEzi' is the output of the hashing algorithm
> 
> --  
> Luis Parravicini  
> [http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)
