Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhtrang committed Jan 1, 2020
0 parents commit 6e30b90
Show file tree
Hide file tree
Showing 26 changed files with 894 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
composer.lock
.php_cs.cache
.idea/
.phpunit.result.cache
21 changes: 21 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

if (PHP_SAPI !== 'cli') {
die('This script supports command line usage only. Please check your command.');
}

$finder = \PhpCsFixer\Finder::create()
->exclude([
'vendor', 'build'
])
->in(__DIR__);

return \PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'escape_implicit_backslashes' => [
'single_quoted' => true
]
])
->setFinder($finder);
47 changes: 47 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
language: php
dist: trusty
php:
- 7.2
- 7.3
- 7.4
stages:
- lint
- test
- report
before_install:
- openssl aes-256-cbc -K $encrypted_589568f37856_key -iv $encrypted_589568f37856_iv -in secrets.tar.enc -out secrets.tar -d
- tar xvf secrets.tar -C tests/fixtures
- pecl install grpc
install:
- composer update --no-progress --no-interaction --no-suggest

script:
- vendor/bin/phpunit --testsuite=unit

jobs:
include:
- stage: lint
name: Code style check
php: '7.2'
script:
- vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation
- stage: test
name: Unit test
script:
- vendor/bin/phpunit --testsuite=integration
- stage: report
name: Code coverage
php: '7.2'
git:
depth: false
script:
- vendor/bin/phpunit --coverage-clover=coverage.xml --log-junit=test-results.xml
- sonar-scanner
branches:
only:
- master
- develop
cache:
directories:
- vendor
- "$HOME/.composer/cache"
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "firebase-php/hash",
"description": "Hash algorithm identifiers used by Firebase",
"homepage": "https://github.com/firebase-php/firebase-hash",
"keywords": [
"firebase",
"php",
"hash algorithms"
],
"type": "library",
"require": {
"php": ">= 7.2"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"friendsofphp/php-cs-fixer": "^2.16"
},
"license": "MIT",
"authors": [
{
"name": "Trang Nguyen",
"email": "hongtrang2203@gmail.com"
}
],
"autoload": {
"psr-4": {
"FirebaseHash\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"FirebaseHash\\Tests\\": "tests"
}
}
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<php>
<ini name="date.timezone" value="UTC" />
</php>
</phpunit>
17 changes: 17 additions & 0 deletions src/Bcrypt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace FirebaseHash;

class Bcrypt implements Hashable
{
public function getOptions()
{
return [];
}

public function getName()
{
return 'BCRYPT';
}
}
11 changes: 11 additions & 0 deletions src/Hashable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace FirebaseHash;

interface Hashable
{
public function getOptions();

public function getName();
}
38 changes: 38 additions & 0 deletions src/Hmac.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace FirebaseHash;

abstract class Hmac implements Hashable
{
private $key;

private $name;

public function __construct(string $name, HmacBuilder $builder)
{
$this->name = $name;

if (is_null($builder->getKey()) || strlen($builder->getKey()) === 0) {
throw new \InvalidArgumentException('A non-empty key is required for HMAC algorithm');
}
$this->key = base64_encode($builder->getKey());
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

abstract public static function builder();

public function getOptions()
{
return [
'signerKey' => $this->key
];
}
}
32 changes: 32 additions & 0 deletions src/HmacBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace FirebaseHash;

abstract class HmacBuilder
{
/**
* @var string|null
*/
private $key;

/**
* @return string|null
*/
public function getKey(): ?string
{
return $this->key;
}

/**
* @param string|null $key
* @return HmacBuilder
*/
public function setKey(string $key): HmacBuilder
{
$this->key = $key;
return $this;
}

abstract public function build();
}
22 changes: 22 additions & 0 deletions src/HmacMd5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FirebaseHash;

class HmacMd5 extends Hmac
{
public function __construct($builder)
{
parent::__construct('HMAC_MD5', $builder);
}

public static function builder()
{
return new class extends HmacBuilder {
public function build()
{
return new HmacMd5($this);
}
};
}
}
22 changes: 22 additions & 0 deletions src/HmacSha1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FirebaseHash;

class HmacSha1 extends Hmac
{
public function __construct($builder)
{
parent::__construct('HMAC_SHA1', $builder);
}

public static function builder()
{
return new class extends HmacBuilder {
public function build()
{
return new HmacSha1($this);
}
};
}
}
22 changes: 22 additions & 0 deletions src/HmacSha256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FirebaseHash;

class HmacSha256 extends Hmac
{
public function __construct($builder)
{
parent::__construct('HMAC_SHA256', $builder);
}

public static function builder()
{
return new class extends HmacBuilder {
public function build()
{
return new HmacSha256($this);
}
};
}
}
21 changes: 21 additions & 0 deletions src/HmacSha512.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FirebaseHash;

class HmacSha512 extends Hmac
{
public function __construct($builder)
{
parent::__construct('HMAC_SHA512', $builder);
}

public static function builder()
{
return new class extends HmacBuilder {
public function build()
{
return new HmacSha512($this);
}
};
}
}
22 changes: 22 additions & 0 deletions src/Md5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FirebaseHash;

class Md5 extends RepeatableHash
{
public function __construct($builder)
{
parent::__construct('MD5', 0, 8192, $builder);
}

public static function builder()
{
return new class extends RepeatableHashBuilder {
public function build()
{
return new Md5($this);
}
};
}
}
22 changes: 22 additions & 0 deletions src/Pbkdf2Sha256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FirebaseHash;

class Pbkdf2Sha256 extends RepeatableHash
{
public function __construct($builder)
{
parent::__construct('PBKDF2_SHA256', 0, 120000, $builder);
}

public static function builder()
{
return new class extends RepeatableHashBuilder {
public function build()
{
return new Pbkdf2Sha256($this);
}
};
}
}
Loading

0 comments on commit 6e30b90

Please sign in to comment.