envelope

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

webcrypto-envelope-go

Tiny, dependency-free AES-256-GCM envelope library for Go - the Go twin of webcrypto-envelope (TypeScript, Web Crypto). The two are wire-compatible: an envelope produced by either implementation opens in the other, and both test suites prove it with fixtures sealed by the opposite side.

Two patterns, one primitive (AES-256-GCM in a compact iv:tag:ciphertext base64 envelope):

  1. Password vault - derive a key from a password (PBKDF2-SHA-256, 600k iterations default) and encrypt/decrypt locally. Good for offline-first apps that sync ciphertext the server can't read.
  2. Sealed share - Seal encrypts under a fresh random key and hands the key back to you. Store the ciphertext anywhere (the server only ever holds ciphertext); deliver the key out-of-band - e.g. in a URL fragment, which browsers never send to the server - and the recipient Opens it. This is the zero-knowledge shareable-link primitive.

Standard library only - crypto/pbkdf2 landed in Go 1.24, so that is the minimum version. Every function returns an error rather than panicking on attacker-controlled input.

Install

go get github.com/JacobStephens2/webcrypto-envelope-go
import envelope "github.com/JacobStephens2/webcrypto-envelope-go"

Password vault

salt := envelope.RandomSalt() // store alongside the ciphertext

key, err := envelope.DeriveKey(password, salt, nil) // nil = 600k iterations
if err != nil {
	return err
}
env, err := envelope.Encrypt(`{"observations":[...]}`, key)
// env is "iv:tag:ciphertext" base64 - safe to store anywhere

plain, err := envelope.Decrypt(env, key) // errors on wrong key or tampering
key, env, err := envelope.Seal(payload)
if err != nil {
	return err
}
// Store env server-side; put key in the URL fragment:
shareURL := "https://app.example.com/share/" + id + "#" + url.PathEscape(key)

// Recipient side:
plain, err := envelope.Open(env, key)

The fragment never reaches the server, so the storage service is zero-knowledge with respect to the content.

Cross-language interop

The envelope format is identical across both implementations: 12-byte GCM IV, 16-byte tag, ciphertext - each standard-base64, joined with :. PBKDF2-SHA-256 keys derive identically from the same password, salt, and iteration count.

  • This repo's tests open envelopes sealed by the TypeScript library (TestOpensTypeScriptSealedEnvelopes).
  • The TypeScript repo's tests open envelopes sealed by this library.

Encrypt in a browser, decrypt in a Go service - or the reverse.

API

Function Purpose
DeriveKey(password, saltB64, opts) PBKDF2-SHA-256 → 32-byte AES key (opts may be nil)
Encrypt(plaintext, key) Encrypt to an iv:tag:ciphertext envelope, fresh IV per call
Decrypt(envelope, key) Open an envelope; errors on wrong key or tampering
Seal(plaintext) Encrypt under a fresh random key; returns (key, envelope, err)
Open(envelope, keyB64) Open a sealed payload with its base64 key
RandomSalt() Random base64 16-byte salt
RandomKey() Random base64 256-bit key

Security notes

  • GCM authenticates. Decrypt/Open fail on any tampering - there is no unauthenticated mode.
  • A fresh random IV per Encrypt call. Never reuse an envelope's IV with the same key for new plaintext.
  • PBKDF2 is for passwords only. Seal/Open use a full-entropy random key and skip derivation entirely.
  • The envelope is confidential, not anonymous. Lengths leak: ciphertext length equals plaintext length.

License

MIT © Jacob Stephens

Documentation

Overview

Package envelope is a tiny, dependency-free AES-256-GCM envelope library - the Go twin of github.com/JacobStephens2/webcrypto-envelope (TypeScript, Web Crypto). The two are wire-compatible: an envelope produced by either implementation opens in the other.

Two patterns, one primitive (AES-256-GCM in a compact "iv:tag:ciphertext" base64 envelope):

  1. Password vault - derive a key from a password (PBKDF2-SHA-256) and encrypt/decrypt locally. Good for offline-first apps that sync ciphertext the server can't read.
  2. Sealed share - Seal encrypts under a fresh random key and hands the key back to you. Store the ciphertext anywhere (the server only ever holds ciphertext); deliver the key out-of-band - e.g. in a URL fragment, which browsers never send to the server - and the recipient Opens it. This is the zero-knowledge shareable-link primitive.

Standard library only (crypto/pbkdf2 needs Go 1.24+). Every function returns an error rather than panicking on attacker-controlled input.

Index

Constants

View Source
const DefaultIterations = 600_000

DefaultIterations is the PBKDF2 iteration count used when DeriveOptions is nil or its Iterations field is zero. It matches the TypeScript twin.

Variables

View Source
var ErrInvalidEnvelope = errors.New("invalid envelope format (expected iv:tag:ciphertext)")

ErrInvalidEnvelope is returned when an envelope is not three base64 parts separated by colons.

Functions

func Decrypt

func Decrypt(envelopeStr string, key []byte) (string, error)

Decrypt opens an "iv:tag:ciphertext" envelope with a 32-byte key. It returns an error if the envelope is malformed, the key is wrong, or the data was tampered with.

func DeriveKey

func DeriveKey(password, saltB64 string, opts *DeriveOptions) ([]byte, error)

DeriveKey derives a 32-byte AES-256-GCM key from a password and a base64 salt using PBKDF2-SHA-256.

func Encrypt

func Encrypt(plaintext string, key []byte) (string, error)

Encrypt encrypts a UTF-8 string under a 32-byte key. It returns a compact "iv:tag:ciphertext" base64 envelope with a fresh random IV per call.

func Open

func Open(envelopeStr, keyB64 string) (string, error)

Open opens a sealed payload with its base64 key.

func RandomKey

func RandomKey() string

RandomKey returns a random base64 256-bit key - e.g. for a sealed, shareable payload.

func RandomSalt

func RandomSalt() string

RandomSalt returns a random base64 salt for password derivation (16 bytes).

func Seal

func Seal(plaintext string) (key, envelope string, err error)

Seal encrypts plaintext under a fresh random key and returns both.

Store the envelope wherever you like - it is opaque ciphertext, so the storage service can't read it. Deliver the key out-of-band (a URL fragment is ideal: browsers never send the fragment to the server). The recipient calls Open(envelope, key). The storage service is therefore zero-knowledge with respect to the content.

Types

type DeriveOptions

type DeriveOptions struct {
	Iterations int
}

DeriveOptions configures DeriveKey. A nil pointer (or zero Iterations) means DefaultIterations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL