Skip to content

Commit

Permalink
feat: add support for multiple hash algorithms (laravel#43407)
Browse files Browse the repository at this point in the history
  • Loading branch information
medeirosinacio authored and Ken committed Aug 9, 2022
1 parent 0d85a9c commit cc3d870
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
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')
{
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

0 comments on commit cc3d870

Please sign in to comment.