Skip to content

Commit

Permalink
add sha256() base64Decode() base64Encode()
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Feb 18, 2021
1 parent e8ec991 commit 52a6053
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,44 @@ public function sha1(bool $raw_output = false): self
return $this;
}

/**
* Generate a sha256 hash string from the input string.
*
* @param string $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. Default is FALSE
*
* @return self Returns instance of The Strings class.
*/
public function sha256(bool $raw_output = false): self
{
$this->string = hash('sha256', $this->string, $raw_output);

return $this;
}

/**
* Encodes data with MIME base64.
*
* @return self Returns instance of The Strings class.
*/
public function base64Encode(): self
{
$this->string = base64_encode($this->string);

return $this;
}

/**
* Decodes data encoded with MIME base64
*
* @return self Returns instance of The Strings class.
*/
public function base64Decode(): self
{
$this->string = base64_decode($this->string);

return $this;
}

/**
* Randomly shuffles a string.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,18 @@
$this->assertEquals(sha1('test'), Strings::create('test')->sha1());
});

test('test sha256() method', function (): void {
$this->assertEquals(hash('sha256', 'test'), Strings::create('test')->sha256());
});

test('test base64Decode() method', function (): void {
$this->assertEquals(base64_decode('test'), Strings::create('test')->base64Decode());
});

test('test base64Encode() method', function (): void {
$this->assertEquals(base64_encode('test'), Strings::create('test')->base64Encode());
});

test('test prepend() method', function (): void {
$this->assertEquals('WORK HARD. PLAY HARD.', Strings::create('PLAY HARD.')->prepend('WORK HARD. '));
});
Expand Down

0 comments on commit 52a6053

Please sign in to comment.