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 util
22 */
23
24/**
25 * An implementation of RFC 4648 Base-32 encoding.
26 *
27 * @link http://tools.ietf.org/html/rfc4648#section-6 RFC 4648
28 * @see GTBaseX, GTBase16, GTBase64
29 *
30 * @package util
31 */
32class GTBase32 {
33
34    private static $instance;
35
36    /**
37     * Encodes the given bytes into a base-32 string.
38     *
39     * @static
40     * @param  array $bytes an array containing the bytes to encode.
41     * @param  int $offset the start offset of the data within bytes
42     * @param  int $length the number of bytes to encode
43     * @return string the base-32 string
44     */
45    public static function encode(array $bytes, $offset = null, $length = null) {
46        return self::getInstance()->encode($bytes, $offset, $length);
47    }
48
49    /**
50     * Encodes the given bytes into a base-32 string, inserting dashes after every 6 characters of output.
51     *
52     * @static
53     * @param  array $bytes an array containing the bytes to encode
54     * @param  int $offset the start offset of the data within bytes
55     * @param  int $length the number of bytes to encode
56     * @return string the base-32 string
57     */
58    public static function encodeWithDashes(array $bytes, $offset = null, $length = null) {
59        return self::getInstance()->encode($bytes, $offset, $length, '-', 6);
60    }
61
62    /**
63     * Decodes the given base-32 string into a byte array.
64     *
65     * @static
66     * @param  string $string base-32 encoded string
67     * @return array array containing the decoded bytes
68     */
69    public static function decode($string) {
70        return self::getInstance()->decode($string);
71    }
72
73    /**
74     * Gets singleton instance of GTBaseX configured for base-32 encoding/decoding.
75     *
76     * @static
77     * @return GTBaseX singleton instance of GTBaseX
78     */
79    private static function getInstance() {
80
81        if (self::$instance == null) {
82            self::$instance = new GTBaseX("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", false, '=');
83        }
84
85        return self::$instance;
86    }
87
88}
89
90?>
91