-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0fd187c
Showing
14 changed files
with
596 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
language: php | ||
sudo: false | ||
|
||
matrix: | ||
fast_finish: true | ||
include: | ||
- php: "7.0" | ||
- php: "7.1" | ||
- php: "nightly" | ||
allow_failures: | ||
- php: "nightly" | ||
|
||
install: | ||
- composer self-update | ||
- composer update | ||
|
||
script: | ||
- vendor/bin/phpunit --bootstrap=phpunit-autoload.php | ||
- php -dmbstring.func_overload=7 vendor/bin/phpunit --bootstrap=phpunit-autoload.php | ||
- vendor/bin/psalm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Paragon Initiative Enterprises | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Blakechain | ||
|
||
Blakechain is a simple hash-chain data structure based on the BLAKE2b hash function. | ||
|
||
Includes: | ||
|
||
* The `Blakechain` implementation, which chains together `Node` objects | ||
* A runtime `Verifier` class that validates the self-consistency of an entire chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "paragonie/blakechain", | ||
"description": "Hash chain using BLAKE2b", | ||
"type": "library", | ||
"require": { | ||
"php": "^7", | ||
"paragonie/constant_time_encoding": "^2", | ||
"paragonie/sodium_compat": "^1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6", | ||
"vimeo/psalm": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"ParagonIE\\Blakechain\\": "src" | ||
} | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Paragon Initiative Enterprises", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
ParagonIE_Sodium_Compat::$fastMult = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
backupGlobals="true" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnError="false" | ||
stopOnFailure="false" | ||
syntaxCheck="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit Tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
stopOnFirstError="false" | ||
useDocblockTypes="true" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
</projectFiles> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace ParagonIE\Blakechain; | ||
|
||
/** | ||
* Class Blakechain | ||
* @package ParagonIE\Blakechain | ||
*/ | ||
class Blakechain | ||
{ | ||
const HASH_SIZE = 32; | ||
|
||
/** | ||
* @var array<int, Node> | ||
*/ | ||
protected $nodes; | ||
|
||
/** | ||
* Blakechain constructor. | ||
* @param array<int, Node> $nodes | ||
* @throws \Error | ||
*/ | ||
public function __construct(Node ...$nodes) | ||
{ | ||
$num = \count($nodes); | ||
if ($num < 1) { | ||
throw new \Error('Nodes expected.'); | ||
} | ||
$prevHash = ''; | ||
for ($i = 0; $i < $num; ++$i) { | ||
$thisNodesPrev = $nodes[$i]->getPrevHash(); | ||
if (empty($thisNodesPrev)) { | ||
$nodes[$i]->setPrevHash($prevHash); | ||
} | ||
$prevHash = $nodes[$i]->getHash(true); | ||
} | ||
$this->nodes = $nodes; | ||
} | ||
|
||
/** | ||
* @param bool $rawBinary | ||
* @return string | ||
*/ | ||
public function getLastHash(bool $rawBinary = false): string | ||
{ | ||
return $this->getLastNode()->getHash($rawBinary); | ||
} | ||
|
||
/** | ||
* Append a new Node. | ||
* | ||
* @param string $data | ||
* @return self | ||
*/ | ||
public function appendData(string $data): self | ||
{ | ||
$last = $this->getLastNode(); | ||
$prevHash = $last->getHash(true); | ||
$this->nodes[] = new Node($data, $prevHash); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<int, Node> | ||
*/ | ||
public function getNodes(): array | ||
{ | ||
return \array_values($this->nodes); | ||
} | ||
|
||
/** | ||
* @param int $offset | ||
* @param int $limit | ||
* @return array | ||
*/ | ||
public function getPartialChain(int $offset = 0, int $limit = PHP_INT_MAX): array | ||
{ | ||
$chain = []; | ||
$num = \count($this->nodes); | ||
for ($i = 0; $i < $limit && $i < $num; ++$i) { | ||
$chain[] = [ | ||
'prev' => $this->nodes[$offset]->getPrevHash(), | ||
'data' => $this->nodes[$offset]->getData(), | ||
'hash' => $this->nodes[$offset]->getHash() | ||
]; | ||
++$offset; | ||
} | ||
return $chain; | ||
} | ||
|
||
/** | ||
* @return Node | ||
*/ | ||
public function getLastNode(): Node | ||
{ | ||
if (empty($this->nodes)) { | ||
throw new \Error('Blakechain has no nodes'); | ||
} | ||
$keys = \array_keys($this->nodes); | ||
$last = \array_pop($keys); | ||
return $this->nodes[$last]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace ParagonIE\Blakechain; | ||
|
||
use ParagonIE\ConstantTime\Base64UrlSafe; | ||
|
||
/** | ||
* Class Node | ||
* @package ParagonIE\Blakechain | ||
*/ | ||
class Node | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $prevHash; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $hash; | ||
|
||
/** | ||
* Node constructor. | ||
* | ||
* @param string $data | ||
* @param string $prevHash | ||
*/ | ||
public function __construct(string $data, string $prevHash = '') | ||
{ | ||
$this->data = $data; | ||
$this->prevHash = $prevHash; | ||
} | ||
|
||
/** | ||
* @param bool $rawBinary | ||
* @return string | ||
*/ | ||
public function getPrevHash(bool $rawBinary = false): string | ||
{ | ||
if ($rawBinary) { | ||
return $this->prevHash; | ||
} | ||
return Base64UrlSafe::encode($this->prevHash); | ||
} | ||
|
||
/** | ||
* @param bool $rawBinary | ||
* @return string | ||
*/ | ||
public function getHash(bool $rawBinary = false): string | ||
{ | ||
if (empty($this->hash)) { | ||
$this->hash = \ParagonIE_Sodium_Compat::crypto_generichash( | ||
$this->data, | ||
$this->prevHash, | ||
Blakechain::HASH_SIZE | ||
); | ||
} | ||
if ($rawBinary) { | ||
return $this->hash; | ||
} | ||
return Base64UrlSafe::encode($this->hash); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getData(): string | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* @param string $prevHash | ||
* @return self | ||
*/ | ||
public function setPrevHash(string $prevHash): self | ||
{ | ||
$this->prevHash = $prevHash; | ||
$this->hash = ''; | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.