Skip to content
This repository has been archived by the owner on Feb 19, 2025. It is now read-only.

Commit

Permalink
Merge pull request #9 from Roave/sha-encoder
Browse files Browse the repository at this point in the history
SHA encoder
  • Loading branch information
Ocramius authored Jan 27, 2017
2 parents b476e4c + 5f40a51 commit bf21a3f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Encoder/Sha1SumEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Roave\Signature\Encoder;

final class Sha1SumEncoder implements EncoderInterface
{
/**
* {@inheritDoc}
*/
public function encode(string $codeWithoutSignature): string
{
return sha1($codeWithoutSignature);
}

/**
* {@inheritDoc}
*/
public function verify(string $codeWithoutSignature, string $signature): bool
{
return hash_equals($this->encode($codeWithoutSignature), $signature);
}
}
6 changes: 6 additions & 0 deletions test/unit/src/Encoder/Base64EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public function testEncode()
self::assertSame('IA==', $encoder->encode(' '));
self::assertSame('PD9waHA=', $encoder->encode('<?php'));
}

public function testVerify()
{
$value = uniqid('values', true);
self::assertTrue((new Base64Encoder())->verify($value, base64_encode($value)));
}
}
25 changes: 25 additions & 0 deletions test/unit/src/Encoder/Sha1SumEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Roave\SignatureTest\Encoder;

use Roave\Signature\Encoder\Sha1SumEncoder;

/**
* @covers \Roave\Signature\Encoder\Sha1SumEncoder
*/
final class Sha1SumEncoderTest extends \PHPUnit_Framework_TestCase
{
public function testEncode()
{
$value = uniqid('values', true);
self::assertSame(sha1($value), (new Sha1SumEncoder())->encode($value));
}

public function testVerify()
{
$value = uniqid('values', true);
self::assertTrue((new Sha1SumEncoder())->verify($value, sha1($value)));
}
}

0 comments on commit bf21a3f

Please # to comment.