PHP Classes

File: src/Node.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PHP Blake Chain   src/Node.php   Download  
File: src/Node.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Blake Chain
Create and verify chained blocks of hashed data
Author: By
Last change:
Date: 4 years ago
Size: 1,795 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Blakechain;

use
ParagonIE_Sodium_Compat as SodiumCompat;
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
     *
     * @throws \SodiumException
     */
   
public function getHash(bool $rawBinary = false): string
   
{
        if (empty(
$this->hash)) {
           
$this->hash = SodiumCompat::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;
    }
}