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

@mainframe/secure-file

v0.2.0

Published

Cryptographic utilities for files

Downloads

2

Readme

secure-file

Cryptographic utilities for files.

Installation

yarn add @mainframe/secure-file

Usage

import {
  createFileHash,
  writeEncryptedFile,
  readEncryptedFile,
  writeSignedFile,
  readSignedFile,
} from '@mainframe/secure-file'
import { decodeBase64, encodeBase64 } from '@mainframe/utils-base64'
import {
  PASSWORDHASH_ALG_ARGON2ID13,
  PASSWORDHASH_MEMLIMIT_SENSITIVE,
  PASSWORDHASH_OPSLIMIT_SENSITIVE,
  createPasswordHashSalt,
  createSecretBoxKey,
  createSecretBoxKeyFromPassword,
  createSignKeyPair,
} from '@mainframe/utils-crypto'

// Get a file hash
const hash = await createFileHash('/path/to/file')

// Encrypt and decrypt a file using a provided key
const encryptData = Buffer.from('secret')
const encryptKey = createSecretBoxKey()
await writeEncryptedFile('/path/to/file', encryptData, encryptKey)
const fileData = readEncryptedFile('/path/to/file', encryptKey)
encryptData.equals(fileData.opened) // true

// Encrypt and decrypt a file using a key derived from password
const password = Buffer.from('password')
// Function computing the secret key based on the know password and hashing config
const getKey = (metadata) => {
  return createSecretBoxKeyFromPassword(
    password,
    decodeBase64(metadata.salt),
    metadata.opslimit,
    metadata.memlimit,
    metadata.algorithm,
  )
}
const encryptData = Buffer.from('secret')
const metadata = {
  algorithm: PASSWORDHASH_ALG_ARGON2ID13,
  memlimit: PASSWORDHASH_MEMLIMIT_SENSITIVE,
  opslimit: PASSWORDHASH_OPSLIMIT_SENSITIVE,
  salt: encodeBase64(createPasswordHashSalt()),
}
await writeEncryptedFile(
  '/path/to/file',
  encryptData,
  await getKey(metadata),
  metadata, // Stored in clear in the file alongside encrypted contents
)
// getKey() will be used to retrieve the decryption key based on the stored metadata
const fileData = readEncryptedFile('/path/to/file', getKey)
encryptData.equals(fileData.opened) // true

// Sign using a single key
const signData = Buffer.from('important')
const signKey = createSignKeyPair()
await writeSignedFile('/path/to/file', signData)
const signedFile = await readSignedFile('/path/to/file')
// Public keys are stored in the file to verify the signed contents
signedFile.file.contents.signed.keys[0] === signKey.publicKey // true
signData.equals(signedFile.opened) // true

// Sign using multiple keys
const signData = Buffer.from('important')
const aliceKey = createSignKeyPair()
const bobKey = createSignKeyPair()
await writeSignedFile('/path/to/file', signData, [aliceKey, bobKey])
const signedFile = await readSignedFile('/path/to/file')
signData.equals(signedFile.opened) // true

Types

EncodedBox

base64 encoded version of the EncryptedBox from utils-crypt:

type EncodedBox = {
  cipher: base64,
  nonce: base64,
}

EncryptedContents

type EncryptedContents = {
  version: 1,
  type: 'encrypted',
  encrypted: EncodedBox,
}

SignedContents

type SignedContents = {
  version: 1,
  type: 'signed',
  signed: {
    keys: Array<base64>,
    message: base64,
  },
}

SecureContents

type SecureContents = EncryptedContents | SignedContents

SecureFile

Shape of the JSON data stored when writing a secure file to disk.

type SecureFile<T = SecureContents> = {
  version: 1,
  contents: T,
  metadata?: Object,
}

OpenedSecureFile

Shape of the JSON data returned after reading a secure file from disk. opened is only provided if the file has successfully been decrypted or having verified signatures.

type OpenedSecureFile<T> = {
  file: SecureFile<T>,
  opened: ?Buffer,
}

KeyExtractor

type KeyExtractor = (metadata?: Object) => Promise<Buffer>

API

createFileHash()

Calculates the hash of the provided file using hashStream() from utils-crypto.

Arguments

  1. path: string
  2. size?: number

Returns Promise<Buffer>

writeEncryptedFile()

Arguments

  1. path: string
  2. data: Buffer: data to encrypt
  3. key: Buffer: secret key
  4. metadata?: Object: optional additional metadata stored in clear

Returns Promise<void>

readEncryptedFile()

Arguments

  1. path: string
  2. key: Buffer | KeyExtractor: secret key or function returning a promise of the key using the stored metadata

Returns Promise<OpenedSecureFile<EncryptedContents>>

writeSignedFile()

Arguments

  1. path: string
  2. data: Buffer: data to sign
  3. key: KeyPair | Array<KeyPair>: single or list of keys used to sign the data. The public keys will be stored along with the signed message. The order of the keys matter as the message will be signed with each key using the provided sequence.
  4. metadata?: Object: optional additional metadata stored in clear

Returns Promise<void>

readSignedFile()

Arguments

  1. path: string
  2. keys?: Array<Buffer>: list of public keys to use to verify the signatures. The order of the keys must be the same as when calling writeSignedFile() (verification happens in reverse order of iteration). When not provided, the keys stored in the file will be used.

Returns Promise<OpenedSecureFile<SignedContents>>

License

MIT