UNPKG

micro-key-producer

Version:

Produces secure passwords & keys for WebCrypto, SSH, PGP, SLIP10, OTP and many others

388 lines 11.6 kB
import * as P from 'micro-packed'; /** Utility */ interface JsonWebKey { crv?: string | undefined; d?: string | undefined; dp?: string | undefined; dq?: string | undefined; e?: string | undefined; k?: string | undefined; kty?: string | undefined; n?: string | undefined; p?: string | undefined; q?: string | undefined; qi?: string | undefined; x?: string | undefined; y?: string | undefined; [key: string]: unknown; } type ECConverter<T, Opts = {}> = { publicKey: P.Coder<Uint8Array, T>; secretKey: { encode(from: Uint8Array, opts?: Opts): T; decode(to: T): Uint8Array; }; }; type JWKConverter = ECConverter<JsonWebKey>; type RSAKey = { version: bigint; modulus: bigint; publicExponent: bigint; privateExponent: bigint; prime1: bigint; prime2: bigint; exponent1: bigint; exponent2: bigint; coefficient: bigint; }; /** Elliptic-curve parameter encoding used by DER key structures. */ export type ECParams = { TAG: 'namedCurve'; data: string; } | { TAG: 'implicitCurve'; data: null; } | { TAG: 'specifiedCurve'; data: unknown; }; /** Algorithm identifier payload for DER key structures. */ export type KeyInfo = { TAG: 'EC'; data: ECParams; } | { TAG: 'X25519'; data: null; } | { TAG: 'X448'; data: null; } | { TAG: 'Ed25519'; data: null; } | { TAG: 'Ed448'; data: null; } | { TAG: 'rsaEncryption'; data: null; } | { TAG: 'DSA'; data: unknown; }; /** Top-level algorithm wrapper for DER key structures. */ export type Algo = { /** Algorithm identifier and its associated parameters. */ info: KeyInfo; }; type PKCS8Secret = { TAG: 'raw'; data: Uint8Array; } | { TAG: 'struct'; data: { version: bigint; privateKey: Uint8Array; parameters?: ECParams; publicKey?: Uint8Array; }; }; /** Decoded PKCS#8 private-key structure. */ export type PKCS8Key = { /** PKCS#8 version field. */ version: bigint; /** Algorithm identifier describing the wrapped private key. */ algorithm: Algo; /** Raw or structured private-key payload. */ privateKey: PKCS8Secret; /** Optional PKCS#8 attributes carried alongside the key. */ attributes?: Uint8Array[]; /** Optional public key attached to the private-key structure. */ publicKey?: Uint8Array; }; /** Decoded SubjectPublicKeyInfo structure. */ export type SPKIKey = { /** Algorithm identifier describing the public key. */ algorithm: Algo; /** Encoded public-key bytes. */ publicKey: Uint8Array; }; type ASN1TagCoder<T> = P.CoderType<T> & { tagByte: number; tagBytes: number[]; constructed: number; inner: P.CoderType<T>; }; type ASN1Pub = { debug: P.CoderType<any>; Integer: ASN1TagCoder<bigint>; OctetString: ASN1TagCoder<Uint8Array>; OID: ASN1TagCoder<string>; BitString: ASN1TagCoder<Uint8Array>; UTF8: ASN1TagCoder<string>; null: ASN1TagCoder<null>; choice: <T extends Record<string, P.CoderType<any>>>(variants: T) => P.CoderType<{ [K in keyof T]: { TAG: K; data: P.UnwrapCoder<T[K]>; }; }[keyof T]>; sequence: <T extends Record<string, any>>(fields: P.StructRecord<T>) => ASN1TagCoder<T>; set: <T>(inner: P.CoderType<T>) => ASN1TagCoder<T[]>; explicit: <T>(number: number, inner: P.CoderType<T>) => ASN1TagCoder<T>; implicit: <T>(number: number, inner: ASN1TagCoder<T>) => ASN1TagCoder<T>; optional: <T>(inner: ASN1TagCoder<T>) => ASN1TagCoder<T | undefined>; }; type DERUtilsPub = { BER: { decode: (src: Uint8Array, opts?: { allowBER?: boolean; }) => { nodes: BerNode[]; der: Uint8Array; }; encode: (nodes: BerNode[], der: Uint8Array) => Uint8Array; normalize: (src: Uint8Array, opts?: { allowBER?: boolean; }) => Uint8Array; }; ASN1: ASN1Pub; RSAPrivateKey: P.CoderType<RSAKey>; PKCS8SecretKey: P.CoderType<PKCS8Secret>; PKCS8: P.CoderType<PKCS8Key>; SPKI: P.CoderType<SPKIKey>; }; type BerNode = { len: number; lenBytes: number; indefinite: boolean; bitUnused?: number; children?: BerNode[]; cls: number; tagNum: number; cons: boolean; }; /** * Low-level DER, BER, ASN.1, PKCS#8, and SPKI helpers. * @example * Reach for the raw ASN.1 coders when you need to inspect key structures by hand. * ```ts * import { DERUtils } from 'micro-key-producer/convert.js'; * DERUtils.ASN1.OID.encode('1.2.840.10045.3.1.7'); * ``` */ export declare const DERUtils: DERUtilsPub; type DEROpts = { noPublicKey?: boolean; compressed?: boolean; }; type DERConverter = ECConverter<Uint8Array, DEROpts>; /** Named-curve OID table used by the DER helpers. */ export declare const CurveOID: { readonly 'P-256': "1.2.840.10045.3.1.7"; readonly 'P-384': "1.3.132.0.34"; readonly 'P-521': "1.3.132.0.35"; readonly brainpoolP256r1: "1.3.36.3.3.2.8.1.1.7"; readonly brainpoolP384r1: "1.3.36.3.3.2.8.1.1.11"; readonly brainpoolP512r1: "1.3.36.3.3.2.8.1.1.13"; }; /** * Maps a named-curve OID to its public curve name. * @param oid - Object identifier string. * @returns Known curve name or `OID:...` fallback. * @example * Convert a DER named-curve OID into the public curve name used by this package. * ```ts * import { CurveOID, curveOID } from 'micro-key-producer/convert.js'; * curveOID(CurveOID['P-256']); * ``` */ export declare const curveOID: (oid: string) => keyof typeof CurveOID | `OID:${string}`; /** * JWK converter for P-256 signing keys. * @example * Encode a freshly generated P-256 signing key as JWK. * ```ts * import { p256 } from '@noble/curves/nist.js'; * import { p256_jwk } from 'micro-key-producer/convert.js'; * p256_jwk.secretKey.encode(p256.utils.randomSecretKey()); * ``` */ export declare const p256_jwk: JWKConverter; /** * JWK converter for P-256 ECDH keys. * @example * Encode a P-256 private key for ECDH-oriented JWK consumers. * ```ts * import { p256 } from '@noble/curves/nist.js'; * import { p256_jwk_ecdh } from 'micro-key-producer/convert.js'; * p256_jwk_ecdh.secretKey.encode(p256.utils.randomSecretKey()); * ``` */ export declare const p256_jwk_ecdh: JWKConverter; /** * DER converter for P-256 keys. * @example * Encode the same P-256 secret key into DER/PKCS#8 form. * ```ts * import { p256 } from '@noble/curves/nist.js'; * import { p256_der } from 'micro-key-producer/convert.js'; * p256_der.secretKey.encode(p256.utils.randomSecretKey()); * ``` */ export declare const p256_der: DERConverter; /** * JWK converter for P-384 signing keys. * @example * Encode a freshly generated P-384 signing key as JWK. * ```ts * import { p384 } from '@noble/curves/nist.js'; * import { p384_jwk } from 'micro-key-producer/convert.js'; * p384_jwk.secretKey.encode(p384.utils.randomSecretKey()); * ``` */ export declare const p384_jwk: JWKConverter; /** * JWK converter for P-384 ECDH keys. * @example * Encode a P-384 private key for ECDH-oriented JWK consumers. * ```ts * import { p384 } from '@noble/curves/nist.js'; * import { p384_jwk_ecdh } from 'micro-key-producer/convert.js'; * p384_jwk_ecdh.secretKey.encode(p384.utils.randomSecretKey()); * ``` */ export declare const p384_jwk_ecdh: JWKConverter; /** * DER converter for P-384 keys. * @example * Encode the same P-384 secret key into DER/PKCS#8 form. * ```ts * import { p384 } from '@noble/curves/nist.js'; * import { p384_der } from 'micro-key-producer/convert.js'; * p384_der.secretKey.encode(p384.utils.randomSecretKey()); * ``` */ export declare const p384_der: DERConverter; /** * JWK converter for P-521 signing keys. * @example * Encode a freshly generated P-521 signing key as JWK. * ```ts * import { p521 } from '@noble/curves/nist.js'; * import { p521_jwk } from 'micro-key-producer/convert.js'; * p521_jwk.secretKey.encode(p521.utils.randomSecretKey()); * ``` */ export declare const p521_jwk: JWKConverter; /** * JWK converter for P-521 ECDH keys. * @example * Encode a P-521 private key for ECDH-oriented JWK consumers. * ```ts * import { p521 } from '@noble/curves/nist.js'; * import { p521_jwk_ecdh } from 'micro-key-producer/convert.js'; * p521_jwk_ecdh.secretKey.encode(p521.utils.randomSecretKey()); * ``` */ export declare const p521_jwk_ecdh: JWKConverter; /** * DER converter for P-521 keys. * @example * Encode the same P-521 secret key into DER/PKCS#8 form. * ```ts * import { p521 } from '@noble/curves/nist.js'; * import { p521_der } from 'micro-key-producer/convert.js'; * p521_der.secretKey.encode(p521.utils.randomSecretKey()); * ``` */ export declare const p521_der: DERConverter; /** * JWK converter for Ed25519 keys. * @example * Encode an Ed25519 secret key into JWK form. * ```ts * import { ed25519 } from '@noble/curves/ed25519.js'; * import { ed25519_jwk } from 'micro-key-producer/convert.js'; * ed25519_jwk.secretKey.encode(ed25519.utils.randomSecretKey()); * ``` */ export declare const ed25519_jwk: JWKConverter; /** * DER converter for Ed25519 keys. * @example * Encode the same Ed25519 secret key into DER/PKCS#8 form. * ```ts * import { ed25519 } from '@noble/curves/ed25519.js'; * import { ed25519_der } from 'micro-key-producer/convert.js'; * ed25519_der.secretKey.encode(ed25519.utils.randomSecretKey()); * ``` */ export declare const ed25519_der: DERConverter; /** * JWK converter for Ed448 keys. * @example * Encode an Ed448 secret key into JWK form. * ```ts * import { ed448 } from '@noble/curves/ed448.js'; * import { ed448_jwk } from 'micro-key-producer/convert.js'; * ed448_jwk.secretKey.encode(ed448.utils.randomSecretKey()); * ``` */ export declare const ed448_jwk: JWKConverter; /** * DER converter for Ed448 keys. * @example * Encode the same Ed448 secret key into DER/PKCS#8 form. * ```ts * import { ed448 } from '@noble/curves/ed448.js'; * import { ed448_der } from 'micro-key-producer/convert.js'; * ed448_der.secretKey.encode(ed448.utils.randomSecretKey()); * ``` */ export declare const ed448_der: DERConverter; /** * JWK converter for X25519 keys. * @example * Encode an X25519 private key into JWK form. * ```ts * import { x25519 } from '@noble/curves/ed25519.js'; * import { x25519_jwk } from 'micro-key-producer/convert.js'; * x25519_jwk.secretKey.encode(x25519.utils.randomSecretKey()); * ``` */ export declare const x25519_jwk: JWKConverter; /** * DER converter for X25519 keys. * @example * Encode the same X25519 secret key into DER/PKCS#8 form. * ```ts * import { x25519 } from '@noble/curves/ed25519.js'; * import { x25519_der } from 'micro-key-producer/convert.js'; * x25519_der.secretKey.encode(x25519.utils.randomSecretKey()); * ``` */ export declare const x25519_der: DERConverter; /** * JWK converter for X448 keys. * @example * Encode an X448 private key into JWK form. * ```ts * import { x448 } from '@noble/curves/ed448.js'; * import { x448_jwk } from 'micro-key-producer/convert.js'; * x448_jwk.secretKey.encode(x448.utils.randomSecretKey()); * ``` */ export declare const x448_jwk: JWKConverter; /** * DER converter for X448 keys. * @example * Encode the same X448 secret key into DER/PKCS#8 form. * ```ts * import { x448 } from '@noble/curves/ed448.js'; * import { x448_der } from 'micro-key-producer/convert.js'; * x448_der.secretKey.encode(x448.utils.randomSecretKey()); * ``` */ export declare const x448_der: DERConverter; export {}; //# sourceMappingURL=convert.d.ts.map