@vanishing.page/crypto-stream
    Preparing search index...

    Class Keychain

    Index
    • Parameters

      • Optionalkey: string | Uint8Array<ArrayBufferLike> | null
      • Optionaloptions:
            | Salt
            | {
                encoding?: SupportedEncodings | undefined;
                salt?: Salt | null | undefined;
            }
            | null

      Returns Keychain

    authTokenPromise: Promise<Uint8Array<ArrayBufferLike>>
    key: Uint8Array<ArrayBuffer>
    mainKeyPromise: Promise<CryptoKey>
    metaKeyPromise: Promise<CryptoKey>
    salt: Uint8Array<ArrayBuffer>
    • get keyB64(): string

      Get the main key as a base64url encoded string

      Returns string

    • get saltB64(): string

      Get the salt as a base64url string.

      Returns string

    • Get a header string: Bearer sync-v1 ${authTokenB64}. Pass in a token, or else this will use the authToken derived from the main key.

      Parameters

      • OptionaltokenString: string

      Returns Promise<string>

    • get a promise for the auth token.

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • Get the auth token as a base64url string.

      Returns Promise<string>

    • SHA-256 digest of the plaintext, used as the input to content-bound salt derivation. Accepts a stream, a byte array, or a Blob; equivalent content yields the same digest.

      Pass the result to encryptStream/header/encryptRecord as contentDigest. The salt is derived from this digest internally, so the Keychain API never exposes a raw salt — a fixed salt can never pair with two different plaintexts.

      NOTE: WebCrypto has no incremental hash, so a stream/Blob is drained into memory before hashing.

      Parameters

      • content: Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Blob

        Plaintext as a ReadableStream, Uint8Array, or Blob.

      Returns Promise<Uint8Array<ArrayBuffer>>

      32-byte SHA-256 digest.

    • Decrypt in memory, not streaming.

      Parameters

      • bytes: Uint8Array
      • Optionalopts: { size?: number }

        Pass the same size that was given to encryptBytes, so the same-length key is re-derived.

      Returns Promise<ArrayBuffer>

    • Parameters

      • ivEncryptedMeta: Uint8Array

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • Take an encrypted stream, return a decrypted stream.

      Parameters

      • encryptedStream: ReadableStream<Uint8Array<ArrayBufferLike>>

        The input (encrypted) stream

      • Optionalopts: { recordSize?: number }

        { recordSize? }. Optional validation hint. Stream decryption reads the ECE salt and record size from the encrypted stream header.

      Returns Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>

      The decrypted stream

    • Returns an object containing ranges, an array of objects containing offset and length integers specifying the encrypted byte ranges that are needed to decrypt the client's specified range, and a decrypt function.

      Parameters

      • offset: number

        Integer

      • length: number

        Integer

      • totalEncryptedLength: number

        Integer

      • Optionalopts: { recordSize?: number }

        Pass the same recordSize that was used to encrypt the stream; defaults to RECORD_SIZE (65536).

      Returns Promise<
          {
              decrypt: (streams: ReadableStream<any>[]) => ReadableStream;
              ranges: { length: number; offset: number }[];
          },
      >

    • Encrypt and return some data; don't stream.

      NOTE: The key is derived deterministically from the main key via this.generateKey, not freshly generated — that is why decryptBytes is able to re-derive the same key and decrypt.

      SAFETY: if a caller-supplied iv is reused across two calls with different plaintexts (same derived key), that is AES-GCM nonce reuse and breaks confidentiality. Omit iv to get a random one per call.

      Parameters

      • bytes: ArrayBuffer | Uint8Array<ArrayBufferLike>
      • Optionalopts: { iv?: Uint8Array<ArrayBufferLike>; size?: number }

        Optional params, iv and size. If size is omitted, default is 16 bytes. iv is a random 12 bytes, will be generated if not passed in.

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • Parameters

      • meta: Uint8Array

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • Encrypt a single ECE record by index, byte-identical to record seq of encryptStream({ contentDigest, recordSize }). The salt is derived from contentDigest internally; no raw salt is accepted.

      Non-final records must be exactly recordPlaintextSize(recordSize) bytes; the final record must be <= that. The low-level function throws otherwise.

      Parameters

      • seq: number

        Zero-based record index.

      • plaintext: Uint8Array

        The record's plaintext slice.

      • opts: { contentDigest: Uint8Array; isLast: boolean; recordSize?: number }

        { isLast, contentDigest, recordSize? }.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The encrypted record bytes.

    • Take a stream, return an encrypted stream.

      With opts.contentDigest, the ECE salt is derived from the digest internally (deriveContentSalt), so two calls over identical input produce byte-identical ciphertext (reproducible encryption). With no opts, a fresh random salt is used (today's behavior).

      The salt is never accepted directly: because it is bound to the content digest, a fixed salt can never encrypt two different plaintexts (AES-GCM nonce-reuse is structurally unreachable from this API).

      Parameters

      • stream: ReadableStream<Uint8Array<ArrayBufferLike>>

        Input plaintext stream.

      • Optionalopts: { contentDigest?: Uint8Array<ArrayBufferLike>; recordSize?: number }

        Optional { contentDigest?, recordSize? }.

      Returns Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>

      Encrypted stream.

    • Derive a new AES-GCM key from the main key.

      Parameters

      • OptionalkeyLength: number

        Optional size for the key, in bytes, eg, 16 or 32.

      Returns Promise<CryptoKey>

    • Build the 21-byte ECE header for content identified by contentDigest. The salt is derived internally from the digest, so this never exposes a raw salt. The header is byte-identical to the header that encryptStream({ contentDigest, recordSize }) emits for the same input.

      Parameters

      • opts: { contentDigest: Uint8Array; recordSize?: number }

        { contentDigest, recordSize? }.

      Returns Promise<Uint8Array<ArrayBuffer>>

      The 21-byte header.

    • Set the auth token

      Parameters

      • OptionalauthToken: string | Uint8Array<ArrayBufferLike>

        The new token

      Returns void

    • Get an authentication header as a static method.

      Parameters

      • secretKey: string
      • salt: string | Uint8Array<ArrayBufferLike>

      Returns Promise<string>

    • Parameters

      • writeToken: string

      Returns string