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

A

Alexandre Hudelot

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
 
A

Alexandru Popescu

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
 
M

Mike Stok

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
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.



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

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
A

Alexandru Popescu

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.

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

/alex
--
w( the_mindstorm )p.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top