Skip to content

Commit

Permalink
Modifed Str::md5(), Str::sha1(), Str::sha256() methods to accept a mo…
Browse files Browse the repository at this point in the history
…de flag
  • Loading branch information
PHLAK committed Jul 8, 2018
1 parent e7aa35e commit 349e856
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 25 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,12 +689,17 @@ $string->crypt('NaCl'); // Returns 'Naq9mOMsN7Yac'
> Calculate the md5 hash of the string.
```php
Twine\Str::md5( [ bool $raw = false ] ) : Twine\Str
Twine\Str::md5( [ bool $mode = Twine\Config\Md5::DEFAULT ] ) : Twine\Str
```

| Parameter | Description |
| --------- | ------------------------------------------- |
| `$raw` | If true, returns the raw binary of the hash |
| Parameter | Description |
| --------- | --------------- |
| `$mode` | A md5 mode flag |

Available md5 modes:

- `Twine\Config\Md5::DEFAULT` - Return the hash
- `Twine\Config\Md5::RAW` - Return the raw binary of the hash

#### Example

Expand All @@ -710,12 +715,17 @@ $string->md5(); // Returns '30cac4703a16a2201ec5cafbd600d803'
> Calculate the sha1 hash of the string.
```php
Twine\Str::sha1( [ bool $raw = false ] ) : Twine\Str
Twine\Str::sha1( [ bool $mode = Twine\Config\Sha1::DEFAULT ] ) : Twine\Str
```

| Parameter | Description |
| --------- | ------------------------------------------- |
| `$raw` | If true, returns the raw binary of the hash |
| Parameter | Description |
| --------- | ---------------- |
| `$mode` | A sha1 mode flag |

Available sha1 mode flags:

- `Twine\Config\Sha1::DEFAULT` - Return the hash
- `Twine\Config\Sha1::RAW` - Return the raw binary of the hash

#### Example

Expand All @@ -731,19 +741,24 @@ $string->sha1(); // Returns 'fcaf28c7705ba8f267472bb5aa8ad883f6bf0427'
> Calculate the sha256 hash of the string.
```php
Twine\Str::sha256( [ bool $raw = false ] ) : Twine\Str
Twine\Str::sha256( [ bool $mode = Twine\Config\Sha256::DEFAULT ] ) : Twine\Str
```

| Parameter | Description |
| --------- | ------------------------------------------- |
| `$raw` | If true, returns the raw binary of the hash |
| Parameter | Description |
| --------- | ------------------ |
| `$mode` | A sha256 mode flag |

Available sha256 mode flags:

- `Twine\Config\Sha256::DEFAULT` - Return the hash
- `Twine\Config\Sha256::RAW` - Return the raw binary of the hash

#### Example

```php
$string = Twine\Str('john pinkerton');

$string->sha1(); // Returns '7434f26c8c2fc83e57347feb2dfb235c2f47b149b54b80692beca9d565159dfd'
$string->sha256(); // Returns '7434f26c8c2fc83e57347feb2dfb235c2f47b149b54b80692beca9d565159dfd'
```

---
Expand Down
9 changes: 9 additions & 0 deletions src/Config/Md5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHLAK\Twine\Config;

final class Md5 extends Config
{
const DEFAULT = false;
const RAW = true;
}
9 changes: 9 additions & 0 deletions src/Config/Sha1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHLAK\Twine\Config;

final class Sha1 extends Config
{
const DEFAULT = false;
const RAW = true;
}
9 changes: 9 additions & 0 deletions src/Config/Sha256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHLAK\Twine\Config;

final class Sha256 extends Config
{
const DEFAULT = false;
const RAW = true;
}
41 changes: 32 additions & 9 deletions src/Traits/Hashable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHLAK\Twine\Traits;

use PHLAK\Twine\Config;

