From 11ffbf93d627fb7c6d865133288c9a30acd0b267 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Wed, 20 Dec 2017 15:09:52 +0800 Subject: [PATCH] hexToBin --- src/Utils.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index 32b33ba..2cad45a 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -62,9 +62,11 @@ class Utils if (!is_string($value)) { throw new InvalidArgumentException('The value to toHex function must be string.'); } - $hexString = str_replace('0x', '', $value); - - return pack('H*', $hexString); + if (strpos($value, '0x') === 0) { + $count = 1; + $value = str_replace('0x', '', $value, $count); + } + return pack('H*', $value); } /** @@ -74,12 +76,12 @@ class Utils * @param string $value * @return string */ - public function sha3($value) + public static function sha3($value) { if (!is_string($value)) { throw new InvalidArgumentException('The value to sha3 function must be string.'); } - if (preg_match('/^0x/', $value) > 0) { + if (strpos($value, '0x') === 0) { $value = self::hexToBin($value); } $hash = Keccak::hash($value, 256);