Exemple #1
0
 /**
  * @return Hash
  */
 public function getHash() : Hash
 {
     $hash = new Hash();
     $hash->setAlgorithm($this->getAlgorithm());
     $hash->setValue(hash_final(hash_copy($this->getHandle()), true));
     return $hash;
 }
Exemple #2
0
 /**
  * @param string $data
  * @param string $algorithm
  *
  * @return Hash
  */
 public static function digest(string $data, string $algorithm) : Hash
 {
     $hash = new Hash();
     $hash->setAlgorithm($algorithm);
     $hash->setValue(openssl_digest($data, $algorithm, TRUE));
     return $hash;
 }
Exemple #3
0
 public function testMd5SecureCompare()
 {
     $generator = new HashGenerator('md5');
     $generator->pushFile(__DIR__ . '/message1.bin');
     $hash1 = $generator->getHash();
     $this->assertEquals('008ee33a9d58b51cfeb425b0959121c9', (string) $hash1);
     $generator = new HashGenerator('md5');
     $generator->pushFile(__DIR__ . '/message2.bin');
     $hash2 = $generator->getHash();
     $this->assertEquals('008ee33a9d58b51cfeb425b0959121c9', (string) $hash2);
     $generator = new HashGenerator('md5');
     $generator->push('foobar');
     $hash3 = $generator->getHash();
     $this->assertTrue(Hash::compare($hash1, $hash1));
     $this->assertTrue(Hash::compare($hash1, $hash2));
     $this->assertFalse(Hash::compare($hash1, $hash3));
     $this->assertTrue(Hash::compare($hash2, $hash1));
     $this->assertTrue(Hash::compare($hash2, $hash2));
     $this->assertFalse(Hash::compare($hash2, $hash3));
     $this->assertFalse(Hash::compare($hash3, $hash1));
     $this->assertFalse(Hash::compare($hash3, $hash2));
     $this->assertTrue(Hash::compare($hash3, $hash3));
     $this->assertTrue($hash1->compareWith($hash1));
     $this->assertTrue($hash1->compareWith($hash2));
     $this->assertFalse($hash1->compareWith($hash3));
     $this->assertTrue($hash2->compareWith($hash1));
     $this->assertTrue($hash2->compareWith($hash2));
     $this->assertFalse($hash2->compareWith($hash3));
     $this->assertFalse($hash3->compareWith($hash1));
     $this->assertFalse($hash3->compareWith($hash2));
     $this->assertTrue($hash3->compareWith($hash3));
 }
Exemple #4
0
 public function testOpensslFormat()
 {
     $hash = new Hash('{sha1}b10a8db164e0754105b7a99be72e3fe5');
     $this->assertSame('sha1', $hash->getAlgorithm());
     $this->assertSame('b10a8db164e0754105b7a99be72e3fe5', $hash->getHexValue());
 }
Exemple #5
0
 /**
  * @param string $algorithm
  *
  * @return Hash
  */
 public function getFingerprint(string $algorithm = 'SHA1') : Hash
 {
     $value = openssl_x509_fingerprint($this->getHandle(), $algorithm, TRUE);
     if (!$value) {
         throw new RuntimeException(OpenSSL::getLastError());
     }
     $hash = new Hash($algorithm);
     $hash->setValue($value);
     return $hash;
 }