Skip to content

Commit 990f43e

Browse files
feat: add support for multiple hash algorithms (#43407)
1 parent 49b63d6 commit 990f43e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Illuminate/Filesystem/Filesystem.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,15 @@ public function lines($path)
164164
}
165165

166166
/**
167-
* Get the MD5 hash of the file at the given path.
167+
* Get the hash of the file at the given path.
168168
*
169169
* @param string $path
170+
* @param string $algorithm
170171
* @return string
171172
*/
172-
public function hash($path)
173+
public function hash($path, $algorithm = 'md5')
173174
{
174-
return md5_file($path);
175+
return hash_file($algorithm, $path);
175176
}
176177

177178
/**

tests/Filesystem/FilesystemTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,21 @@ public function testAllFilesReturnsFileInfoObjects()
610610
$this->assertContainsOnlyInstancesOf(SplFileInfo::class, $files->allFiles(self::$tempDir));
611611
}
612612

613-
public function testHash()
613+
public function testHashWithDefaultValue()
614614
{
615615
file_put_contents(self::$tempDir.'/foo.txt', 'foo');
616616
$filesystem = new Filesystem;
617617
$this->assertSame('acbd18db4cc2f85cedef654fccc4a4d8', $filesystem->hash(self::$tempDir.'/foo.txt'));
618618
}
619619

620+
public function testHash()
621+
{
622+
file_put_contents(self::$tempDir.'/foo.txt', 'foo');
623+
$filesystem = new Filesystem;
624+
$this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $filesystem->hash(self::$tempDir.'/foo.txt', 'sha1'));
625+
$this->assertSame('76d3bc41c9f588f7fcd0d5bf4718f8f84b1c41b20882703100b9eb9413807c01', $filesystem->hash(self::$tempDir.'/foo.txt', 'sha3-256'));
626+
}
627+
620628
/**
621629
* @param string $file
622630
* @return int

0 commit comments

Comments
 (0)