Shawn
Shawn

Reputation: 115

Split a string into a flat array of characters

I've done plenty of googling and whatnot and can't find quite what I'm looking for...

I am working on tightening up the authentication for my website. I decided to take the user's credentials, and hash/salt the heck out of them. Then store these values in the DB and in user cookies. I modified a script I found on the PHP website and it's working great so far. I noticed however when using array_rand(), that it would select the chars from the predefined string, sequentially. I didn't like that, so I decided to use a shuffle on the array_rand()'d array. Worked great.

Next! I thought it would be clever to turn my user inputted password into an array, then merge that with my salted array! Well, I am having trouble turning my user's password into an array. I want each character in their password to be an array entry. IE, if your password was "cool", the array would be, Array 0 => c 1 => o 2 => o 3 => l, etc etc. I have tried word to split up the string then explode it with the specified break character, that didn't work. I figure I could do something with a for loop, strlen and whatnot, but there HAS to be a more elegant way.

Any suggestions? I'm kind of stumped :( Here is what I have so far, I'm not done with this as I haven't progressed further than the explodey part.

$strings = wordwrap($string, 1, "|");
echo $strings . "<br />";
$stringe = explode("|", $strings, 1);
print_r($stringe);
echo "<br />";
echo "This is the exploded password string, for mixing with salt.<hr />";

Upvotes: 7

Views: 11747

Answers (3)

user836352
user836352

Reputation:

Never, EVER implement (or in this case, design too!) cryptographical algorithms unless you really know what you are doing. If you decide to go ahead and do it anyways, you're putting your website at risk. There's no reason you should have to do this: there is most certainly libraries and/or functions to do all of this sort of thing already.

Upvotes: 3

Dan Smith
Dan Smith

Reputation: 5685

Thanks to PHP's loose typing if you treat the string as an array, php will hapilly do what you would expect. For example:

$string = 'cool';
echo $string[1]; // output's 'o'.

Upvotes: 3

Colum
Colum

Reputation: 3914

The php function you want is str_split

str_split('cool', 1);

And it would return, is used as above

[0] => c
[1] => o
[2] => o
[3] => l

Upvotes: 25

Related Questions