Skip to content

Commit a720cbe

Browse files
[12.x] Add hash string helper (#55767)
* feat: add `hash` method to `Stringable` * fix: cs * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 40edd68 commit a720cbe

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,17 @@ public function fromBase64($strict = false)
13351335
return new static(base64_decode($this->value, $strict));
13361336
}
13371337

1338+
/**
1339+
* Hash the string using the given algorithm.
1340+
*
1341+
* @param string $algorithm
1342+
* @return static
1343+
*/
1344+
public function hash(string $algorithm)
1345+
{
1346+
return new static(hash($algorithm, $this->value));
1347+
}
1348+
13381349
/**
13391350
* Dump the string.
13401351
*

tests/Support/SupportStringableTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,4 +1419,11 @@ public function testFromBase64()
14191419
$this->assertSame('foobar', (string) $this->stringable(base64_encode('foobar'))->fromBase64(true));
14201420
$this->assertSame('foobarbaz', (string) $this->stringable(base64_encode('foobarbaz'))->fromBase64());
14211421
}
1422+
1423+
public function testHash()
1424+
{
1425+
$this->assertSame(hash('xxh3', 'foo'), (string) $this->stringable('foo')->hash('xxh3'));
1426+
$this->assertSame(hash('xxh3', 'foobar'), (string) $this->stringable('foobar')->hash('xxh3'));
1427+
$this->assertSame(hash('sha256', 'foobarbaz'), (string) $this->stringable('foobarbaz')->hash('sha256'));
1428+
}
14221429
}

0 commit comments

Comments
 (0)