<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @package asn1
 * @subpackage tsp
 */

/**
 * TSP MessageImprint implementatino.
 *
 * <pre>
 * MessageImprint ::= SEQUENCE  {
 *  hashAlgorithm                AlgorithmIdentifier,
 *  hashedMessage                OCTET STRING
 * }
 * </pre>
 *
 * @see X509AlgorithmIdentifier
 *
 * @package asn1
 * @subpackage tsp
 *
 * @link http://tools.ietf.org/html/rfc3161#section-2.4.1 RFC 3161: Time-Stamp Protocol
 */
class TSPMessageImprint implements ASN1DEREncodable {

    private $hashAlgorithm;
    private $hashedMessage;

    /**
     * Constructs a new instance of TSPMessageImprint.
     */
    public function __construct() {
    }

    /**
     * Decodes the given ASN1Sequence as TSPMessageImprint.
     *
     * @throws GTException
     * @param  ASN1Sequence $object TSPMessageImprint encoded as ASN1Sequence
     * @return void
     */
    public function decode($object) {

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("object must be an instance of ASN1Sequence");
        }

        if ($object->getObjectCount() != 2) {
            throw new GTException("object must have the size of 2");
        }

        $hashAlgorithm = new X509AlgorithmIdentifier();
        $hashAlgorithm->decode($object->getObjectAt(0));

        $hashedMessage = $object->getObjectAt(1);

        if (!$hashedMessage instanceof ASN1OctetString) {
            throw new GTException("Expecting an ASN1OctetString");
        }

        $hashedMessage = $hashedMessage->getValue();

        $this->setHashAlgorithm($hashAlgorithm);
        $this->setHashedMessage($hashedMessage);

    }

    /**
     * Encodes this TSPMessageImprint using DER.
     *
     * @return array byte array that contains the DER encoding of this TSPMessageImprint
     */
    public function encodeDER() {

        $sequence = new ASN1Sequence();

        $sequence->add($this->hashAlgorithm);
        $sequence->add(new ASN1OctetString($this->hashedMessage));

        return $sequence->encodeDER();
    }

    /**
     * Sets the hash algorithm.
     *
     * @throws GTException
     * @param  X509AlgorithmIdentifier $hashAlgorithm the hash algorithm used
     * @return void
     */
    public function setHashAlgorithm($hashAlgorithm) {

        if (!$hashAlgorithm instanceof X509AlgorithmIdentifier) {
            throw new GTException("hashAlgorithm must be an instance of X509AlgorithmIdentifier");
        }

        $this->hashAlgorithm = $hashAlgorithm;
    }

    /**
     * Gets the hash algorithm.
     *
     * @return X509AlgorithmIdentifier the hash algorithm used
     */
    public function getHashAlgorithm() {
        return $this->hashAlgorithm;
    }

    /**
     * Sets the hased message
     *
     * @throws GTException
     * @param  array $hashedMessage byte array containing the message bytes
     * @return void
     */
    public function setHashedMessage($hashedMessage) {

        if (!is_array($hashedMessage)) {
            throw new GTException("hashedMessage must be an array of bytes");
        }

        $this->hashedMessage = $hashedMessage;
    }

    /**
     * Gets the hashed message.
     *
     * @return array byte array containing the message bytes
     */
    public function getHashedMessage() {
        return $this->hashedMessage;
    }

}

?>
