# Welcome to Crypto-Middleware

This documentation provides all the information needed to start using the **crypto-middleware API tgo start  securing your web applications.**

{% hint style="info" %}
**Good to know: Crypto-middleware works with the Web Cryptography API & Streams API. Your browser must be able to support this,** \
**Check :** [**https://caniuse.com/?search=web%20crypto**](https://caniuse.com/?search=web%20crypto)  **(97% browser support)**

**Check :** [**https://caniuse.com/streams**](https://caniuse.com/streams)  **(78% browser support)**
{% endhint %}

## About

Crypto-middleware is a middleware library that can be installed in your client side javascript web application.  It proxies your  File object, converts it to a stream, encrypts it using a passphrase and returns back a transform stream which can be consumed however you like. You can Read the stream to perform other actions, or you can write the stream directly to your cloud storage

## Install the library

The best way to interact with our API is to use one of our official libraries:

{% tabs %}
{% tab title="Node" %}
{% code title="In your node terminal:" %}

```javascript
# Install via NPM
npm install --save crypto-middlware
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Good to know:** Import as a ES Module
{% endhint %}

***

## Quick Start

## Add the import to your React/Vue/Svelte component or import it directly in your main.js or index.js file

{% code title="In your component :" %}

```javascript
import * as secure from "crypto-middleware";
```

{% endcode %}

**Crypto-middleware exposes many methods, but you may only need two that does most of the heavy lifting for you.**

* [startStreaming](/crypto-middleware-npm-package/reference/api-reference/startstreaming.md)
* [decryptStream](/crypto-middleware-npm-package/reference/api-reference/decryptstream-passphrase.md)

```javascript
 const stream = await secure.startStreaming(file, passphrase);
 //Returns a TransoformStream
```

{% hint style="info" %}
**startStreaming is a Asynchronous, you will need to await the return TransformStream**
{% endhint %}

| Arguments  | Type          | Description                 | Required |
| ---------- | ------------- | --------------------------- | -------- |
| file       | `File Object` | HTML file input object      | Yes      |
| passphrase | String        | Enforce a strong passphrase | Yes      |

{% hint style="info" %}
**Good to know:** startStreaming will convert the File Object in to a stream, you only need to send the File reference, not the file buffer object!
{% endhint %}

## Example Encryption.

This example, takes in a File object, and a passphrase.

The example uses AWS to upload the file. The whole process will be streamed with low latency and memory consumption.

```html
<!DOCTYPE html>
<html>
<head>
  <title>File Encryption and Upload</title>
  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.0.min.js"></script>
  <script src="path/to/crypto-middleware.js"></script>
</head>
<body>
  <h1>File Encryption and Upload</h1>
  
  <input type="file" id="fileInput">
  <br><br>
  
  <label for="passphraseInput">Passphrase:</label>
  <input type="text" id="passphraseInput">
  <br><br>
  
  <button onclick="encryptAndUploadFile()">Encrypt and Upload</button>
  
  <script>
    async function encryptAndUploadFile() {
      const fileInput = document.getElementById('fileInput');
      const passphraseInput = document.getElementById('passphraseInput');
      
      const file = fileInput.files[0];
      const passphrase = passphraseInput.value;
      
      if (file && passphrase) {
        try {
          // Encrypt the file using crypto-middleware
          const response = await secure.startStreaming(file, passphrase);
          
          // Configure AWS SDK with your credentials and region
          AWS.config.update({
            accessKeyId: 'YOUR_ACCESS_KEY',
            secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
            region: 'YOUR_REGION'
          });
          
          // Create an instance of the AWS S3 upload streaming class
          const s3 = new AWS.S3();
          
          // Set up the upload parameters
          const uploadParams = {
            Bucket: 'YOUR_BUCKET_NAME',
            Key: file.name + '.encrypted',
            Body: response.readable()
          };
          
          // Upload the encrypted file to AWS S3
          const uploadResponse = await s3.upload(uploadParams)
          
          console.log('File uploaded successfully:', uploadResponse.Location);
        } catch (error) {
          console.error('Error encrypting or uploading file:', error);
        }
      } else {
        console.log('Please select a file and provide a passphrase.');
      }
    }
  </script>
</body>
</html>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://securesend.gitbook.io/crypto-middleware-npm-package/welcome-to-crypto-middleware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
