Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): 保留输入字符串中的空格符 & 二进制输入(类似‘010101’)当做字符串处理 #3

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SM3.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct($message)
private function sm3()
{
/** @var string $m 转化后的消息(二进制码) */
$m = new BitString($this->message);
$m = new BitString($this->message, true);

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

$this->bit_string = $this->is_bit_string($string)
? $string
: "{$this->str2bin($string)}";
if (!$is_bit_string) {
// 正常情况直接转换
$this->bit_string = "{$this->str2bin($string)}";
} else {
// 如果指定了传入的是比特串,就先走个验证试试
$this->bit_string = $this->is_bit_string($string)
? $string
: "{$this->str2bin($string)}";
}
}

/**
* 判断是否为比特串类型
*
Expand Down Expand Up @@ -147,7 +153,7 @@ public function offsetExists($offset)
{
return isset($this->bit_string[$offset]);
}

/**
* Offset to set
*
Expand All @@ -156,7 +162,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 @@ -168,7 +174,7 @@ public function offsetSet($offset, $value)
$this->bit_string[$offset] = $value;
return $this;
}

/**
* Offset to unset
*
Expand Down