Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 添加了测试用例 '010101'
  • Loading branch information
DongyunLee committed Jul 6, 2020
1 parent 622dc46 commit 203463c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/SM3.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SM3 implements ArrayAccess
* 实例化时直接调用将参数传给主方法
* Sm3 constructor.
*
* @param $message string 传入的消息
* @param $message string|mixed 传入的消息
*
* @throws \ErrorException
*/
Expand Down Expand Up @@ -68,7 +68,7 @@ public function __construct($message)
private function sm3()
{
/** @var string $m 转化后的消息(二进制码) */
$m = new BitString($this->message, true);
$m = new BitString($this->message, false);

// 一、填充
$l = strlen($m);
Expand Down
79 changes: 40 additions & 39 deletions src/types/BitString.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,57 @@ class BitString implements ArrayAccess
/**
* BitString constructor.
*
* @param $string string|BitString|Word|mixed 传入的数据
* @param bool $is_bit_string 是否比特串,默认不是
* @param $string string|BitString|Word|mixed 传入的数据
* @param false|null $is_bit_string 是否比特串(只有入口明确知道不是)
*
* @throws \ErrorException
*/
public function __construct($string, $is_bit_string = false)
public function __construct($string, $is_bit_string = null)
{
if (is_object($string)) {
$string = $string->getString();
}

if (!$is_bit_string) {
// 正常情况直接转换
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);
}
$arr = preg_split('/(?<!^)(?!$)/u', $str);
foreach ($arr as &$v) {
$temp = unpack('H*', $v, 0);
$v = base_convert($temp[1], 16, 2);
while (strlen($v) < 8) {
$v = '0' . $v;
}
unset($temp);
}
return join('', $arr);
}

/**
* 判断是否为比特串类型
*
Expand Down Expand Up @@ -85,35 +115,6 @@ public function is_bit_string($string)
return true;
}

/**
* 字符串转比特串
*
* @param $str int|string 普通字符串
*
* @return string 转换为比特串
* @throws \Exception
*/
private function str2bin($str)
{
if (!is_string($str) && !is_int($str)) {
throw new ErrorException('输入的类型错误');
}

if (is_int($str)) {
return decbin($str);
}
$arr = preg_split('/(?<!^)(?!$)/u', $str);
foreach ($arr as &$v) {
$temp = unpack('H*', $v, 0);
$v = base_convert($temp[1], 16, 2);
while (strlen($v) < 8) {
$v = '0' . $v;
}
unset($temp);
}
return join('', $arr);
}

public function __toString()
{
return $this->getString();
Expand Down Expand Up @@ -153,7 +154,7 @@ public function offsetExists($offset)
{
return isset($this->bit_string[$offset]);
}

/**
* Offset to set
*
Expand All @@ -162,7 +163,7 @@ public function offsetExists($offset)
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* @param mixed $value <p>
* The value to set.
* </p>
*
Expand All @@ -174,7 +175,7 @@ public function offsetSet($offset, $value)
$this->bit_string[$offset] = $value;
return $this;
}

/**
* Offset to unset
*
Expand Down
3 changes: 2 additions & 1 deletion tests/SM3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function additionProvider()
array('a a','d6ef141c5faa9bbde67cbc9f45988d6158eaf0bc2ab492bb489a6524ca492cbc'),
array("a\na",'c413edbaa5449ada676857e243ae8d66401d82474cc68c243950178280bf7ae0'),
array("a\ra",'75056f768d1ac970ef2faf58ae4be4666afc157e2cb87b9e08c526463bf787da'),
array("a\r\na",'d1041bb570f8c65e26299159e41e21961d0ce4b79a285ee32c75c11eab0a2dd7')
array("a\r\na",'d1041bb570f8c65e26299159e41e21961d0ce4b79a285ee32c75c11eab0a2dd7'),
array('010101','edf91d5ad8aca4b2d2d42b348516f33cdc0dfee9305554d447ed5710f670bc9d')
);
}
}

0 comments on commit 203463c

Please sign in to comment.