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-16 encoding. 26 * 27 * @link http://tools.ietf.org/html/rfc4648#section-8 RFC 4648 28 * @see GTBaseX, GTBase32, GTBase64 29 * 30 * @package util 31 */ 32class GTBase16 { 33 34 private static $instance; 35 36 /** 37 * Encodes the given bytes into a base-16 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-16 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-16 string, inserting colons after every 2 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-16 string 57 */ 58 public static function encodeWithColons(array $bytes, $offset = null, $length = null) { 59 return self::getInstance()->encode($bytes, $offset, $length, ':', 2); 60 } 61 62 /** 63 * Encodes the given bytes into a base-16 string, inserting spaces after every 2 characters of output. 64 * 65 * @static 66 * @param array $bytes an array containing the bytes to encode 67 * @param int $offset the start offset of the data within bytes 68 * @param int $length the number of bytes to encode 69 * @return string the base-16 string 70 */ 71 public static function encodeWithSpaces(array $bytes, $offset = null, $length = null) { 72 return self::getInstance()->encode($bytes, $offset, $length, ' ', 2); 73 } 74 75 76 /** 77 * Decodes the given base-16 string into a byte array. 78 * 79 * @static 80 * @param string $string base-16 encoded string 81 * @return array array containing the decoded bytes 82 */ 83 public static function decode($string) { 84 return self::getInstance()->decode($string); 85 } 86 87 /** 88 * Gets singleton instance of GTBaseX configured for base-16 encoding/decoding. 89 * 90 * @static 91 * @return GTBaseX singleton instance of GTBaseX 92 */ 93 private static function getInstance() { 94 95 if (self::$instance == null) { 96 self::$instance = new GTBaseX("0123456789ABCDEF", false, ' '); 97 } 98 99 return self::$instance; 100 } 101 102} 103 104?> 105