1<?php
2/*
3 * Copyright 2008-2010 GuardTime AS
4 *
5 * This file is part of the GuardTime PHP SDK.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @package tsp
22 */
23
24/**
25 * Object representing a single entry in a GTHashChain.
26 * @package tsp
27 */
28class GTHashEntry {
29
30    private $hashAlgorithm;
31    private $direction;
32    private $siblingHash;
33    private $level;
34
35    /**
36     * Construct a new GTHashEntry instance.
37     *
38     * @param  GTHashAlgorithm $hashAlgorithm
39     * @param  int $direction
40     * @param  GTDataHash $siblingHash
41     * @param  int $level
42     */
43    public function __construct(GTHashAlgorithm $hashAlgorithm, $direction, GTDataHash $siblingHash, $level) {
44
45        $this->hashAlgorithm = $hashAlgorithm;
46        $this->direction = $direction;
47        $this->siblingHash = $siblingHash;
48        $this->level = $level;
49
50    }
51
52    /**
53     * Computes the output of this hash entry with given bytes.
54     *
55     * @param  array $bytes bytes to compute output for
56     * @return array the result
57     */
58    public function computeOutput(array $bytes) {
59
60        $hash = new GTDataHash($this->hashAlgorithm);
61
62        $hash->update($bytes);
63        $hash->close();
64
65        $imprint1 = $hash->getDataImprint();
66        $imprint2 = $this->siblingHash->getDataImprint();
67
68        $output = array();
69
70        if ($this->direction == 0) {
71
72            foreach ($imprint2 as $byte) {
73                array_push($output, $byte);
74            }
75
76            foreach ($imprint1 as $byte) {
77                array_push($output, $byte);
78            }
79
80        } else {
81
82            foreach ($imprint1 as $byte) {
83                array_push($output, $byte);
84            }
85
86            foreach ($imprint2 as $byte) {
87                array_push($output, $byte);
88            }
89
90        }
91
92        array_push($output, $this->level);
93
94        return $output;
95
96    }
97
98    /**
99     * Gets the direction of this hash entry.
100     *
101     * @return int direction of this hash entry
102     */
103    public function getDirection() {
104        return $this->direction;
105    }
106
107    /**
108     * Gets the level of this hash entry.
109     *
110     * @return int level of this hash entry
111     */
112    public function getLevel() {
113        return $this->level;
114    }
115
116}
117
118?>
119