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 */
23
24/**
25 * ASN.1 Octet String (byte array) implementation.
26 *
27 * @package asn1
28 */
29class ASN1OctetString extends ASN1Object {
30
31    protected $value;
32
33    /**
34     * Constructs a new ASN1OctetString.
35     *
36     * @throws GTException
37     * @param  array $value byte array
38     */
39    public function __construct($value = null) {
40
41        if (!is_null($value)) {
42
43            if (!is_array($value)) {
44                throw new GTException("value must be an array of bytes");
45            }
46
47            $this->value = $value;
48        }
49    }
50
51    /**
52     * Gets the byte array stored as value.
53     *
54     * @return array byte array
55     */
56    public function getValue() {
57        return $this->value;
58    }
59
60    /**
61     * Encodes the contents of this ASN1 Octet String.
62     *
63     * @return array DER encoding of this octet string
64     */
65    public function encodeDER() {
66
67        $bytes = array();
68
69        $this->append($bytes, ASN1DER::encodeType(ASN1_TAG_OCTET_STRING));
70        $this->append($bytes, ASN1DER::encodeLength(count($this->value)));
71        $this->append($bytes, $this->value);
72
73        return $bytes;
74    }
75
76    /**
77     * Decodes an ASN.1 Octet String from the given byte stream.
78     *
79     * @param  $bytes V bytes of this ASN.1 Octet String TLV
80     * @return void
81     */
82    public function decodeDER($bytes) {
83        $this->value = $bytes;
84    }
85}
86
87?>
88