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 all 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
18 changes: 9 additions & 9 deletions src/Provider/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,33 +243,33 @@ public static function boolean($chanceOfGettingTrue = 50)
}

/**
* @example 'cfcd208495d565ef66e7dff9f98764da'
* @example '7c2a5276141e81fdf60015689c53d8a1'
*
* @return string
*/
public static function md5()
public static function md5($seed = null)
{
return md5(self::numberBetween());
return md5($seed ?? random_bytes(128 / 8));
}

/**
* @example 'b5d86317c2a144cd04d0d7c03b2b02666fafadf2'
* @example '2d37797087b5130d79e51a08fa7f1cf7678c70ab'
*
* @return string
*/
public static function sha1()
public static function sha1($seed = null)
{
return sha1(self::numberBetween());
return sha1($seed ?? random_bytes(160 / 8));
}

/**
* @example '85086017559ccc40638fcde2fecaf295e0de7ca51b7517b6aebeaaf75b4d4654'
* @example 'a5aa26c454007319b3458f9a9162822c8c1ac5394c4124186b4d54703d9ac3cd'
*
* @return string
*/
public static function sha256()
public static function sha256($seed = null)
{
return hash('sha256', self::numberBetween());
return hash('sha256', $seed ?? random_bytes(256 / 8));
}

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

public function testMd5ExpectedSeed(): void
{
self::assertEquals('7c2a5276141e81fdf60015689c53d8a1', $this->faker->md5('seed_123'));
}

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

public function testSha1ExpectedSeed(): void
{
self::assertEquals('2d37797087b5130d79e51a08fa7f1cf7678c70ab', $this->faker->sha1('seed_123'));
}

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

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

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

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