Skip to content

sigil ​

@c9up/sigil: password hashing with argon2, bcrypt and scrypt, run by a native Rust engine. Guide: Hashing.

Installation ​

bash
pnpm add @c9up/sigil

ream configure @c9up/sigil registers the provider, writes config/hash.ts and adds HASH_DRIVER=argon2 to the environment files. The native engine comes as one small package per platform, @c9up/sigil-<platform>, for linux x64 and arm64 (glibc), macOS x64 and arm64, and Windows x64. @c9up/sigil lists them as optional dependencies, and your package manager installs only the one that matches your machine. No Rust toolchain is needed. Do not install with optional dependencies turned off (--no-optional, --omit=optional): the engine would be left out.

There is no JavaScript fallback. Without the engine, hashing throws E_SIGIL_NAPI_REQUIRED, and its message names the package to install, for example @c9up/sigil-linux-x64-gnu.

Entry points ​

ImportContents
@c9up/sigilHash, Hasher, defineConfig, drivers, the error classes and the config types
@c9up/sigil/providerSigilProvider (default export)
@c9up/sigil/services/mainThe application's Hash instance (default export)
@c9up/sigil/configureconfigure(), run by ream configure @c9up/sigil

Exports ​

@c9up/sigil ​

ExportKindDescription
HashclassHash manager: holds the configured hashers and delegates to the default one
HashManagerclassAnother name for Hash
HasherclassOne hasher, as returned by hash.use(name)
defineConfigfunctiondefineConfig(config: HashConfig): HashConfig, types config/hash.ts
driversconstdrivers.argon2(options?), drivers.bcrypt(options?), drivers.scrypt(options?): build a HashDriverConfig
SigilProviderproviderSame class as @c9up/sigil/provider
errorsnamespaceThe package's errors, one class per code, see Errors
HashConfiginterface{ default: string; list?: Record<string, HashDriverConfig>; drivers?: Record<string, HashDriverConfig> }
HashDriverConfiginterface{ driver: string; [key: string]: unknown }
HashDriverinterfaceWhat a driver implements: isValidHash, make, verify, needsReHash
Argon2OptionsinterfaceOptions of drivers.argon2()
BcryptOptionsinterfaceOptions of drivers.bcrypt()
ScryptOptionsinterfaceOptions of drivers.scrypt()
ReleasableSecretinterface{ release(): string }, the shape of an environment Secret

Hash ​

new Hash(config: HashConfig). The constructor validates every hasher: an unknown driver, an option out of range (E_SIGIL_INVALID_OPTION) or a default that names no hasher fails here, at boot.

MemberSignatureDescription
configreadonly config: HashConfigThe configuration, with every argon2 secret wrapped so logging it does not print the pepper
makemake(value: string): Promise<string>Hash value with the default hasher
verifyverify(hash: string, value: string): Promise<boolean>Check value against a stored hash; the hash comes first
isValidHashisValidHash(hash: string): booleanWhether hash has the default driver's format and costs; no derivation
needsReHashneedsReHash(hash: string): booleanWhether hash was made by another algorithm or other parameters than the current config
assertEqualsassertEquals(hash: string, value: string): Promise<void>Throws an AssertionError when value does not verify; the message never contains value
assertNotEqualsassertNotEquals(hash: string, value: string): Promise<void>Throws an AssertionError when value verifies
useuse(name?: string): HasherThe named hasher, or the default one; throws E_SIGIL_UNKNOWN_HASHER for an undeclared name
fakefake(): { [Symbol.dispose](): void }Replace every hasher with a fake: make returns the value, verify compares strings. using guard = hash.fake() restores at scope exit
restorerestore(): voidLeave fake mode

Hasher ​

new Hasher(driver: HashDriver). It has the same isValidHash, make, verify, needsReHash, assertEquals and assertNotEquals members as Hash, run against its own driver.

make and verify run the derivation on the libuv thread pool (4 threads unless UV_THREADPOOL_SIZE says otherwise), never on the event loop. make throws for a value longer than 1024 bytes. bcrypt's make throws for a value longer than 72 bytes rather than truncate it, and its verify answers false.

verify returns false without deriving when a stored hash costs more than any real configuration: argon2 memory × iterations above 2^20, a bcrypt cost above 16, a scrypt N × r × p above 2^22 or a working set 128 × r × (N + p + 2) above maxMemory. A configuration above these limits throws when the driver is built.

@c9up/sigil/provider ​

ExportKindDescription
defaultproviderSigilProvider: binds Hash from the hash config and fills @c9up/sigil/services/main at boot
SigilAppContextinterface{ container, config }, what the provider needs from the application