trait Hashable
{
/**
Expand Down Expand Up @@ -30,36 +32,57 @@ public function crypt($salt)
/**
* Calculate the md5 hash of the string.
*
* @param bool $raw If true, returns the raw binary of the hash
* @param bool $mode A md5 mode flag
*
* Available md5 modes:
*
* - Twine\Config\Md5::DEFAULT - Return the hash
* - Twine\Config\Md5::RAW - Return the raw binary of the hash
*
* @return self
*/
public function md5($raw = false)
public function md5($mode = Config\Md5::DEFAULT)
{
return new static(hash('md5', $this->string, $raw));
Config\Md5::validateOption($mode);

return new static(hash('md5', $this->string, $mode));
}

/**
* Calculate the sha1 hash of the string.
*
* @param bool $raw If true, returns the raw binary of the hash
* @param bool $mode A sha1 mode flag
*
* Available sha1 modes:
*
* - Twine\Config\Sha1::DEFAULT - Return the hash
* - Twine\Config\Sha1::RAW - Return the raw binary of the hash
*
* @return self
*/
public function sha1($raw = false)
public function sha1($mode = Config\Md5::DEFAULT)
{
return new static(hash('sha1', $this->string, $raw));
Config\Md5::validateOption($mode);

return new static(hash('sha1', $this->string, $mode));
}

/**
* Calculate the sha256 hash of the string.
*
* @param bool $raw If true, returns the raw binary of the hash
* @param bool $mode A sha256 mode flag
*
* Available sha256 modes:
*
* - Twine\Config\Sha256::DEFAULT - Return the hash
* - Twine\Config\Sha256::RAW - Return the raw binary of the hash
*
* @return self
*/
public function sha256($raw = false)
public function sha256($mode = Config\Sha256::DEFAULT)
{
return new static(hash('sha256', $this->string, $raw));
Config\Sha256::validateOption($mode);

return new static(hash('sha256', $this->string, $mode));
}
}
18 changes: 18 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ public function test_it_has_equals_config_options()
$this->assertEquals(Twine\Config\Equals::CASE_SENSITIVE, 'strcmp');
$this->assertEquals(Twine\Config\Equals::CASE_INSENSITIVE, 'strcasecmp');
}

public function test_it_has_md5_config_options()
{
$this->assertFalse(Twine\Config\Md5::DEFAULT);
$this->assertTrue(Twine\Config\Md5::RAW);
}

public function test_it_has_sha1_config_options()
{
$this->assertFalse(Twine\Config\Sha1::DEFAULT);
$this->assertTrue(Twine\Config\Sha1::RAW);
}

public function test_it_has_sha256_config_options()
{
$this->assertFalse(Twine\Config\Sha256::DEFAULT);
$this->assertTrue(Twine\Config\Sha256::RAW);
}
}
6 changes: 3 additions & 3 deletions tests/HashableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_it_can_be_hashed_with_md5()
$string = new Twine\Str('john pinkerton');

$md5 = $string->md5();
$raw = $string->md5(true);
$raw = $string->md5(Twine\Config\Md5::RAW);

$this->assertInstanceOf(Twine\Str::class, $md5);
$this->assertEquals('30cac4703a16a2201ec5cafbd600d803', $md5);
Expand All @@ -43,7 +43,7 @@ public function test_it_can_be_hashed_with_sha1()
$string = new Twine\Str('john pinkerton');

$sha1 = $string->sha1();
$raw = $string->sha1(true);
$raw = $string->sha1(Twine\Config\Sha1::RAW);

$this->assertInstanceOf(Twine\Str::class, $sha1);
$this->assertEquals('fcaf28c7705ba8f267472bb5aa8ad883f6bf0427', $sha1);
Expand All @@ -55,7 +55,7 @@ public function test_it_can_be_hashed_with_sha256()
$string = new Twine\Str('john pinkerton');

$sha256 = $string->sha256();
$raw = $string->sha256(true);
$raw = $string->sha256(Twine\Config\Sha256::RAW);

$this->assertInstanceOf(Twine\Str::class, $sha256);
$this->assertEquals('7434f26c8c2fc83e57347feb2dfb235c2f47b149b54b80692beca9d565159dfd', $sha256);
Expand Down

0 comments on commit 349e856

Please sign in to comment.