KScript docs
EN

password fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

From KScript.Core

#password->hash(algorithm?: argon2id|bcrypt) → string

A hash to store instead of the password: argon2id (the default) or bcrypt, with a new random salt each time.
ParameterTakes
algorithm optionalone of these words: argon2id|bcrypt
Example
<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

True when the password passes ->strength() with these rules.
ParameterTakes
min optionalint
max optionalint
upper optionala flag: upper or not upper
lower optionala flag: lower or not lower
digit optionala flag: digit or not digit
symbol optionala flag: symbol or not symbol
common optionala flag: common or not common
not optionalstring|string[]
score optionalint

Arguments can be named: ->is_strong(…, max: …, upper: …).

Example
<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

True when the stored hash was made with weaker settings or another algorithm than ->hash() uses now, so it should be replaced after a successful login.
ParameterTakes
hash requiredstring
Example
<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

The password's text. Only use it to pass the password on, such as to another service's login.
Example
<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

How hard the password is to guess: { score (0 to 4), label, ok, problems }. Rules: min length (8), max, upper, lower, digit, symbol, common (true rejects well-known passwords), not (text it mustn't contain) and score (the lowest score that passes, 3).
ParameterTakes
min optionalint
max optionalint
upper optionala flag: upper or not upper
lower optionala flag: lower or not lower
digit optionala flag: digit or not digit
symbol optionala flag: symbol or not symbol
common optionala flag: common or not common
not optionalstring|string[]
score optionalint

Arguments can be named: ->strength(…, max: …, upper: …).

Example
<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

True when this is the password the stored hash was made from. Takes the same time whether it matches or not.
ParameterTakes
hash requiredstring
Example
<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)

Methods from any