Skip to content

[9.x] Add support for multiple hash algorithms #43407

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

Merged
Merged
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
7 changes: 4 additions & 3 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ public function lines($path)
}

/**
* Get the MD5 hash of the file at the given path.
* Get the hash of the file at the given path.
*
* @param string $path
* @param string $algorithm
* @return string
*/
public function hash($path)
public function hash($path, $algorithm = 'md5')
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change to the method signature unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had done PR for version v10, but they asked to do it for v9.
Let me know if I need to change anything.

#43378 (comment)

{
return md5_file($path);
return hash_file($algorithm, $path);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,21 @@ public function testAllFilesReturnsFileInfoObjects()
$this->assertContainsOnlyInstancesOf(SplFileInfo::class, $files->allFiles(self::$tempDir));
}

public function testHash()
public function testHashWithDefaultValue()
{
file_put_contents(self::$tempDir.'/foo.txt', 'foo');
$filesystem = new Filesystem;
$this->assertSame('acbd18db4cc2f85cedef654fccc4a4d8', $filesystem->hash(self::$tempDir.'/foo.txt'));
}

public function testHash()
{
file_put_contents(self::$tempDir.'/foo.txt', 'foo');
$filesystem = new Filesystem;
$this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $filesystem->hash(self::$tempDir.'/foo.txt', 'sha1'));
$this->assertSame('76d3bc41c9f588f7fcd0d5bf4718f8f84b1c41b20882703100b9eb9413807c01', $filesystem->hash(self::$tempDir.'/foo.txt', 'sha3-256'));
}

/**
* @param string $file
* @return int
Expand Down