password 字段和方法
字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。
来自 KScript.Core
#password->hash(algorithm?: argon2id|bcrypt) → string
存储时用来代替密码的哈希:argon2id(默认)或 bcrypt,每次都使用新的随机盐。
| 参数 | 接受 |
|---|
algorithm 可选 | 以下词之一:argon2id|bcrypt |
示例
<let name="pw" type="password" value="'correct horse battery staple'" />
<echo>{ $pw->hash()->starts_with('$argon2id$') }</echo>
$pw: password = 'correct horse battery staple'
echo $pw->hash()->starts_with('$argon2id$')
#password->is_strong(min?: int, max?: int, upper?: bool, lower?: bool, digit?: bool, symbol?: bool, common?: bool, not?: string|string[], score?: int) → bool
当密码通过 ->strength() 并满足这些规则时为 true。
| 参数 | 接受 |
|---|
min 可选 | int |
max 可选 | int |
upper 可选 | 标志:upper 或 not upper |
lower 可选 | 标志:lower 或 not lower |
digit 可选 | 标志:digit 或 not digit |
symbol 可选 | 标志:symbol 或 not symbol |
common 可选 | 标志:common 或 not common |
not 可选 | string|string[] |
score 可选 | int |
参数可以使用命名方式:->is_strong(…, max: …, upper: …)。
示例
<let name="pw" type="password" value="'correct horse battery staple'" />
<echo>{ $pw->is_strong(min: 12) }</echo>
$pw: password = 'correct horse battery staple'
echo $pw->is_strong(min: 12)
#password->needs_rehash(hash: string) → bool
当存储的哈希是用比 ->hash() 当前所用更弱的设置或另一种算法生成时返回 true,因此登录成功后应替换它。
示例
<let name="pw" type="password" value="'correct horse battery staple'" />
<echo>{ $pw->needs_rehash($pw->hash(algorithm: 'bcrypt')) }</echo>
$pw: password = 'correct horse battery staple'
echo $pw->needs_rehash($pw->hash(algorithm: 'bcrypt'))
#password->reveal() → string
密码的文本。仅用于传递密码,例如传给另一服务的登录页面。
示例
<let name="pw" type="password" value="'s3cret!'" />
<echo>$pw is { $pw->reveal()->count() } characters</echo>
$pw: password = 's3cret!'
echo "{ $pw } is { $pw->reveal()->count() } characters"
#password->strength(min?: int, max?: int, upper?: bool, lower?: bool, digit?: bool, symbol?: bool, common?: bool, not?: string|string[], score?: int) → map
密码的猜测难度:{ score(0 到 4)、label、ok、problems }。规则:min length(8)、max、upper、lower、digit、symbol、common(为 true 时拒绝常见密码)、not(不得包含的文本)以及 score(通过所需的最低分数,3)。
| 参数 | 接受 |
|---|
min 可选 | int |
max 可选 | int |
upper 可选 | 标志:upper 或 not upper |
lower 可选 | 标志:lower 或 not lower |
digit 可选 | 标志:digit 或 not digit |
symbol 可选 | 标志:symbol 或 not symbol |
common 可选 | 标志:common 或 not common |
not 可选 | string|string[] |
score 可选 | int |
参数可以命名:->strength(…, max: …, upper: …)。
示例
<let name="pw" type="password" value="'password1'" />
<echo>{ $pw->strength(min: 12, symbol).problems }</echo>
$pw: password = 'password1'
echo $pw->strength(min: 12, symbol).problems
#password->verify(hash: string) → bool
当此密码是所存哈希所由生成的密码时为真。无论是否匹配,所耗时间相同。
示例
<let name="pw" type="password" value="'correct horse battery staple'" />
<let name="stored" value="$pw->hash()" />
<echo>{ $pw->verify($stored) }</echo>
$pw: password = 'correct horse battery staple'
$stored = $pw->hash()
echo $pw->verify($stored)
来自 any 的方法