hcrypto

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 16 Imported by: 0 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.

Documentation

Overview

Package hcrypto implements the Hemmelig encryption scheme.

The scheme matches src/lib/crypto.ts in the web app:

  • PBKDF2 with SHA-256 and 1,300,000 iterations derives a 256-bit key from the secret key (or the password) and the salt.
  • AES-256-GCM encrypts the data with a random 96-bit IV.
  • The encrypted message is the IV followed by the ciphertext and the tag.
  • The password verifier is the hex SHA-256 digest of the derived key.

Index

Constants

View Source
const (
	// Iterations is the PBKDF2 iteration count of the web app.
	Iterations = 1300000
	// KeyLength is the length of a generated key and of a generated salt.
	KeyLength = 32
)
View Source
const (
	// FileFormatV1 is one AES-GCM message: the IV, the ciphertext and the tag.
	FileFormatV1 = "v1"
	// FileFormatV2 is the chunked format of the web app. Each chunk has its
	// own IV and tag, and its index and a final flag in the additional data.
	FileFormatV2 = "v2"
)

File formats of attachments. The server stores the format next to the file.

View Source
const (
	// DefaultChunkSize is the plaintext size of one v2 chunk, as in the web app.
	DefaultChunkSize = 4 * 1024 * 1024
)

Variables

View Source
var ErrDecrypt = errors.New("could not decrypt: the key or password is wrong, or the data is damaged")

ErrDecrypt means that the key, the password or the data is wrong.

View Source
var ErrFileFormat = errors.New("the file is not in a known Hemmelig format")

ErrFileFormat means that an attachment is not in a known format.

Functions

func DecryptInboxMessage

func DecryptInboxMessage(private *ecdh.PrivateKey, envelope *InboxEnvelope) ([]byte, error)

DecryptInboxMessage decrypts an envelope with the inbox private key. The CLI does not hold inbox keys. Tests use this function to check EncryptForInbox.

func DeriveKey

func DeriveKey(secret, salt string) []byte

DeriveKey derives the 256-bit AES key from a secret key or a password.

func GenerateKey

func GenerateKey() (string, error)

GenerateKey returns a new random 32-character secret key.

func GenerateSalt

func GenerateSalt() (string, error)

GenerateSalt returns a new random 32-character salt.

func HexToBytes

func HexToBytes(value string) ([]byte, bool)

HexToBytes decodes a hex string. It returns false for empty or invalid input, like hexToBytes in the web app.

func IsV2

func IsV2(data []byte) bool

IsV2 reports whether data starts with the v2 header.

func ParseInboxPublicKey

func ParseInboxPublicKey(value string) (*ecdh.PublicKey, error)

ParseInboxPublicKey reads an inbox public key: a P-256 key in SPKI form, standard base64.

func PasswordVerifier

func PasswordVerifier(password, salt string) string

PasswordVerifier returns the value that the server stores for a password-protected secret. The server never sees the password or the key.

func RandomString

func RandomString(n int) (string, error)

RandomString returns n random characters from the nanoid alphabet.

func VerifierFromKey

func VerifierFromKey(derived []byte) string

VerifierFromKey returns the password verifier for an already derived key.

Types

type Cipher

type Cipher struct {
	// contains filtered or unexported fields
}

Cipher encrypts and decrypts with one derived key. Derive the key once per secret, because PBKDF2 with this iteration count is slow on purpose.

func NewCipher

func NewCipher(secret, salt string) (*Cipher, error)

NewCipher derives the key for secret and salt and returns a Cipher.

func NewCipherFromKey

func NewCipherFromKey(derived []byte) (*Cipher, error)

NewCipherFromKey returns a Cipher for a derived 32-byte key.

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(message []byte) ([]byte, error)

Decrypt reverses Encrypt.

func (*Cipher) DecryptFile

func (c *Cipher) DecryptFile(data []byte) ([]byte, error)

DecryptFile decrypts an attachment in the v1 or the v2 format.

func (*Cipher) DecryptString

func (c *Cipher) DecryptString(message []byte) (string, error)

DecryptString decrypts to UTF-8 text.

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(plain []byte) ([]byte, error)

Encrypt returns the IV followed by the ciphertext and the tag.

func (*Cipher) EncryptFile

func (c *Cipher) EncryptFile(plain []byte) ([]byte, error)

EncryptFile encrypts a file in the v2 chunked format of the web app.

func (*Cipher) EncryptString

func (c *Cipher) EncryptString(text string) ([]byte, error)

EncryptString encrypts UTF-8 text.

func (*Cipher) Verifier

func (c *Cipher) Verifier() string

Verifier returns the password verifier for the key of this Cipher.

type InboxEnvelope

type InboxEnvelope struct {
	// EphemeralPublicKey is the one-time public key of the sender in SPKI form.
	EphemeralPublicKey string `json:"ephemeralPublicKey"`
	HKDFSalt           string `json:"hkdfSalt"`
	IV                 string `json:"iv"`
	Ciphertext         string `json:"ciphertext"`
}

InboxEnvelope is an encrypted inbox message. All values are standard base64.

func EncryptForInbox

func EncryptForInbox(recipient *ecdh.PublicKey, plain []byte) (*InboxEnvelope, error)

EncryptForInbox encrypts a message for the inbox with this public key.

Jump to

Keyboard shortcuts

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