Skip to content

Commit

Permalink
Add md5, sha1 and sha256
Browse files Browse the repository at this point in the history
Improve demo
  • Loading branch information
8ctopus committed Jan 12, 2024
1 parent b9e5b5b commit 3284a09
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ echo "\nInvert Parrot\n";

echo $parrot->invert();
// hex (0/6): 746f7272 6150 - torraP

echo "\nCalculate hashes\n";
echo 'md5: ' . $parrot->md5(false) . "\n";
echo 'sha1: ' . $parrot->sha1(false) . "\n";
echo 'sha256: ' . $parrot->sha256(false) . "\n";
// md5: 4264ed2a05f9548fb3b26601c1c904c4
// sha1: d50da4682cdb41c803075168f5c132fd33ffa34d
// sha256: e2d6ddb19ca9c3396521297f4a09581c1187acb29931c8d4431941be71d2215c

echo "\nTruncate buffer\n";

echo $parrot->truncate();
// hex (0/0):
```

## run tests
Expand Down
9 changes: 9 additions & 0 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@
echo "\nInvert Parrot\n";

echo $parrot->invert();

echo "\nCalculate hashes\n";
echo 'md5: ' . $parrot->md5(false) . "\n";
echo 'sha1: ' . $parrot->sha1(false) . "\n";
echo 'sha256: ' . $parrot->sha256(false) . "\n";

echo "\nTruncate buffer\n";

echo $parrot->truncate();
36 changes: 36 additions & 0 deletions src/ByteBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,42 @@ public function crc32b(bool $asString = false) : int|string
return $asString ? dechex($crc) : $crc;
}

/**
* Calculate md5 hash
*
* @param bool $binary
*
* @return string
*/
public function md5(bool $binary = false) : string
{
return md5($this->data, $binary);
}

/**
* Calculate sha1 hash
*
* @param bool $binary
*
* @return string
*/
public function sha1(bool $binary = false) : string
{
return sha1($this->data, $binary);
}

/**
* Calculate sha256 hash
*
* @param bool $binary
*
* @return string
*/
public function sha256(bool $binary = false) : string
{
return hash('sha256', $this->data, $binary);
}

public function offsetGet(mixed $offset) : int
{
if (gettype($offset) !== 'integer') {
Expand Down
17 changes: 17 additions & 0 deletions tests/ByteBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ public function testCrc() : void
self::assertSame(hash('crc32b', $str, false), $buffer->crc32b(true));
}

public function testHashes() : void
{
$str = 'abcdefghijklmnopqrstuvwxyz0123456789';

$buffer = (new ByteBuffer())
->setEndian(Endian::LittleEndian)
->writeChars($str);

self::assertSame(hash('sha1', $str, false), $buffer->sha1(false));
self::assertSame(hash('sha256', $str, false), $buffer->sha256(false));
self::assertSame(hash('md5', $str, false), $buffer->md5(false));

self::assertSame(hash('sha1', $str, true), $buffer->sha1(true));
self::assertSame(hash('sha256', $str, false), $buffer->sha256(false));
self::assertSame(hash('md5', $str, true), $buffer->md5(true));
}

public function testDelete() : void
{
$str = 'abcdefghijklmnopqrstuvwxyz0123456789';
Expand Down

0 comments on commit 3284a09

Please sign in to comment.