diff --git a/src/smecc/SM4OLD/README.md b/src/smecc/SM4OLD/README.md deleted file mode 100644 index 7c0d35a..0000000 --- a/src/smecc/SM4OLD/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# sm -国密sm3,sm4 ECB加密源码 diff --git a/src/smecc/SM4OLD/SM3.php b/src/smecc/SM4OLD/SM3.php deleted file mode 100644 index 7829dbc..0000000 --- a/src/smecc/SM4OLD/SM3.php +++ /dev/null @@ -1,196 +0,0 @@ -message = $message; - /** @var string hash_value 杂凑值 */ - $this->hash_value = $this->sm3(); - } - - /** - * 主方法 - * - * @return string - * @throws \ErrorException - */ - private function sm3() - { - /** @var string $m 转化后的消息(二进制码) */ - // $json = json_encode($this->message); - $m = new BitString($this->message, false); - //var_dump($m);die(); //11010010 10111011 - // 一、填充 - $l = strlen($m); - - // 满足l + 1 + k ≡ 448mod512 的最小的非负整数 - $k = $l % 512; - $k = $k + 64 >= 512 - ? 512 - ($k % 448) - 1 - : 512 - 64 - $k - 1; - - $bin_l = new BitString($l); - // 填充后的消息 - $m_fill = new BitString( - $m # 原始消息m - . '1' # 拼个1 - . str_pad('', $k, '0') # 拼上k个比特的0 - . ( - strlen($bin_l) >= 64 - ? substr($bin_l, 0, 64) - : str_pad($bin_l, 64, '0', STR_PAD_LEFT) - ) # 64比特,l的二进制表示 - ); - // 二、迭代压缩 - // 迭代过程 - $B = str_split($m_fill, 512); - /** @var int $n m'可分为的组数 */ - $n = ($l + $k + 65) / 512; - if (count($B) !== $n) { - throw new ErrorException(); - } - - $V = array( - WordConversion::hex2bin(self::IV), - ); - $extended = new ExtendedCompression(); - foreach ($B as $key => $Bi) { - // print_r($Bi."\n"."====== $key ========\n".strlen($Bi)."\n"); - $V[$key + 1] = $extended->CF($V[$key], $Bi)->getBitString(); - } - - krsort($V); - reset($V); - $binary = current($V); - $rt = WordConversion::bin2hex($binary); - return $rt; - } - - /** - * 方便直接输出实例化的对象 - * - * @return string - */ - public function __toString() - { - return $this->hash_value; - } - - /** - * Whether a offset exists - * - * @link https://php.net/manual/en/arrayaccess.offsetexists.php - * - * @param mixed $offset

- * An offset to check for. - *

- * - * @return bool true on success or false on failure. - *

- *

