PHP Classes

File: src/Structure/Node.php

Recommend this page to a friend!
  Packages of Scott Arciszewski   Halite   src/Structure/Node.php   Download  
File: src/Structure/Node.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: Halite
Perform cryptography operations with libsodium
Author: By
Last change:
Date: 10 years ago
Size: 975 bytes
 

Contents

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

class Node
{
    private $data;
    private $hash;
    
    public function __construct(string $data)
    {
        $this->data = $data;
        $this->hash = \Sodium\crypto_generichash($data);
    }

    /**
     * Get the data
     *
     * @return string
     */
    public function getData(): string
    {
        return $this->data;
    }

    /**
     * Get a hash of the data (defaults to hex encoded)
     *
     * @param bool $raw
     * @return string
     */
    public function getHash(bool $raw = false): string
    {
        if ($raw) {
            return $this->hash;
        }
        return \Sodium\bin2hex($this->hash);
    }

    /**
     * Nodes are immutable, but you can create one with extra data.
     *
     * @param string $concat
     * @return Node
     */
    public function getExpandedNode(string $concat): Node
    {
        return new Node($this->data . $concat);
    }
}