Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #759 : The output of the hash generators is misleading #880

Open
wants to merge 3 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Provider/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,30 @@ public static function boolean($chanceOfGettingTrue = 50)
return self::numberBetween(1, 100) <= $chanceOfGettingTrue;
}

/**
* Return a string representing a big number, which size is $n * 4 bytes ($n * 32 bits)
*
* @param int $n multiplier for numberBetween.
*
* @return string
*
* @example "38921908753913698485999935526730716"
*/
public static function multiplyNumberBetweenNTimes($n)
{
return array_reduce(range(1, $n), static function ($carry, $item) {
return bcmul($carry, self::numberBetween());
}, '1');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is heavily biased, which is easy to see by performing an exhaustive search for a smaller number range:

<?php
for ($a = 0; $a < 16; $a++) 
for ($b = 0; $b < 16; $b++) 
for ($c = 0; $c < 16; $c++) 
@$results[$a * $b * $c]++;

var_dump($results);

If any of the multiplied numbers is zero, the result will also be zero, which makes zero the most likely result. But the others are not uniformly distributed either:

https://3v4l.org/VJnZK

}

/**
* @example 'cfcd208495d565ef66e7dff9f98764da'
*
* @return string
*/
public static function md5()
{
return md5(self::numberBetween());
return md5(self::multiplyNumberBetweenNTimes(128 / 32));
}

/**
Expand All @@ -259,7 +275,7 @@ public static function md5()
*/
public static function sha1()
{
return sha1(self::numberBetween());
return sha1(self::multiplyNumberBetweenNTimes(160 / 32));
}

/**
Expand All @@ -269,7 +285,7 @@ public static function sha1()
*/
public static function sha256()
{
return hash('sha256', self::numberBetween());
return hash('sha256', self::multiplyNumberBetweenNTimes(256 / 32));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/Provider/MiscellaneousTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,34 @@ public function testMd5(): void
self::assertMatchesRegularExpression('/^[a-z0-9]{32}$/', Miscellaneous::md5());
}

public function testMd5ExpectedSeed(): void
{
$this->faker->seed(252);
self::assertEquals('fa72146b50f73db08c92053d1fc5f263', $this->faker->md5());
}

public function testSha1(): void
{
self::assertMatchesRegularExpression('/^[a-z0-9]{40}$/', Miscellaneous::sha1());
}

public function testSha1ExpectedSeed(): void
{
$this->faker->seed(252);
self::assertEquals('5ecb7a1b22291b6b15e534852e03ac72f4187a48', $this->faker->sha1());
}

public function testSha256(): void
{
self::assertMatchesRegularExpression('/^[a-z0-9]{64}$/', Miscellaneous::sha256());
}

public function testSha256ExpectedSeed(): void
{
$this->faker->seed(252);
self::assertEquals('ea98b35136e020d9dd9b594252d9263021d1742da2bd4c8592b854c62b4122c2', $this->faker->sha256());
}

public function testLocale(): void
{
self::assertMatchesRegularExpression('/^[a-z]{2,3}_[A-Z]{2}$/', Miscellaneous::locale());
Expand Down Expand Up @@ -61,4 +79,9 @@ public function testEmoji(): void
{
self::assertMatchesRegularExpression('/^[\x{1F600}-\x{1F637}]$/u', Miscellaneous::emoji());
}

protected function getProviders(): iterable
{
yield new Miscellaneous($this->faker);
}
}