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 asn1
22 * @subpackage gt
23 */
24
25/**
26 * GT PublishedData implementation.
27 *
28 * <pre>
29 * PublishedData ::= SEQUENCE {
30 *    publicationIdentifier   INTEGER,
31 *    publicationImprint      DataImprint
32 * }
33 * </pre>
34 *
35 * <pre>
36 * DataImprint ::= OCTET STRING
37 * </pre>
38 *
39 * @package asn1
40 * @subpackage gt
41 */
42class GTPublishedData implements ASN1DEREncodable {
43
44    private $publicationIdentifier;
45    private $publicationImprint;
46
47    /**
48     * Constructs a new instance of GTPublishedData.
49     */
50    public function __construct() {
51    }
52
53    /**
54     * Decodes the given ASN1Sequence as GTPublishedData.
55     *
56     * @throws GTException
57     * @param  ASN1Sequence $object GTPublishedData encoded as ASN1Sequence
58     * @return void
59     */
60    public function decode($object) {
61
62        if (!$object instanceof ASN1Sequence) {
63            throw new GTException("Expecting an ASN1Sequence");
64        }
65
66        $size = $object->getObjectCount();
67
68        if ($size != 2) {
69            throw new GTException("Invalid sequence size: {$size}");
70        }
71
72        $publicationIdentifier = $object->getObjectAt(0);
73
74        if (!$publicationIdentifier instanceof ASN1Integer) {
75            throw new GTException("Expecting an ASN1Integer for GTPublishedData.publicationIdentifier");
76        }
77
78        $publicationImprint = $object->getObjectAt(1);
79
80        if (!$publicationImprint instanceof ASN1OctetString) {
81            throw new GTException("Expecting an ASN1Integer for GTPublishedData.publicationImprint");
82        }
83
84        $this->publicationIdentifier = $publicationIdentifier->getValue();
85        $this->publicationImprint = $publicationImprint->getValue();
86
87        GTDataHash::getInstance($this->publicationImprint);
88    }
89
90    /**
91     * Encodes this GTPublishedData using DER.
92     *
93     * @return array byte array containing the DER encoding of this GTPublishedData
94     */
95    public function encodeDER() {
96
97        $sequence = new ASN1Sequence();
98
99        $sequence->add(new ASN1Integer(new GTBigInteger($this->publicationIdentifier)));
100        $sequence->add(new ASN1OctetString($this->publicationImprint));
101
102        return $sequence->encodeDER();
103    }
104
105    /**
106     * Gets the encoded publication.
107     *
108     * @return string encoded publication.
109     */
110    public function getEncodedPublication() {
111
112        $int = new GTBigInteger($this->publicationIdentifier);
113
114        $bytes = $int->toBytes();
115
116        // pad to 64 bit
117        while (count($bytes) % 8 != 0) {
118            array_unshift($bytes, 0);
119        }
120
121        foreach ($this->publicationImprint as $byte) {
122            array_push($bytes, $byte);
123        }
124
125        $bytes = GTUtil::addCrc32($bytes);
126
127        return GTBase32::encodeWithDashes($bytes);
128    }
129
130    /**
131     * Gets the publication identifier.
132     *
133     * @return string publication identifier
134     */
135    public function getPublicationIdentifier() {
136        return $this->publicationIdentifier;
137    }
138
139    /**
140     * Gets the publication imprint.
141     *
142     * @return array byte array containing the publication imprint bytes
143     */
144    public function getPublicationImprint() {
145        return $this->publicationImprint;
146    }
147
148}
149
150?>
151