Skip to content

Commit

Permalink
Add static helper function for hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
mleczakm committed Jul 20, 2024
1 parent a4b64e5 commit 9c2f16a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/core/etl/src/Flow/ETL/Function/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Flow\ETL\Function;

use Flow\ETL\Hash\Algorithm;
use Flow\ETL\Hash\{Algorithm, NativePHPHash};
use Flow\ETL\Row;

final class Hash extends ScalarFunctionChain
{
public function __construct(
private readonly ScalarFunction $ref,
private readonly Algorithm $algorithm,
private readonly Algorithm $algorithm = new NativePHPHash(),
) {

}

public function eval(Row $row) : ?string
Expand Down
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Hash/NativePHPHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public function __construct(private string $algorithm = 'xxh128', private bool $
}
}

public static function xxh128(string $string) : string
{
return (new self('xxh128'))->hash($string);
}

public function hash(string $value) : string
{
return \hash($this->algorithm, $value, $this->binary, $this->options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function test_hashing_array_value() : void
public function test_hashing_concat() : void
{
self::assertSame(
(new NativePHPHash('xxh128'))->hash('test_test'),
NativePHPHash::xxh128('test_test'),
hash(concat(ref('value'), lit('_'), ref('value')), new NativePHPHash('xxh128'))->eval(Row::create(str_entry('value', 'test')))
);
}
Expand Down
36 changes: 36 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Hash/NativePHPHashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Hash;

use Flow\ETL\Hash\NativePHPHash;
use PHPUnit\Framework\TestCase;

class NativePHPHashTest extends TestCase
{
public static function test_hashing_xxh128_by_static_call() : void
{
static::assertSame(
'6c78e0e3bd51d358d01e758642b85fb8',
NativePHPHash::xxh128('test'),
);
}

public function test_hashing_string_using_xxh128_by_default() : void
{
static::assertSame(
'6c78e0e3bd51d358d01e758642b85fb8',
NativePHPHash::xxh128('test'),
);
}

public function test_support_sha512_hash() : void
{
static::assertSame(
'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff',
(new NativePHPHash('sha512'))->hash('test')
);

}
}

0 comments on commit 9c2f16a

Please sign in to comment.