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 * Hash algorithm object used to calculate data hashes.
26 *
27 * The currently supported algorithms are:
28 *
29 * <pre>
30 * ------------------------------------------------------------------
31 * Name         | ASN.1 OID                 | GTID  | Digest size   |
32 * ------------------------------------------------------------------
33 * RIPEMD160    | 1.3.36.3.2.1              | 2     | 20 bytes      |
34 * SHA1         | 1.3.14.3.2.26             | 0     | 20 bytes      |
35 * SHA224       | 2.16.840.1.101.3.4.2.4    | 3     | 28 bytes      |
36 * SHA256       | 2.16.840.1.101.3.4.2.1    | 1     | 32 bytes      |
37 * SHA384       | 2.16.840.1.101.3.4.2.2    | 4     | 48 bytes      |
38 * SHA512       | 2.16.840.1.101.3.4.2.3    | 5     | 64 bytes      |
39 *-------------------------------------------------------------------
40 * </pre>
41 *
42 * To create a hash algorithm object, use one of these methods
43 * (examples given for SHA-256):
44
45 * <code>
46 * $algorithm1 = GTHashAlgorithm::getByName('SHA256');
47 * $algorithm2 = GTHashAlgorithm::getByOid('2.16.840.1.101.3.4.2.1');
48 * $algorithm3 = GTHashAlgorithm::getByGtid(1);
49 * </code>
50 *
51 * @see GTDataHash
52 *
53 * @package tsp
54 */
55class GTHashAlgorithm {
56
57    private $name;
58    private $oid;
59    private $gtid;
60    private $length;
61
62    /**
63     * Construct a new instance of GTHashAlgorithm.
64     *
65     * @param  string $name algorithm name
66     * @param  string $oid algorithm oid
67     * @param  int $gtid algorithm GuardTime ID
68     * @param  int $length message digest (hash value) length
69     */
70    private function __construct($name, $oid, $gtid, $length) {
71
72        $this->name = $name;
73        $this->oid = $oid;
74        $this->gtid = $gtid;
75        $this->length = $length;
76
77    }
78
79    /**
80     * Returns the name of this hash algorithm.
81     *
82     * Name is an upper case string with no dashes, e.g. {@code SHA256}.
83     *
84     * @return string hash algorithm name
85     */
86    public function getName() {
87        return $this->name;
88    }
89
90    /**
91     * Returns the ASN.1 object identifier (OID) of this hash algorithm.
92     *
93     * @return string hash algorithm OID
94     */
95    public function getOid() {
96        return $this->oid;
97    }
98
99    /**
100     * Returns the GuardTime identifier (GTID) of this hash algorithm.
101     *
102     * @return int hash algorithm GTID.
103     */
104    public function getGtid() {
105        return $this->gtid;
106    }
107
108    /**
109     * Returns the length (in bytes) of message digest (hash value) produced by this hash algorithm.
110     *
111     * @return int message digest length in bytes
112     */
113    public function getLength() {
114        return $this->length;
115    }
116
117    /**
118     * Retrieves the hash algorithm with the given name.
119     *
120     * This method is case- and dash-insensitive
121     * e.g. 'SHA256', 'sha256', 'SHA-256' and 'sha-256' all will be accepted.
122     *
123     * @static
124     * @param string $name algorithm name
125     * @throws GTException if there is no algorithm with the given name.
126     * @return GTHashAlgorithm hash algorithm object with the given name.
127     */
128    public static function getByName($name) {
129
130        if (empty($name)) {
131            throw new GTException("parameter name must not be empty");
132        }
133
134        $name = str_replace('-', '', $name);
135        $name = strtoupper($name);
136
137        if ($name === 'DEFAULT') {
138            return GTHashAlgorithm::getByName('SHA256');
139        }
140
141        foreach (GTHashAlgorithm::getSupportedAlgorithms() as $algorithm) {
142            if ($algorithm->getName() === $name) {
143                return $algorithm;
144            }
145        }
146
147        throw new GTException("Unsupported hash algorithm name: {$name}");
148
149    }
150
151    /**
152     * Retrieves the hash algorithm with the given object identifier (OID).
153     *
154     * @static
155     * @throws GTException if there is no algorithm with the given OID
156     * @param  string $oid algorithm OID.
157     * @return GTHashAlgorithm hash algorithm object with the given OID.
158     */
159    public static function getByOid($oid) {
160
161        if (empty($oid)) {
162            throw new GTException("parameter oid must not be empty");
163        }
164
165        foreach (GTHashAlgorithm::getSupportedAlgorithms() as $algorithm) {
166            if ($algorithm->getOid() === $oid) {
167                return $algorithm;
168            }
169        }
170
171        throw new GTException("Unsupported hash algorithm oid: {$oid}");
172
173    }
174
175    /**
176     * Retrieves the hash algorithm with the given GuardTime ID (GTID).
177     *
178     * @static
179     * @throws GTException if there is no algorithm with the given GTID.
180     * @param  int $gtid algorithm GTID
181     * @return GTHashAlgorithm hash algorithm object with the given GTID.
182     */
183    public static function getByGtid($gtid) {
184
185        if (!is_integer($gtid)) {
186            $gtid = (int) $gtid;
187        }
188
189        foreach (GTHashAlgorithm::getSupportedAlgorithms() as $algorithm) {
190            if ($algorithm->getGtid() === $gtid) {
191                return $algorithm;
192            }
193        }
194
195        throw new GTException("Unsupported hash algorithm gtid: {$gtid}");
196
197    }
198
199    /**
200     * Gets all supported hash algorithms.
201     *
202     * @static
203     * @return array array of all supported hash algorithms
204     */
205    private static function getSupportedAlgorithms() {
206
207        return array(
208            new GTHashAlgorithm("RIPEMD160", "1.3.36.3.2.1", 2, 20),
209            new GTHashAlgorithm("SHA1", "1.3.14.3.2.26", 0, 20),
210            new GTHashAlgorithm("SHA224", "2.16.840.1.101.3.4.2.4", 3, 28),
211            new GTHashAlgorithm("SHA256", "2.16.840.1.101.3.4.2.1", 1, 32),
212            new GTHashAlgorithm("SHA384", "2.16.840.1.101.3.4.2.2", 4, 48),
213            new GTHashAlgorithm("SHA512", "2.16.840.1.101.3.4.2.3", 5, 64)
214        );
215
216    }
217}
218
219?>
220