- * The return value will be casted to boolean if non-boolean was returned. - * @since 5.0.0 - */ - public function offsetExists($offset) - { - return isset($this->hash_value[$offset]); - } - - /** - * Offset to retrieve - * - * @link https://php.net/manual/en/arrayaccess.offsetget.php - * - * @param mixed $offset

- * The offset to retrieve. - *

- * - * @return mixed Can return all value types. - * @since 5.0.0 - */ - public function offsetGet($offset) - { - return $this->hash_value[$offset]; - } - - /** - * Offset to set - * - * @link https://php.net/manual/en/arrayaccess.offsetset.php - * - * @param mixed $offset

- * The offset to assign the value to. - *

- * @param mixed $value

- * The value to set. - *

- * - * @return \SM3\SM3 - * @since 5.0.0 - */ - public function offsetSet($offset, $value) - { - $this->hash_value[$offset] = $value; - return $this; - } - - /** - * Offset to unset - * - * @link https://php.net/manual/en/arrayaccess.offsetunset.php - * - * @param mixed $offset

- * The offset to unset. - *

- * - * @return void - * @since 5.0.0 - */ - public function offsetUnset($offset) - { - unset($this->hash_value[$offset]); - } -} diff --git a/src/smecc/SM4OLD/Sm4.php b/src/smecc/SM4OLD/Sm4.php deleted file mode 100644 index 662ed87..0000000 --- a/src/smecc/SM4OLD/Sm4.php +++ /dev/null @@ -1,196 +0,0 @@ -_rk = array(); - $this->sM4KeySchedule($key); - $bytes = $this->pad($data); - $chunks = array_chunk($bytes, $this->_block_size); - $ciphertext = ""; - foreach ($chunks as $chunk) { - $ciphertext .= $this->sM4Encrypt($chunk); - } - return bin2hex($ciphertext); - return base64_encode($ciphertext); - } - - - public function decrypt($key, $data) - { - $this->_rk = array(); - // $data = base64_decode($data); - $data = hex2bin($data); - if (strlen($data) < 0 || strlen($data) % $this->_block_size != 0) { - return false; - } - $this->sM4KeySchedule($key); - $bytes = unpack("C*", $data); - $chunks = array_chunk($bytes, $this->_block_size); - $plaintext = ""; - foreach ($chunks as $chunk) { - $plaintext .= substr($this->sM4Decrypt($chunk), 0, 16); - } - $plaintext = $this->un_pad($plaintext); - return $plaintext; - } - - private function sM4Decrypt($cipherText) - { - $x = array(); - for ($j = 0; $j < 4; $j++) { - $x[$j] = ($cipherText[$j * 4] << 24) | ($cipherText[$j * 4 + 1] << 16) | ($cipherText[$j * 4 + 2] << 8) - | ($cipherText[$j * 4 + 3]); - } - for ($i = 0; $i < 32; $i++) { - $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[31 - $i]; - $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 - | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); - $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) - ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); - } - $plainText = array(); - for ($k = 0; $k < 4; $k++) { - $plainText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; - $plainText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; - $plainText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; - $plainText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; - } - return $this->bytesToString($plainText); - } - - private function sM4Encrypt($plainText) - { - $x = array(); - for ($j = 0; $j < 4; $j++) { - $x[$j] = ($plainText[$j * 4] << 24) | ($plainText[$j * 4 + 1] << 16) | ($plainText[$j * 4 + 2] << 8) - | ($plainText[$j * 4 + 3]); - } - for ($i = 0; $i < 32; $i++) { - $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[$i]; - $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 - | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); - $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) - ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); - } - $cipherText = array(); - for ($k = 0; $k < 4; $k++) { - $cipherText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; - $cipherText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; - $cipherText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; - $cipherText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; - } - return $this->bytesToString($cipherText); - } - - private function stringToBytes($string) - { - return unpack('C*', $string); - } - - private function bytesToString($bytes) - { - return vsprintf(str_repeat('%c', count($bytes)), $bytes); - } - - private function pad($data) - { - $bytes = $this->stringToBytes($data); - $rem = $this->_block_size - count($bytes) % $this->_block_size; - for ($i = 0; $i < $rem; $i++) { - array_push($bytes, $rem); - } - return $bytes; - } - - private function un_pad($data) - { - $bytes = $this->stringToBytes($data); - $rem = $bytes[count($bytes)]; - $bytes = array_slice($bytes, 0, count($bytes) - $rem); - return $this->bytesToString($bytes); - } - - private function sm4Rotl32($buf, $n) - { - return (($buf << $n) & 0xffffffff) | ($buf >> (32 - $n)); - } - - private function sM4KeySchedule($key) - { - $this->_rk = array(); - $key = array_values(unpack("C*", $key)); - $k = array(); -// for ($i = 0; $i < 4; $i++) { -// $k[$i] = self::$SM4_FK[$i] -// ^ ((($key[4 * $i] ?? 0) << 24) -// | (($key[4 * $i + 1] ?? 0) << 16) -// | (($key[4 * $i + 2] ?? 0) << 8) -// | ($key[4 * $i + 3] ?? null)); -// } - - for ($i = 0; $i < 4; $i++) { - $k[$i] = self::$SM4_FK[$i] - ^ (((isset($key[4 * $i]) ? $key[4 * $i] : 0) << 24) - | ((isset($key[4 * $i + 1]) ? $key[4 * $i + 1] : 0) << 16) - | ((isset($key[4 * $i + 2]) ? $key[4 * $i + 2] : 0) << 8) - | (isset($key[4 * $i + 3]) ? $key[4 * $i + 3] : null)); - } - for ($j = 0; $j < 32; $j++) { - $tmp = $k[$j + 1] ^ $k[$j + 2] ^ $k[$j + 3] ^ self::$SM4_CK[$j]; - $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 - | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); - $k[$j + 4] = $k[$j] ^ (($buf) ^ ($this->sm4Rotl32(($buf), 13)) ^ ($this->sm4Rotl32(($buf), 23))); - $this->_rk[$j] = $k[$j + 4]; - } - } - -} - diff --git a/src/smecc/SM4OLD/handler/BigJHandler.php b/src/smecc/SM4OLD/handler/BigJHandler.php deleted file mode 100644 index 14316c4..0000000 --- a/src/smecc/SM4OLD/handler/BigJHandler.php +++ /dev/null @@ -1,80 +0,0 @@ -extended($Bi); - - /** @var array $registers 八个寄存器的名字 */ - $registers = array( - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H' - ); - - // 将 Vi 的值依次放入八个寄存器中 - // 下列注释用于防止IDE报错 - /** @var Word $A */ - /** @var Word $B */ - /** @var Word $C */ - /** @var Word $D */ - /** @var Word $E */ - /** @var Word $F */ - /** @var Word $G */ - /** @var Word $H */ - foreach ($registers as $i => $register) { - $$register = new Word(substr($Vi, $i * 32, 32)); - } - - - $small_j_handler = new SmallJHandler(); - $big_j_handler = new BigJHandler(); - - for ($j = 0; $j < 64; $j++) { - $j_handler = ($j >= SmallJHandler::SMALLEST_J && $j < BigJHandler::SMALLEST_J) - ? $small_j_handler - : $big_j_handler; - - $SS1 = WordConversion::shiftLeftConversion( - WordConversion::addConversion( - array( - WordConversion::shiftLeftConversion($A, 12), - $E, - WordConversion::shiftLeftConversion($j_handler->getT(), $j) - ) - ), - 7 - ); - - $SS2 = WordConversion::xorConversion( - array( - $SS1, - WordConversion::shiftLeftConversion($A, 12) - ) - ); - - $TT1 = WordConversion::addConversion( - array( - $j_handler->FF($A, $B, $C), - $D, - $SS2, - $this->W_s[$j] - ) - ); - - $TT2 = WordConversion::addConversion( - array( - $j_handler->GG($E, $F, $G), - $H, - $SS1, - $this->W[$j] - ) - ); - - $D = $C; - - $C = WordConversion::shiftLeftConversion($B, 9); - - $B = $A; - - $A = $TT1; - - $H = $G; - - $G = WordConversion::shiftLeftConversion($F, 19); - - $F = $E; - - $TT2_object = new Substitution($TT2); - $E = $TT2_object->P0(); - } - - return WordConversion::xorConversion( - array( - join( - '', - array( - (new Word($A)), - (new Word($B)), - (new Word($C)), - (new Word($D)), - (new Word($E)), - (new Word($F)), - (new Word($G)), - (new Word($H)) - ) - ), - $Vi - ) - ); - } - - /** - * 消息扩展 - * - * 将消息分组B(i)按以下方法扩展生成132个字W0, W1, · · · , W67, W′0, W′1, · · · , W′63, - * 用于压缩函数CF - * - * @param $Bi 消息分组中的第i个,最大512位 (\SM3\types\BitString) - * - * @return void - * @throws \ErrorException - */ - public function extended($Bi) - { - // 将消息分组B(i)划分为16个字W0, W1, · · · , W15。 - $this->W = $this->W_s = array(); - - $word_per_times = (int)ceil(strlen($Bi) / 16); - for ($i = 0; $i < 16; $i++) { - $this->W[$i] = new Word( - substr($Bi, $i * $word_per_times, $word_per_times) - ); - } - - // 计算W - for ($j = 16; $j <= 67; $j++) { - $param_1 = (new Substitution( - WordConversion::xorConversion( - array( - $this->W[$j - 16], - $this->W[$j - 9], - WordConversion::shiftLeftConversion($this->W[$j - 3], 15) - ) - ) - )); - - $this->W[$j] = WordConversion::xorConversion( - array( - $param_1->P1(), - WordConversion::shiftLeftConversion($this->W[$j - 13], 7), - $this->W[$j - 6] - ) - ); - } - - unset($j); - - // 计算W' - for ($j = 0; $j <= 63; $j++) { - $this->W_s[$j] = WordConversion::xorConversion( - array( - $this->W[$j], - $this->W[$j + 4] - ) - ); - } - } -} diff --git a/src/smecc/SM4OLD/handler/JHandler.php b/src/smecc/SM4OLD/handler/JHandler.php deleted file mode 100644 index 1d56adb..0000000 --- a/src/smecc/SM4OLD/handler/JHandler.php +++ /dev/null @@ -1,94 +0,0 @@ -setT($T); - $this->setSectionJ($smallest, $biggest); - } - - /** - * 配置 继承本抽象类的子类可以处理的j的大小 - * - * @param $smallest int j的最小长度 - * @param $biggest int j的最大长度 - */ - public function setSectionJ($smallest, $biggest) - { - $this->section_j = array($smallest, $biggest); - } - - /** - * 布尔函数 - * - * @param $X string 长度32的比特串 - * @param $Y string - * @param $Z string - * - * @return mixed - */ - abstract public function FF($X, $Y, $Z); - - /** - * 布尔函数 - * - * @param $X - * @param $Y - * @param $Z - * - * @return mixed - */ - abstract public function GG($X, $Y, $Z); - - /** - * 读取常量T - * @return Word - * @throws \ErrorException - */ - public function getT() - { - return new Word($this->T); - } - - /** - * 配置常量T - * - * @param string $T - */ - public function setT($T) - { - $this->T = WordConversion::hex2bin($T); - } -} diff --git a/src/smecc/SM4OLD/handler/README.md b/src/smecc/SM4OLD/handler/README.md deleted file mode 100644 index ba17868..0000000 --- a/src/smecc/SM4OLD/handler/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# *handle/* 目录 - -本目录包含了加密过程中的核心算法 - -* `\SM3\handler\JHandler` - 根据J的不同,进行不同处理的抽象类 - * `\SM3\handler\SmalJHandler` - 变量j处于较小值时的处理类 - * `\SM3\handler\BigJHandler` - 变量j处于较大值时的处理类 -* `\SM3\handler\Substitution` - 置换函数类 \ No newline at end of file diff --git a/src/smecc/SM4OLD/handler/SmallJHandler.php b/src/smecc/SM4OLD/handler/SmallJHandler.php deleted file mode 100644 index f3fdf05..0000000 --- a/src/smecc/SM4OLD/handler/SmallJHandler.php +++ /dev/null @@ -1,79 +0,0 @@ -X = $X; - } - - /** - * 压缩函数中的置换函数 - * - * @return \SM3\types\Word 置换结果 - */ - public function P0() - { - return $this->substitutionFunction(0); - } - - /** - * 置换函数的公共函数 - * @param $type - * @return Word - * @throws \ErrorException - */ - private function substitutionFunction($type) - { - if (!in_array($type, array(0, '0', 1, '1'))) { - return new Word(''); - } - - $times_name = $type == 1 - ? $this->P1_shiftLeft_times - : $this->P0_shiftLeft_times; - - $X_shiftLeft_1 = WordConversion::shiftLeftConversion($this->X, $times_name[0]); - $X_shiftLeft_2 = WordConversion::shiftLeftConversion($this->X, $times_name[1]); - - return WordConversion::xorConversion( - array( - $this->X, - $X_shiftLeft_1, - $X_shiftLeft_2 - ) - ); - } - - /** - * 消息扩展中的置换函数 - * @return Word - * @throws \ErrorException - */ - public function P1() - { - return $this->substitutionFunction(1); - } -} diff --git a/src/smecc/SM4OLD/libs/README.md b/src/smecc/SM4OLD/libs/README.md deleted file mode 100644 index 3a40f0c..0000000 --- a/src/smecc/SM4OLD/libs/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# *libs/* 目录 - -本目录包含了 *动态读写配置* 和 *一些底层的基础操作(如:按位运算)* - -* `\SM3\libs\Config` - 配置类,获取和动态设定配置 -* `\SM3\libs\WordConversion` - 字的位运算类,包含了位的各种运算 diff --git a/src/smecc/SM4OLD/libs/WordConversion.php b/src/smecc/SM4OLD/libs/WordConversion.php deleted file mode 100644 index 6e04af0..0000000 --- a/src/smecc/SM4OLD/libs/WordConversion.php +++ /dev/null @@ -1,271 +0,0 @@ -= strlen($prevent)) { - $longest = strlen($current); - $longest_value = $current; - $shortest = strlen($prevent); - } else { - $longest = strlen($prevent); - $longest_value = $prevent; - $shortest = strlen($current); - } - - if ($prevent === '0' || $current === '0') { - switch ($type) { - // and - case 1: - return 0; - // or - case 2: - // xor - case 3: - // add - case 5: - return $prevent == '0' ? $current : $prevent; - default: - break; - } - } - - $value = array(); - /** - * 加运算时需要,用来储存需要进几 - * - * @var int 向前一位进的数字 - */ - $carry = 0; - /** - * 大端 - * - * 这里从大端跑完之后,结果数组的序号是从大到小排列的 - * 还需要根据键名排序一次 - * - * 个人感觉区分不区分大端并没有什么意义 - * 如果换成字符串拼接的话更好用 - * 但是方便你理解,还是按照大端+数组的方式进行的排列 - */ - for ($i = $longest - 1; $i >= 0; $i--) { - $prevent_number = $prevent[$i]; - switch ($type) { - // 与 - case 1: - $value[$i] = ($i >= $shortest) - ? $longest_value[$i] - : ($prevent_number & $current[$i]); - break; - // 或 - case 2: - $value[$i] = ($i >= $shortest) - ? ~$longest_value[$i] - : ($prevent_number | $current[$i]); - break; - // 异或 - case 3: - $value[$i] = $i > $shortest - ? 1 - : (intval($prevent_number) ^ intval($current[$i])); - break; - // 非(按位取反) - case 4: - $value[$i] = $prevent_number === '1' - ? '0' - : '1'; - break; - // 加 - case 5: - $add = $prevent_number + $current[$i] + $carry; - $value[$i] = $add % 2; - $carry = ($add - $value[$i]) / 2; - break; - // 特殊情况 - default: - break; - } - } - - ksort($value); - return new Word(join('', $value)); - } - - /** - * @param $params array 需要进行异或运算的字列表 - * @param $type int 位运算类型 - * @return Word|null 运算结果 - */ - private static function conversion($params, $type) - { - $prevent = null; - foreach ($params as $param){ - $prevent = self::conversion_func($prevent,$param,$type); - } - return $prevent; - } - - /** - * 字的与运算 - * - * @param $params array 需要进行与运算的字列表 - * - * @return Word - */ - public static function andConversion($params) - { - return self::conversion($params, 1); - } - - /** - * 字的或运算 - * - * @param $params - * - * @return Word - */ - public static function orConversion($params) - { - return self::conversion($params, 2); - } - - /** - * 字的非运算 - * - * @param $word - * - * @return Word - */ - public static function notConversion($word) - { - return self::conversion(array($word, null), 4); - } - - - - /** - * @param $word Word 待运算的字 - * @param $times int 左移的位数 - * @return Word - * @throws \ErrorException - */ - public static function shiftLeftConversion($word, $times) - { - return new Word( - substr( - $word, - ($times % strlen($word)) - ) - . substr( - $word, - 0, - ($times % strlen($word)) - ) - ); - } - - /** - * 将16进制数串转换为二进制数据 - * 使用字符串形式实现,解决了PHP本身进制转换的时候受限于浮点数大小的问题 - * - * @param int|string $hex - * - * @return string - */ - public static function hex2bin($hex) - { - // 格式化为字符串 - $hex = strval($hex); - - /** 十六进制转二进制,每1位一组 */ - defined('HEX_TO_BIN_NUM') || define('HEX_TO_BIN_NUM', 1); - /** @var array $hex_array 把指定的十六进制数按位切片为数组 */ - $hex_array = str_split($hex, HEX_TO_BIN_NUM); - // 最终的二进制数字(为确保长度不丢失,使用字符串类型) - $binary = ''; - - foreach ($hex_array as $number) { - $bin_number = strval(base_convert($number, 16, 2)); - if (strlen($bin_number) < 4) { - $bin_number = str_pad($bin_number, 4, '0', STR_PAD_LEFT); - } - $binary .= $bin_number; - } - - return $binary; - } - - /** - * 二进制转十六进制 - * @param $bin - * - * @return string - */ - public static function bin2hex($bin) - { - // 格式化为字符串 - $bin = strval($bin); - - /** 二进制转十六进制,每4位一组 */ - defined('BIN_TO_HEX_NUM') || define('BIN_TO_HEX_NUM', 4); - /** @var array $bin_array 把指定的二进制数按位切片为数组 */ - $bin_array = str_split($bin, BIN_TO_HEX_NUM); - // 最终的二进制数字(为确保长度不丢失,使用字符串类型) - $hex = ''; - - foreach ($bin_array as $number) { - $hex .= strval(base_convert($number, 2, 16)); - } - - return $hex; - } - - /** - * 二进制加运算 - * - * @param array $params - * - * @return Word - */ - public static function addConversion($params) - { - return self::conversion($params, 5); - } -} diff --git a/src/smecc/SM4OLD/types/BitString.php b/src/smecc/SM4OLD/types/BitString.php deleted file mode 100644 index d4c7c5e..0000000 --- a/src/smecc/SM4OLD/types/BitString.php +++ /dev/null @@ -1,204 +0,0 @@ -getString(); - } - - if ($is_bit_string === false) { - // 指定不是比特串的,直接转换 - $this->bit_string = "{$this->str2bin($string)}"; - } else { - // 默认走个验证试试 - $this->bit_string = $this->is_bit_string($string) - ? $string - : "{$this->str2bin($string)}"; - } - } - - /** - * 字符串转比特串 - * - * @param $str int|string 普通字符串 - * - * @return string 转换为比特串 - * @throws \ErrorException - */ - private function str2bin($str) - { - if (!is_string($str) && !is_int($str)) { - throw new ErrorException('输入的类型错误'); - } - if (is_int($str)) { - return decbin($str); - } - $fileType = mb_detect_encoding($str , array('UTF-8','GBK','LATIN1','BIG5')) ; - if( $fileType != 'UTF-8'){ - $str = mb_convert_encoding($str ,'utf-8' , $fileType); - } - $arr = preg_split('/(?getString(); - } - // 检查是否为字符串 - if (!is_string($string)) { - return false; - } - - // 检查是否为只有0和1组成的字符串 - $array = array_filter(str_split($string)); - foreach ($array as $value) { - if (!in_array( - $value, - array( - 0, - '0', - 1, - '1' - ), - true - )) { - return false; - } - } - - return true; - } - - public function __toString() - { - return $this->getString(); - } - - /** - * 获取比特串的值 - * - * @return string - */ - public function getString() - { - return $this->bit_string; - } - - public function offsetGet($offset) - { - return $this->bit_string[$offset]; - } - - /** - * Whether a offset exists - * - * @link https://php.net/manual/en/arrayaccess.offsetexists.php - * - * @param mixed $offset

- * An offset to check for. - *

- * - * @return bool true on success or false on failure. - *

- *

- * The return value will be casted to boolean if non-boolean was returned. - * @since 5.0.0 - */ - public function offsetExists($offset) - { - return isset($this->bit_string[$offset]); - } - - /** - * Offset to set - * - * @link https://php.net/manual/en/arrayaccess.offsetset.php - * - * @param mixed $offset

- * The offset to assign the value to. - *

- * @param mixed $value

- * The value to set. - *

- * - * @return BitString - * @since 5.0.0 - */ - public function offsetSet($offset, $value) - { - $this->bit_string[$offset] = $value; - return $this; - } - - /** - * Offset to unset - * - * @link https://php.net/manual/en/arrayaccess.offsetunset.php - * - * @param mixed $offset

- * The offset to unset. - *

- * - * @return void - * @since 5.0.0 - */ - public function offsetUnset($offset) - { - unset($this->bit_string[$offset]); - } -} diff --git a/src/smecc/SM4OLD/types/README.md b/src/smecc/SM4OLD/types/README.md deleted file mode 100644 index 73fe7c3..0000000 --- a/src/smecc/SM4OLD/types/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# *types/* 目录 - -本目录为自定义的扩展类型的目录 - -* `\SM3\types\BitString` - 比特串 类型 - * `\SM3\types\Word` - 字 类型 \ No newline at end of file diff --git a/src/smecc/SM4OLD/types/Word.php b/src/smecc/SM4OLD/types/Word.php deleted file mode 100644 index ed52370..0000000 --- a/src/smecc/SM4OLD/types/Word.php +++ /dev/null @@ -1,73 +0,0 @@ -bit_string) === self::length) { - $this->word = $this->bit_string; - } else { - $this->word = intval($this->bit_string) === 0 - ? 0 - : $this->bit_string; - - if (strlen($this->word) <= self::length) { - $this->word = str_pad( - $this->word, - self::length, - '0', - STR_PAD_LEFT - ); - } else { - $this->word = substr($this->bit_string, -(self::length)); - } - } - } - - public function __toString() - { - return $this->word; - } - - public function offsetGet($offset) - { - return $this->word[$offset]; - } - - public function getString() - { - return $this->word; - } - - /** - * @return string - */ - public function getBitString() - { - return $this->bit_string; - } - -}