react-native-matrix-crypto
    Preparing search index...

    Function encryptAttachment

    • Encrypts a file, and hands back the ciphertext to upload and the secret that opens it.

      Why this library encrypts, rather than handing you a key

      This is the same split buildHistoryBundle documents, and it is here for the same reason: that one was tried the other way round first. Handing a product a plaintext and a key is, on React Native, an instruction to implement Matrix's attachment encryption in JavaScript — a platform with no AES and no SHA-256, and the most to lose if it is done wrong.

      What this does not do

      It does not upload. The media repository is yours: it has the homeserver's address, the access token and the retry policy, and a crypto library that acquired an HTTP client would have acquired all three.

      A fresh key every call, including for the same bytes. Encrypting one file twice gives two ciphertexts and two secrets, which costs nothing — the discarded one was never uploaded.

      The whole file is held in memory twice over, once as plaintext and once as ciphertext. That is a real limit and it is yours to respect: this takes bytes rather than a stream because the boundary it crosses has none.

      Parameters

      • plaintext: Uint8Array

        The file's bytes.

      Returns Promise<SealedAttachment>

      The bytes to upload, and the secret to keep.

      CryptoError if the file could not be encrypted.