Skip to content

Commit 52a6053

Browse files
committed
add sha256() base64Decode() base64Encode()
1 parent e8ec991 commit 52a6053

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Strings.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,44 @@ public function sha1(bool $raw_output = false): self
13121312
return $this;
13131313
}
13141314

1315+
/**
1316+
* Generate a sha256 hash string from the input string.
1317+
*
1318+
* @param string $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. Default is FALSE
1319+
*
1320+
* @return self Returns instance of The Strings class.
1321+
*/
1322+
public function sha256(bool $raw_output = false): self
1323+
{
1324+
$this->string = hash('sha256', $this->string, $raw_output);
1325+
1326+
return $this;
1327+
}
1328+
1329+
/**
1330+
* Encodes data with MIME base64.
1331+
*
1332+
* @return self Returns instance of The Strings class.
1333+
*/
1334+
public function base64Encode(): self
1335+
{
1336+
$this->string = base64_encode($this->string);
1337+
1338+
return $this;
1339+
}
1340+
1341+
/**
1342+
* Decodes data encoded with MIME base64
1343+
*
1344+
* @return self Returns instance of The Strings class.
1345+
*/
1346+
public function base64Decode(): self
1347+
{
1348+
$this->string = base64_decode($this->string);
1349+
1350+
return $this;
1351+
}
1352+
13151353
/**
13161354
* Randomly shuffles a string.
13171355
*

tests/StringsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@
493493
$this->assertEquals(sha1('test'), Strings::create('test')->sha1());
494494
});
495495

496+
test('test sha256() method', function (): void {
497+
$this->assertEquals(hash('sha256', 'test'), Strings::create('test')->sha256());
498+
});
499+
500+
test('test base64Decode() method', function (): void {
501+
$this->assertEquals(base64_decode('test'), Strings::create('test')->base64Decode());
502+
});
503+
504+
test('test base64Encode() method', function (): void {
505+
$this->assertEquals(base64_encode('test'), Strings::create('test')->base64Encode());
506+
});
507+
496508
test('test prepend() method', function (): void {
497509
$this->assertEquals('WORK HARD. PLAY HARD.', Strings::create('PLAY HARD.')->prepend('WORK HARD. '));
498510
});

0 commit comments

Comments
 (0)