import ssh from 'micro-key-producer/ssh.js';import pgp from 'micro-key-producer/pgp.js';import slip10 from 'micro-key-producer/slip10.js';import * as webconv from 'micro-key-producer/convert.js';import ipns from 'micro-key-producer/ipns.js';import tor from 'micro-key-producer/tor.js';import { secureMask } from 'micro-key-producer/password.js';import { hotp, totp } from 'micro-key-producer/otp.js';import { randomBytes } from 'micro-key-producer/utils.js';
Every method takes a seed (key), from which the formatted result is produced.
A seed can be deterministic (a.k.a. known - it will always produce the same result), or random.
npm install @scure/bip39 for mnemonic-based examples
// known: (deterministic) Uses known mnemonic (handled in separate package)import { mnemonicToSeedSync } from '@scure/bip39';const mnemonic = 'letter advice cage absurd amount doctor acoustic avoid letter advice cage above';const knownSeed = mnemonicToSeedSync(mnemonic, '');// random: Uses system's CSPRNG to produce new random seedimport { randomBytes } from 'micro-key-producer/utils.js';const randSeed = randomBytes(32);
The PGP (GPG) keys conform to
RFC 4880 &
RFC 6637. Only ed25519 algorithm is currently supported.
Password-protected private keys use a memory-hard Argon2id S2K with AES-256-GCM
(RFC 9580) by default, which GnuPG 2.4
and earlier cannot import. Pass { protection: 'legacy' } as the last argument to emit
the iterated-SHA-1 + AES-128-CFB envelope those versions understand:
Point gpgkp to your armored private key (e.g. generated with pgp.getKeys()) via
environment variable. If the key is password-protected, gpgkp prompts on the terminal
for each signature:
export GPGKP_KEY="$HOME/.config/gpgkp/key.asc"export GPGKP_HIDE_PASSWORD=1 # optional: don't echo '*' while typing the password
slip10: bip32-like ed25519 keys
import slip10 from 'micro-key-producer/slip10.js';import { sha256 } from '@noble/hashes/sha2.js';import { randomBytes } from 'micro-key-producer/utils.js';const seed = randomBytes(32);const root = slip10.fromMasterSeed(seed);const account0 = root.derive("m/0'");const signing = root.derive("m/0/2147483647'/1'", true);const msgHash = sha256(new TextEncoder().encode('hello slip10'));// props[root.depth, root.index, root.chainCode];[account0.privateKey, account0.publicKey];const sig = signing.sign(msgHash);signing.verify(msgHash, sig);
SLIP10 (ed25519 BIP32) HDKey implementation has been funded by the Kin Foundation for
Kinetic.
The module allows to convert between "raw" noble-curves format and WebCrypto formats (JWK, DER, PKCS, SPKI).
tor: keys and addresses
import tor from 'micro-key-producer/tor.js';import { randomBytes } from 'micro-key-producer/utils.js';const seed = randomBytes(32);const key = tor(seed);console.log(key.privateKey, key.publicKey);// ED25519-V3:EOl78M2gA...// rx724x3oambzxr46pkbd... .onion
ipns: addresses
import ipns from 'micro-key-producer/ipns.js';import { randomBytes } from 'micro-key-producer/utils.js';const seed = randomBytes(32);const k = ipns(seed);console.log(k.privateKey, k.publicKey, k.base16, k.base32, k.base36, k.contenthash);// 0x080112400681d6420abb1b...// 0x017200240801122012c829...// ipns://f0172002408011220...// ipns://bafzaajaiaejcaewi...// ipns://k51qzi5uqu5dgnfwb...// 0xe501017200240801122012...
password: secure passwords with masks
import { legacySecureMask, mask, secureMask } from 'micro-key-producer/password.js';import { randomBytes } from '@noble/hashes/utils.js';const seed = randomBytes(32);const pass = secureMask.apply(seed).password;// wivfi1-Zykrap-fohcij, will change on each run// secureMask follows the default Apple Passwords / iOS Keychain structure.// Only for reproducing passwords derived by versions before the format correction:const oldPass = legacySecureMask.apply(seed).password;
secureMask matches the public structure of Apple's generated strong passwords. Apple also
rejects candidates containing terms from a private on-device blocklist; that unavailable
filter is intentionally not reproduced here. The default format is compatible with most,
not all, service-specific password policies.
Supports iOS / macOS Safari Secure Password from Keychain. Optional zxcvbn score for password bruteforce estimation
import * as x509 from 'micro-key-producer/x509.js';
Internals
PGP key generation
Generated private and public keys would have different representation, however, their
fingerprints would be the same. This is because AES encryption is used to hide the keys, and
AES requires different IV / salt.
The function is slow (400ms on Apple M4), because it uses S2K to derive keys.
"warning: lower 3 bits of the secret key are not cleared" happens even for keys generated with
GnuPG 2.3.6, because check looks at item as Opaque MPI, when it is just MPI: see
bugtracker URL.
import * as pgp from 'micro-key-producer/pgp.js';import { randomBytes } from 'micro-key-producer/utils.js';const pseed = randomBytes(32);pgp.getKeyId(pseed); // fastconst pkeys = pgp.getKeys(pseed, 'user@example.com', 'password');console.log(pkeys.keyId);console.log(pkeys.privateKey);console.log(pkeys.publicKey);// Also, you can explore existing keys internal structureconsole.log(pgp.pubArmor.decode(pkeys.publicKey));const privDecoded = pgp.privArmor.decode(pkeys.privateKey);console.log(privDecoded);// And receive raw private keys as bigintconsole.log({ ed25519: pgp.decodeSecretKey('password', privDecoded[0].data), cv25519: pgp.decodeSecretKey('password', privDecoded[3].data),});
Mask: Cvccvc-cvccvc-cvccv1 will generate Mavmuq-xadgys-poqsa5
Mask @Ss-ss-ss will generate: *Tavy-qyjy-vemo
Design rationale
Most strict password rules (so password will be accepted everywhere):
at least one upper-case character
at least one lower-case character
at least one symbol
at least one digit
length greater or equal to 8
These rules don't significantly increase password entropy (most humans will use mask like 'Aaaaaa1@' or any other popular mask),
but they means that we cannot simple use mask like ********, since it can generate passwords which won't satisfy these rules.
What do we want from passwords?
length: entering 32 character password for FDE via IPMI java applet on remote server is pretty painful.
-> 12-16 probably ok, anything with more characters has chance to be truncated by service.
readability: entering '!#%!$#Y^&*#%@#!!1' from air-gapped pc is hard.
80 bits is probably outside of budget for most attackers (btc hash rate) even if there is major speedup for specific algorithm
For websites and services we don't care much about entropy, since passwords are unique and there is no re-usage,
however for FDE / server password entropy is pretty important
no fancy and unique mask by default: we don't want to fingeprint users
any mask will leak eventually (even if user choices personal mask, there will be password leaks from websites),
so we cannot calculate entropy by ****** mask, we need to calculate entropy for specific mask (which is smaller).
Password generator should be reversible, that way we can easily proof entropy/strength of password.
SLIP10 API
SLIP-0010 hierarchical deterministic (HD) wallets for implementation. Based on code from
scure-bip32. Check out
scure-bip39 if you also need mnemonic phrases.
SLIP-0010 publicKey is 33 bytes (see
this issue), if you want 32-byte publicKey,
use .publicKeyRaw getter
SLIP-0010 vectors fingerprint is actually parentFingerprint
SLIP-0010 doesn't allow deriving non-hardened keys for Ed25519, however some other libraries treat
non-hardened keys (m/0/1) as hardened (m/0'/1'). If you want this behaviour, there is a flag
forceHardened in derive method
Note: chainCode property is essentially a private part of a secret "master" key, it should be
guarded from unauthorized access.
The full API is:
export const HARDENED_OFFSET: number;declare class HDKey { public static fromMasterSeed(seed: Uint8Array): HDKey; readonly depth: number; readonly index: number; readonly chainCode: Uint8Array; readonly parentFingerprint: number; public readonly privateKey: Uint8Array; get fingerprint(): number; get fingerprintHex(): string; get parentFingerprintHex(): string; get pubHash(): Uint8Array; get publicKey(): Uint8Array; get publicKeyRaw(): Uint8Array; derive(path: string, forceHardened?: boolean): HDKey; deriveChild(index: number): HDKey; sign(message: Uint8Array): Uint8Array; verify(message: Uint8Array, signature: Uint8Array): boolean;}
BLS EIP-2333, EIP-2334 key generation
BLS keygen (EIP-2333, EIP-2334) was previously available in the module.