8000 [12.x] Add `hash` string helper by istiak-tridip · Pull Request #55767 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add hash string helper #55767

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 3 commits into from
May 19, 2025
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
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,17 @@ public function fromBase64($strict = false)
return new static(base64_decode($this->value, $strict));
}

/**
* Hash the string using the given algorithm.
*
* @param string $algorithm
* @return static
*/
public function hash(string $algorithm)
{
return new static(hash($algorithm, $this->value));
}

/**
* Dump the string.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,4 +1419,11 @@ public function testFromBase64()
$this->assertSame('foobar', (string) $this->stringable(base64_encode('foobar'))->fromBase64(true));
$this->assertSame('foobarbaz', (string) $this->stringable(base64_encode('foobarbaz'))->fromBase64());
}

public function testHash()
{
$this->assertSame(hash('xxh3', 'foo'), (string) $this->stringable('foo')->hash('xxh3'));
$this->assertSame(hash('xxh3', 'foobar'), (string) $this->stringable('foobar')->hash('xxh3'));
$this->assertSame(hash('sha256', 'foobarbaz'), (string) $this->stringable('foobarbaz')->hash('sha256'));
}
}
Loading
0