Skip to content

Commit e8ec991

Browse files
committed
add sha1() and md5() methods
1 parent 1525594 commit e8ec991

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Strings.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,34 @@ public function crc32(): int
12841284
return crc32($this->string);
12851285
}
12861286

1287+
/**
1288+
* Generate a md5 hash string from the input string.
1289+
*
1290+
* @param string $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. Default is FALSE
1291+
*
1292+
* @return self Returns instance of The Strings class.
1293+
*/
1294+
public function md5(bool $raw_output = false): self
1295+
{
1296+
$this->string = hash('md5', $this->string, $raw_output);
1297+
1298+
return $this;
1299+
}
1300+
1301+
/**
1302+
* Generate a sha1 hash string from the input string.
1303+
*
1304+
* @param string $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. Default is FALSE
1305+
*
1306+
* @return self Returns instance of The Strings class.
1307+
*/
1308+
public function sha1(bool $raw_output = false): self
1309+
{
1310+
$this->string = hash('sha1', $this->string, $raw_output);
1311+
1312+
return $this;
1313+
}
1314+
12871315
/**
12881316
* Randomly shuffles a string.
12891317
*

tests/StringsTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@
485485
$this->assertEquals(crc32('test'), Strings::create('test')->crc32());
486486
});
487487

488+
test('test md5() method', function (): void {
489+
$this->assertEquals(md5('test'), Strings::create('test')->md5());
490+
});
491+
492+
test('test sha1() method', function (): void {
493+
$this->assertEquals(sha1('test'), Strings::create('test')->sha1());
494+
});
495+
488496
test('test prepend() method', function (): void {
489497
$this->assertEquals('WORK HARD. PLAY HARD.', Strings::create('PLAY HARD.')->prepend('WORK HARD. '));
490498
});

0 commit comments

Comments
 (0)