Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jun 23, 2017
0 parents commit 0fd187c
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
20 changes: 20 additions & 0 deletions .travis.yml
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
22 changes: 22 additions & 0 deletions LICENSE
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.

8 changes: 8 additions & 0 deletions README.md
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
26 changes: 26 additions & 0 deletions composer.json
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]"
}
]
}
4 changes: 4 additions & 0 deletions phpunit-autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require 'vendor/autoload.php';

ParagonIE_Sodium_Compat::$fastMult = true;
25 changes: 25 additions & 0 deletions phpunit.xml.dist
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>
9 changes: 9 additions & 0 deletions psalm.xml
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>
103 changes: 103 additions & 0 deletions src/Blakechain.php
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];
}
}
89 changes: 89 additions & 0 deletions src/Node.php
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;
}
}
Loading

0 comments on commit 0fd187c

Please sign in to comment.