| @@ 118-130 (lines=13) @@ | ||
| 115 | * @return string A hexadecimal-encoded string |
|
| 116 | * @throws TypeError |
|
| 117 | */ |
|
| 118 | public static function bin2hex($string) |
|
| 119 | { |
|
| 120 | /* Type checks: */ |
|
| 121 | ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
|
| 122 | ||
| 123 | if (self::isPhp72OrGreater()) { |
|
| 124 | return bin2hex($string); |
|
| 125 | } |
|
| 126 | if (self::use_fallback('bin2hex')) { |
|
| 127 | return call_user_func('\\Sodium\\bin2hex', $string); |
|
| 128 | } |
|
| 129 | return ParagonIE_Sodium_Core_Util::bin2hex($string); |
|
| 130 | } |
|
| 131 | ||
| 132 | /** |
|
| 133 | * Compare two strings, in constant-time. |
|
| @@ 947-959 (lines=13) @@ | ||
| 944 | * @param string $seed |
|
| 945 | * @return string |
|
| 946 | */ |
|
| 947 | public static function crypto_box_seed_keypair($seed) |
|
| 948 | { |
|
| 949 | /* Type checks: */ |
|
| 950 | ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); |
|
| 951 | ||
| 952 | if (self::isPhp72OrGreater()) { |
|
| 953 | return sodium_crypto_box_seed_keypair($seed); |
|
| 954 | } |
|
| 955 | if (self::use_fallback('crypto_box_seed_keypair')) { |
|
| 956 | return call_user_func('\\Sodium\\crypto_box_seed_keypair', $seed); |
|
| 957 | } |
|
| 958 | return ParagonIE_Sodium_Crypto::box_seed_keypair($seed); |
|
| 959 | } |
|
| 960 | ||
| 961 | /** |
|
| 962 | * Calculates a BLAKE2b hash, with an optional key. |
|
| @@ 2008-2020 (lines=13) @@ | ||
| 2005 | * @return string Raw binary string |
|
| 2006 | * @throws TypeError |
|
| 2007 | */ |
|
| 2008 | public static function hex2bin($string) |
|
| 2009 | { |
|
| 2010 | /* Type checks: */ |
|
| 2011 | ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); |
|
| 2012 | ||
| 2013 | if (self::isPhp72OrGreater()) { |
|
| 2014 | return sodium_hex2bin($string); |
|
| 2015 | } |
|
| 2016 | if (self::use_fallback('hex2bin')) { |
|
| 2017 | return call_user_func('\\Sodium\\hex2bin', $string); |
|
| 2018 | } |
|
| 2019 | return ParagonIE_Sodium_Core_Util::hex2bin($string); |
|
| 2020 | } |
|
| 2021 | ||
| 2022 | /** |
|
| 2023 | * Increase a string (little endian) |
|
| @@ 2120-2137 (lines=18) @@ | ||
| 2117 | * @return void |
|
| 2118 | * @throws Error (Unless libsodium is installed) |
|
| 2119 | */ |
|
| 2120 | public static function memzero(&$var) |
|
| 2121 | { |
|
| 2122 | /* Type checks: */ |
|
| 2123 | ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); |
|
| 2124 | ||
| 2125 | if (self::isPhp72OrGreater()) { |
|
| 2126 | sodium_memzero($var); |
|
| 2127 | return; |
|
| 2128 | } |
|
| 2129 | if (self::use_fallback('memzero')) { |
|
| 2130 | @call_user_func('\\Sodium\\memzero', $var); |
|
| 2131 | return; |
|
| 2132 | } |
|
| 2133 | // This is the best we can do. |
|
| 2134 | throw new Error( |
|
| 2135 | 'This is not implemented, as it is not possible to securely wipe memory from PHP' |
|
| 2136 | ); |
|
| 2137 | } |
|
| 2138 | ||
| 2139 | /** |
|
| 2140 | * Generate a string of bytes from the kernel's CSPRNG. |
|