David Nap
David Nap

Reputation: 11

PHP Encryption, encrypts with empty line in mind

I'm working with some encryption in PHP that's being accessed by some c# code,

When I sha1 Encrypt the following: test1:test1:apple, every online generator will return: 8ee27a0e9368d2835b3fdeb4b50caf1d8f790314 However, when I run my PHP script it returns 296b39344a6eb9d88c3bb1122f5941f0bcf3b0c2 instead. Because essentially it's adding an empty line along with the string (test1:test1:apple) that I'm encrypting.

Does anyone have any idea how to fix this? It's not neccesarily the worst thing in the world, it's just extremely annoying.

The code I'm using is pretty simple:

function GetRandomWord()
{
$file = "../Core/nouns0.txt";
$file_arr = file($file);
$num_lines = count($file_arr);
$last_index = $num_lines -1;

$rand_index = rand(0, $last_index);
$rand_text = $file_arr[$rand_index];

return $rand_text;
}

$enc = GetRandomWord();
$encrypted = sha1("$username:$password:$enc");
echo $encrypted;

which gets caught in c# by a script.

Thanks in advance!

Upvotes: 0

Views: 45

Answers (2)

David Nap
David Nap

Reputation: 11

@IłyaBursov their answer helped! I changed the GetRandomWord() function's return statement to return trim($rand_text);

Thanks!

Upvotes: 1

miken32
miken32

Reputation: 42714

file() will read each line of a file into an array. It reads the entire line, including the linefeed at the end. If you don't want this behaviour, use the second argument to say so:

$file_arr = file($file, FILE_IGNORE_NEW_LINES);

Upvotes: 0

Related Questions