npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-file-encryption

v1.2.1

Published

Simple public/private key encryption and decryption of files in your browser using the WebCrypto API.

Downloads

4

Readme

Simple public/private key file encryption 🔑

Encrypt and decrypt files in your browser using the WebCrypto API.

  • Simple API
  • Zero dependencies
  • Browser native features

This project aims at simplicity: You don't get a lot of options, but it's dead to encrypt and decrypt a file using Public/Private keys.

const encrypted = await encryptFile(file, publicKey);
const decrypted = await decryptFile(encrypted, privateKey);

Disclaimer: I am not a security expert and this project has no security audits. Do not use it if leaking information would put your personal safety at risk.

Background

The original use case for this library was a service that supported file uploads, but made these files public under a non-guessable link. Encrypting the files added an extra layer of protection in case an URL is leaked accidentally or intentionally.

How it works

Encryption works by encrypting the whole message with AES-CBC and a random key. The key is then encrypted using a provided public key. The result is packed in the following format:

| 4 Bytes | 16 Bytes | <Keylength> Bytes | Rest of buffer |
| Keylength | AES IV | Encrypted AES Key | Encrypted Payload | `

Encryption

Encryption schema

Decryption

Decryption schema

Usage

  1. Install the package
npm install simple-file-encryption
  1. Encrypt a file
import { encryptFile } from "simple-file-encryption";

const publicKey = `-----BEGIN PUBLIC KEY-----
MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAx0Vy
<...snip...>
+1u67KNkH3KWb6hBk2Meg6QvhrRNszGcI5GxAgMBAAE=
-----END PUBLIC KEY-----`

const file: File = //... a file from a form

/** Result is a file object **/
const result = await encryptFile(file, publicKey);
  1. Decrypt a file
import { decryptFile } from "simple-file-encryption";

/** Make sure this key is not shared **/
const privateKey = `-----BEGIN PRIVATE KEY-----
MIIG/QIBADANBgkqhkiG9w0BAQEFAASCBucwggbjAgEAAoIBgQDHRXLoudUu5afI
<...snip...>
+1u67KNkH3KWb6hBk2Meg6QvhrRNszGcI5GxAgMBAAE=
-----END PRIVATE KEY-----`

const file: File = //... a file from a form

/** Result is a file object **/
const plainText = await decryptFile(file, privateKey);

You can also take a look at the decrypt test for a full example.

Supported key formats

The private key needs to be in pcks#8 format, while the public key should be in spki format. You can run

npm run genkeys

in order to create a matching public/private key pair. Take a look at the rsa-keys module if you encounter issues with you key, in my tests WebCrypto was quite picky.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

MIT