Mix two strings into one long PHP string

I have two strings and I would like to mix characters from each string into one big string, how can I do this in PHP? I can swap the symbols, but I need something more complex as one would guess.

And please don't say that md5 () is sufficient and irreversible. :)

$string1 = '9cb5jplgvsiedji9mi9o6a8qq1';//session_id()
$string2 = '5d41402abc4b2a76b9719d911017c592';//md5()

      

Thanks for any help.

EDIT: Ah, sorry Rob. It would be great if there was a solution where it was just a function that I could pass two strings with and it returned a string.

The returned string must contain both previous lines. Not only concatenation, but the characters of each string are mixed into one big one.

0


source to share


7 replies


If you want to make a tamper-proof string that is human readable, add a secure hash to it. MD5 is really going to be phased out, so try sha1 . for example

$salt="secret";
$hash=sha1($string1.$string2.$salt);
$separator="_";
$str=$string1.$separator.$string2.$separator.$hash;

      



If you need a string that cannot be read by humans, encrypt it - check out the mcrypt extension , which offers many options.

+4


source


Use one of the SHA options hash () . Sha2 or sha256 should be sufficient and is definitely much better than anything you could think of.



+4


source


If I'm missing something, if you want to concatenate these values ​​into a unique value, why not do sha1 (string1, string2);

+2


source


I am assuming that you want something reversible so that you can get those values ​​back. A quick and dirty technique for shading these two lines further would be to base64 them:

base64_encode($string1 . $string2);

      

0


source


Thanks everyone. I completely forgot about SHA1 - I also tackled the problem that I forgot what else was there. :)

0


source


Well, if not md5 (), then sha1 (). :)

Anyway, the possibilities to maim are endless, take your poison.

What I would do, if I really wanted to do something like this (which might be useful sometimes), I would add another item, selected at random and shuffled with the md5 string. and write a random element into it.

For example, let's add a random 2-digit number to each md5 character, which we then divide into digits and add the 1st digit to the resulting string, and add the second digit to it.

I stumbled upon the place where something was done today. I was trying to find some link to a specific phone number - whether it appears somewhere in the local country or not.

I visited a popular ad site that lists advertiser numbers and you have the option when you watch a specific ad to find all ads with the same phone number. However, what they did was they encoded a search string, so you don't search? Phone = 123123, but something like? Phone == FFYx23 =.

If they didn't, I would be able to find out my goals and not check ads if the user with the phone number 123123 placed any ads on the site.

0


source


If you want to check the integrity and authenticity of messages using hashing - you can have a look at HMAC - there are many implementations in PHP using SHA1 and MD5:

http://en.wikipedia.org/wiki/HMAC

EDIT: actually PHP now has a function to do this:

http://us3.php.net/manual/en/function.hash-hmac.php

0


source







All Articles