password API reference
Password generation, validation, and complexity scoring against a policy schema. A Schema names the allowed character classes (lowercase, uppercase, digits, symbols), a length range, and per-class minimum counts; generate(schema) produces a conforming password, validate(schema, pw) reports whether a password meets the policy (and why not), and complexity(pw) estimates a password's strength in bits of entropy.
Randomness comes from the crypto library's crypto-grade source (character choice and the final shuffle), so a generated password is unpredictable and safe to mint as a real credential. Pure .j over crypto / strings / convert; runs on both binaries.
Import with import "password.j" as password;. See the password guide for prose and examples.
Contents
| Name | What it is |
|---|---|
complexity | Estimate a password's strength. |
generate | Generate a password conforming to the schema: a length picked in [minLength, maxLength], at least the per... |
schema | A strong default policy: exactly 16 characters, all four classes, at least one of each, the default symbol ... |
validate | Validate a password against a schema. |
withClasses | Copy a schema with new class enables (which classes appear in generation). |
withLength | Copy a schema with a new length range (set lo == hi for a fixed length). |
withMinimums | Copy a schema with new per-class minimum counts. |
withSymbolSet | Copy a schema with a replacement symbol set. |
withoutAmbiguous | Copy a schema that excludes ambiguous glyphs (0 O o 1 l I |) from generation. |
Report | struct: A validation result: whether the password conforms, and a list of the failed rules (empty when valid). |
Schema | struct: A password policy: allowed classes, a length range, per-class minimum counts, the symbol set, and whether t... |
Strength | struct: An estimated-strength summary of a password. |
Functions
complexity
password.complexity(pw as string)Estimate a password's strength. The alphabet size is the sum of the class sizes present (lower/upper 26 each, digits 10, symbols the default-set size), and entropy is length * log2(alphabet) bits.
pw- the password
- Returns
Strength - length, class count, pool size, entropy bits, and a label
generate
password.generate(s as Schema)Generate a password conforming to the schema: a length picked in [minLength, maxLength], at least the per-class minimums, the remainder drawn from the enabled alphabet, then shuffled.
s- the policy
- Returns
string - a fresh password
- Throws
Error - kind "password" if the schema is infeasible (no classes, an empty required pool, minimums that exceed the length) or maxLength is over 4096
schema
password.schema()A strong default policy: exactly 16 characters, all four classes, at least one of each, the default symbol set, ambiguous glyphs allowed.
- Returns
Schema - the default policy
validate
password.validate(s as Schema, pw as string)Validate a password against a schema. Checks the length bounds and each per-class minimum; it does not reject characters outside the alphabet (a policy sets minimums, not a whitelist).
s- the policy
pw- the password to check
- Returns
Report validplus the list of failed-rulereasons- Throws
Error - kind "password" on an invalid schema (see
withLength/withMinimums)
withClasses
password.withClasses(
s as Schema,
lo as bool,
up as bool,
dig as bool,
sym as bool)Copy a schema with new class enables (which classes appear in generation).
s- the base schema
lo- lowercase
up- uppercase
dig- digits
sym- symbols
- Returns
Schema - a fresh schema
withLength
password.withLength(s as Schema, lo as int, hi as int)Copy a schema with a new length range (set lo == hi for a fixed length).
s- the base schema
lo- minimum length
hi- maximum length
- Returns
Schema - a fresh schema
- Throws
Error - kind "password" if
lo< 1 orlo>hi
withMinimums
password.withMinimums(
s as Schema,
lo as int,
up as int,
dig as int,
sym as int)Copy a schema with new per-class minimum counts.
s- the base schema
lo- minimum lowercase
up- minimum uppercase
dig- minimum digits
sym- minimum symbols
- Returns
Schema - a fresh schema
- Throws
Error - kind "password" on a negative minimum
withSymbolSet
password.withSymbolSet(s as Schema, setChars as string)Copy a schema with a replacement symbol set.
s- the base schema
setChars- the symbol characters to draw from
- Returns
Schema - a fresh schema
withoutAmbiguous
password.withoutAmbiguous(s as Schema)Copy a schema that excludes ambiguous glyphs (0 O o 1 l I |) from generation.
s- the base schema
- Returns
Schema - a fresh schema
Structs
Report
A validation result: whether the password conforms, and a list of the failed rules (empty when valid).
def struct Report {
valid as bool,
reasons as list of string
};valid- true when every rule passed
reasons- human-readable failed rules
Schema
A password policy: allowed classes, a length range, per-class minimum counts, the symbol set, and whether to drop ambiguous glyphs.
def struct Schema {
minLength as int,
maxLength as int,
lower as bool,
upper as bool,
digits as bool,
symbols as bool,
symbolSet as string,
minLower as int,
minUpper as int,
minDigits as int,
minSymbols as int,
excludeAmbiguous as bool
};minLength- shortest allowed length (also the generated length floor)
maxLength- longest allowed length (generation picks in the range)
lower- include lowercase in the generation alphabet
upper- include uppercase
digits- include digits
symbols- include symbols (from
symbolSet) symbolSet- the symbol characters to draw from
minLower- minimum lowercase (generation guarantees, validation requires)
minUpper- minimum uppercase
minDigits- minimum digits
minSymbols- minimum symbols
excludeAmbiguous- drop ambiguous glyphs (0 O o 1 l I |) from generation
Strength
An estimated-strength summary of a password.
def struct Strength {
length as int,
classes as int,
poolSize as int,
entropy as float,
label as string
};length- character count
classes- how many of the four classes are present (0-4)
poolSize- the estimated alphabet size (guessing space per char)
entropy- estimated bits of entropy (length * log2(poolSize))
label- a qualitative band: very weak / weak / reasonable / strong / very strong