Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Utils
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 bin2hex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hex2bin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 base642bin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 bin2base64
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Crypto Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Crypto;
11
12use SodiumException;
13
14/**
15 * Class Utils.
16 *
17 * @package crypto
18 */
19class Utils
20{
21    /**
22     * Converts a binary string to hexadecimal.
23     *
24     * @param string $string
25     *
26     * @throws SodiumException
27     *
28     * @return string The hexadecimal string
29     */
30    public static function bin2hex(string $string) : string
31    {
32        return \sodium_bin2hex($string);
33    }
34
35    /**
36     * Converts a hexadecimal string to binary.
37     *
38     * @param string $string
39     * @param string $ignore
40     *
41     * @throws SodiumException
42     *
43     * @return string The binary string
44     */
45    public static function hex2bin(string $string, string $ignore = '') : string
46    {
47        return \sodium_hex2bin($string, $ignore);
48    }
49
50    /**
51     * Converts a base64 string to binary.
52     *
53     * @param string $string
54     * @param int $id
55     * @param string $ignore
56     *
57     * @throws SodiumException
58     *
59     * @return string The binary string
60     */
61    public static function base642bin(
62        string $string,
63        int $id = \SODIUM_BASE64_VARIANT_ORIGINAL,
64        string $ignore = ''
65    ) : string {
66        return \sodium_base642bin($string, $id, $ignore);
67    }
68
69    /**
70     * Converts a binary string to base64.
71     *
72     * @param string $string
73     * @param int $id
74     *
75     * @throws SodiumException
76     *
77     * @return string The base64 encoded string
78     */
79    public static function bin2base64(
80        string $string,
81        int $id = \SODIUM_BASE64_VARIANT_ORIGINAL,
82    ) : string {
83        return \sodium_bin2base64($string, $id);
84    }
85}