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-64 encoding.
26 *
27 * @link http://tools.ietf.org/html/rfc4648#section-4 RFC 4648
28 * @see GTBaseX, GTBase16, GTBase32
29 *
30 * @package util
31 */
32class GTBase64 {
33
34    private static $instance;
35
36    /**
37     * Encodes the given bytes into a base-64 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-64 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     * Decodes the given base-64 string into a byte array.
51     *
52     * @static
53     * @param  string $string base-64 encoded string
54     * @return array array containing the decoded bytes
55     */
56    public static function decode($string) {
57        return self::getInstance()->decode($string);
58    }
59
60    /**
61     * Gets singleton instance of GTBaseX configured for base-64 encoding/decoding.
62     *
63     * @static
64     * @return GTBaseX singleton instance of GTBaseX
65     */
66    private static function getInstance() {
67
68        if (self::$instance == null) {
69            self::$instance = new GTBaseX("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", true, '=');
70        }
71
72        return self::$instance;
73    }
74
75}
76
77?>
78