# Salt = \[Array.new(6){rand(256).chr}.join\].pack("m").chomp

**URL:** <https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559>\
**Category:** Ruby\
**Created:** [July 16, 2006, 2:19am UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559 "2006-07-16T02:19:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [July 16, 2006, 2:19am UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559/1 "2006-07-16T02:19:37Z")

</div>

This is a line taken from the “authentification” recipe in the ‘Rails  
Recipes’ book.  
I can see that it generates a string of random characters but I am  
unsure of all that’s going on here.

The strings that are generated have 8 characters, how come when the  
array is initialized with the arg ‘6’ ?

What exactly is .join doing here?

What does .pack(“m”) do?

And why the .chomp? I thought that was a func to remove newline  
characters.

Thanks in advance for the help.

-Alex

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [July 16, 2006, 2:33am UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559/2 "2006-07-16T02:33:51Z")

</div>

To my understanding: creates  
Array.new(6){rand(256).chr} =\> array of 6 random chars  
array.join =\> string of 6 chars  
[array.join] =\> array with 1 string of length 6  
newarray.pack(“m”) =\> encode base64

and the last chomp assures that the last generated char is not a  
newline.

hth (even if I am not completely sure about it, and I would like to  
hear from more experienced guys).

## ./alex

.w( the\_mindstorm )p.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [July 16, 2006, 2:43am UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559/3 "2006-07-16T02:43:22Z")

</div>

On 15-Jul-06, at 8:30 PM, Alexandru P. wrote:

> To my understanding: creates  
> Array.new(6){rand(256).chr} =\> array of 6 random chars  
> array.join =\> string of 6 chars  
> [array.join] =\> array with 1 string of length 6  
> newarray.pack(“m”) =\> encode base64
> 
> and the last chomp assures that the last generated char is not a  
> newline.

Close - the pack(‘m’) always pads its result with = if necessary (to  
get to a multiple of 4 characters for the encoded string) and adds a  
newline. As we start with 6 characters, and base64 encoding turns  
three characters into four, we end up with an 8 character string plus  
a new line. The chomp deletes that newline.

In irb:

ratdog:~ mike$ irb --prompt simple

> > Array.new(6){rand(256).chr}  
> > =\> [“\332”, “w”, “\035”, “!”, “\237”, “\030”]  
> > [“\332”, “w”, “\035”, “!”, “\237”, “\030”].join  
> > =\> “\332w\035!\237\030”  
> > [“\332w\035!\237\030”].pack(‘m’)  
> > =\> “2ncdIZ8Y\n”  
> > “2ncdIZ8Y\n”.chomp  
> > =\> “2ncdIZ8Y”

Mike

> On 7/16/06, Alexandre H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What does .pack(“m”) do?

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [July 16, 2006, 10:47am UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559/4 "2006-07-16T10:47:25Z")

</div>

On 7/16/06, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > newline.
> 
> Close - the pack(‘m’) always pads its result with = if necessary (to  
> get to a multiple of 4 characters for the encoded string) and adds a  
> newline. As we start with 6 characters, and base64 encoding turns  
> three characters into four, we end up with an 8 character string plus  
> a new line. The chomp deletes that newline.

I was quite close… thanks for details Mike :-).

## ./alex

.w( the\_mindstorm )p.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [July 16, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/salt-array-new-6-rand-256-chr-join-pack-m-chomp/98559/5 "2006-07-16T15:03:35Z")

</div>

Much thanks to you two 🙂