@c9up/sigil/services/main ​

ExportKindDescription
defaultconsthash: Hash, the application's Hash once SigilProvider booted; any member read before throws E_SIGIL_NOT_BOOTED
ts
import hash from '@c9up/sigil/services/main'

const digest = await hash.make(password)
if (await hash.verify(digest, password) && hash.needsReHash(digest)) {
  user.password = await hash.make(password)
}

@c9up/sigil/configure ​

ExportKindDescription
configurefunctionconfigure(codemods): Promise<void>: adds HASH_DRIVER, registers @c9up/sigil/provider, writes config/hash.ts

Configuration ​

config/hash.ts, read under the hash key. Without it, the provider uses { default: 'argon2', list: { argon2: { driver: 'argon2' } } }.

ts
import { defineConfig, drivers } from '@c9up/sigil'
import env from '#start/env'

export default defineConfig({
  default: env.get('HASH_DRIVER', 'argon2'),
  list: {
    argon2: drivers.argon2({ secret: env.get('APP_KEY') }),
    bcrypt: drivers.bcrypt({ rounds: 12 }),
  },
})
OptionTypeDefaultDescription
defaultstring—Name of the hasher make, verify and use() use
listRecord<string, HashDriverConfig>—The hashers, by name
driversRecord<string, HashDriverConfig>—Older name of list; both are read, list wins on a shared key

drivers.argon2(options) (argon2 driver):

OptionTypeDefaultDescription
variant'd' | 'i' | 'id''id'Argon2 variant
memorynumber65536Memory in KiB, at least 8 × parallelism; memoryKib is read as an alias
iterationsnumber3Passes, at least 2
parallelismnumber4Lanes
saltSizenumber16Salt bytes, 8 to 48
hashLengthnumber32Output bytes, 10 to 64
secretstring | ReleasableSecret—Pepper mixed into every hash and not stored in it; verifying needs the same secret

drivers.bcrypt(options) (bcrypt driver):

OptionTypeDefaultDescription
roundsnumber12Cost, 10 to 16
versionnumber9897 writes $2a$, 98 writes $2b$, 121 writes $2y$
saltSizenumber16Only 16 is accepted

drivers.scrypt(options) (scrypt driver):

OptionTypeDefaultDescription
costnumber16384N, a power of two
blockSizenumber8r
parallelizationnumber1p
keyLengthnumber64Key bytes, 64 to 128
saltSizenumber16Salt bytes, 8 to 1024; saltLength is read as an alias
maxMemorynumber33554432Bytes, at least the working set 128 × r × (N + p + 2); bounds verify too

bcrypt writes the MCF form ($2b$12$…) and also verifies the PHC form $bcrypt$v=98$r=…$…$…, for which needsReHash answers true. scrypt writes the PHC form $scrypt$n=…,r=…,p=…$<salt>$<hash>.

Container bindings ​

TokenResolves to
Hash (class)The Hash built from the hash config
'sigil.hash'The same Hash
'hash'The same Hash

'sigil.hash' and 'hash' are declared in ContainerBindings, so container.make('sigil.hash') is typed.

Errors ​

CodeRaised when
E_SIGIL_UNKNOWN_DRIVERA hasher in the config names a driver other than argon2, bcrypt or scrypt; thrown by the Hash constructor
E_SIGIL_UNKNOWN_HASHERdefault (in the constructor) or use(name) names a hasher the config does not declare
E_SIGIL_NAPI_REQUIREDmake or verify ran without the native engine loaded
E_SIGIL_NOT_BOOTED@c9up/sigil/services/main was read before SigilProvider.boot()
E_SIGIL_INVALID_OPTIONA driver option is not an integer, out of range, not an allowed value, a scrypt cost that is not a power of two, or a combination costing more than verify accepts; thrown when the driver is built
E_SIGIL_PASSWORD_TOO_LONGmake received a password longer than the shared byte cap, or longer than 72 bytes for bcrypt

Each one is a class of the errors namespace and a @c9up/flare Exception with status 500: branch on error.code or on error instanceof errors.E_SIGIL_*.

Migration ​

  • SigilError is gone: test error.code, or error instanceof errors.E_SIGIL_* with import { errors } from '@c9up/sigil'.
  • The error classes are no longer exported one by one: E_SIGIL_NAPI_REQUIRED becomes errors.E_SIGIL_NAPI_REQUIRED, and so on.
  • E_SIGIL_INVALID_OPTION is no longer a TypeError: replace instanceof TypeError with error.code === 'E_SIGIL_INVALID_OPTION'.
  • error.name is no longer the code: read error.code.

See also ​