flowcrypto

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NonceLength is the standard 96-bit (12-byte) nonce for AES-GCM.
	NonceLength = 12

	// TagLength is the 128-bit (16-byte) authentication tag for AES-GCM.
	TagLength = 16

	// MinBinaryPayloadLength is the minimum byte length for packed binary ciphertext:
	// Salt (16) + Nonce (12) + Tag (16) = 44 bytes.
	MinBinaryPayloadLength = SaltLength + NonceLength + TagLength
)
View Source
const (
	// ArmoredPrefixV1 is the header identifying text-armored version 1 encrypted payloads.
	ArmoredPrefixV1 = "FLOWENC:v1:"

	// SignaturePrefixV1 is the header identifying text-armored detached signatures.
	SignaturePrefixV1 = "FLOWSIG:v1:"

	// SignedPrefixV1 is the header identifying unified signed payload envelopes (FLOWSIGNED:v1:<format>:<sig>:<content>).
	SignedPrefixV1 = "FLOWSIGNED:v1:"
)
View Source
const (
	// DefaultKeyLength is the default length in bytes for raw AES-256 keys.
	DefaultKeyLength = 32

	// SaltLength is the length in bytes for PBKDF2 random salt.
	SaltLength = 16

	// PBKDF2Iterations is the iteration count for key derivation.
	PBKDF2Iterations = 100000
)

Variables

View Source
var (
	// ErrEmptyKey is returned when an empty encryption key is supplied.
	ErrEmptyKey = errors.New("encryption key cannot be empty")

	// ErrInvalidCiphertext is returned when ciphertext is too short or malformed.
	ErrInvalidCiphertext = errors.New("ciphertext payload is invalid or truncated")

	// ErrAuthFailed is returned when authentication/decryption fails (wrong key or corrupted data).
	ErrAuthFailed = errors.New("decryption failed: invalid key or tampered ciphertext")
)
View Source
var (
	// ErrNotEncrypted is returned when attempting to decrypt text missing the FLOWENC prefix.
	ErrNotEncrypted = errors.New("data is not in recognized encrypted format (missing FLOWENC header)")

	// ErrNotSigned is returned when attempting to parse a signed envelope missing the FLOWSIGNED prefix.
	ErrNotSigned = errors.New("data is not in recognized signed format (missing FLOWSIGNED header)")
)

Functions

func DecryptArmored

func DecryptArmored(armoredText string, key string) ([]byte, error)

DecryptArmored decrypts a text-safe armored string formatted with FLOWENC:v1:.

func DecryptAuto

func DecryptAuto(data []byte, key string, requireEncrypted bool) ([]byte, error)

DecryptAuto attempts to decrypt data if it contains the armored prefix, or if it matches raw binary payload format when requireEncrypted is true. If it is already unencrypted plaintext and requireEncrypted is false, it returns the input data unchanged.

func DecryptBytes

func DecryptBytes(packed []byte, key string) ([]byte, error)

DecryptBytes decrypts a packed binary ciphertext produced by EncryptBytes.

func DeriveKey

func DeriveKey(passphrase string, salt []byte) []byte

DeriveKey derives a 32-byte (256-bit) AES key from a passphrase and salt using PBKDF2-HMAC-SHA256.

func EncryptArmored

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

EncryptArmored encrypts plaintext bytes and formats them as a text-safe armored string. Format: FLOWENC:v1:<base64(salt || nonce || ciphertext + tag)>

func EncryptBytes

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

EncryptBytes encrypts plaintext using AES-256-GCM with a PBKDF2-derived key. The returned slice contains: salt (16 bytes) || nonce (12 bytes) || ciphertext + tag (16 bytes).

func GenerateECDSAKeyPair

func GenerateECDSAKeyPair(curve string) (privPEM []byte, pubPEM []byte, err error)

GenerateECDSAKeyPair creates a new ECDSA private/public key pair in PEM format.

func GenerateEd25519KeyPair

func GenerateEd25519KeyPair() (privPEM []byte, pubPEM []byte, err error)

GenerateEd25519KeyPair creates a new Ed25519 private/public key pair in PEM format.

func GenerateOpenPGPKeyPair

func GenerateOpenPGPKeyPair(name, comment, email string) (privArmored, pubArmored []byte, err error)

GenerateOpenPGPKeyPair creates a new OpenPGP RSA keypair and returns ASCII-armored private and public keys.

func GenerateRSAKeyPair

func GenerateRSAKeyPair(bits int) (privPEM []byte, pubPEM []byte, err error)

GenerateRSAKeyPair creates a new RSA private/public key pair in PEM format.

func GenerateSecureKey

func GenerateSecureKey(byteLength int) (string, error)

GenerateSecureKey creates a cryptographically secure random key formatted as a base64 string.

func GenerateSelfSignedCertificate

func GenerateSelfSignedCertificate(privKey crypto.PrivateKey, pubKey crypto.PublicKey, commonName string, validityDays int) (certPEM []byte, err error)

GenerateSelfSignedCertificate creates a self-signed X.509 certificate for a given key.

func IsEncryptedPayload

func IsEncryptedPayload(data []byte) bool

IsEncryptedPayload checks if the data begins with the FLOWENC envelope prefix.

func IsSignaturePayload

func IsSignaturePayload(data []byte) bool

IsSignaturePayload checks if the data begins with the FLOWSIG envelope prefix.

func IsSignedPayload

func IsSignedPayload(data []byte) bool

IsSignedPayload checks if the data begins with the FLOWSIGNED envelope prefix.

func ParseCertificatePEM

func ParseCertificatePEM(certPEM []byte) (*x509.Certificate, error)

ParseCertificatePEM parses a PEM-encoded X.509 certificate.

func ParsePrivateKeyPEM

func ParsePrivateKeyPEM(keyPEM []byte) (crypto.PrivateKey, error)

ParsePrivateKeyPEM parses a PEM-encoded private key (PKCS#1, PKCS#8, or EC).

func ParsePublicKeyOrCertPEM

func ParsePublicKeyOrCertPEM(pemBytes []byte) (crypto.PublicKey, *x509.Certificate, error)

ParsePublicKeyOrCertPEM parses a public key from PEM containing either a public key or an X.509 certificate.

func ParseSignedPayload

func ParseSignedPayload(data []byte) (format string, sig []byte, content []byte, err error)

ParseSignedPayload parses a FLOWSIGNED:v1:<format>:<b64(sig)>:<b64(content)> envelope.

func SignData

func SignData(data []byte, sigType string, privKeyBytes []byte, certBytes []byte, passphrase string) (sigBytes []byte, format string, err error)

SignData is a high-level signer supporting 'openssl', 'pgp', and 'pkcs7'.

func SignOpenPGP

func SignOpenPGP(data []byte, privKeyArmored []byte, passphrase string) (sigArmored []byte, err error)

SignOpenPGP signs arbitrary data using an armored OpenPGP private key.

func SignOpenSSL

func SignOpenSSL(data []byte, privKeyPEM []byte) (sigBytes []byte, keyType string, err error)

SignOpenSSL signs arbitrary data using an RSA, ECDSA, or Ed25519 PEM private key.

func SignPKCS7

func SignPKCS7(data []byte, cert *x509.Certificate, privKey crypto.PrivateKey) ([]byte, error)

SignPKCS7 creates a cross-platform PKCS#7 / CMS (RFC 2315 / RFC 5652) detached SignedData structure.

func SignPKCS7Detached

func SignPKCS7Detached(data []byte, certPEM []byte, privKeyPEM []byte) (p7DER []byte, err error)

SignPKCS7Detached generates a Windows CMS / PKCS#7 detached signature.

func VerifyPKCS7

func VerifyPKCS7(data []byte, p7Bytes []byte, rootCert *x509.Certificate) (*x509.Certificate, error)

VerifyPKCS7 parses and verifies a detached PKCS#7 / CMS SignedData structure against the provided data in pure Go. Returns the verified signer certificate or an error.

func VerifyPKCS7WithCA

func VerifyPKCS7WithCA(data []byte, p7Bytes []byte, signerCert *x509.Certificate, rootCert *x509.Certificate) (*x509.Certificate, error)

VerifyPKCS7WithCA parses and verifies a detached PKCS#7 / CMS SignedData structure against the provided data, optional signer certificate, and optional root CA certificate.

func WrapSignatureOnly

func WrapSignatureOnly(format string, sig []byte) string

WrapSignatureOnly wraps a raw or armored signature into the FLOWSIG:v1:<format>:<base64> envelope.

func WrapSignedPayload

func WrapSignedPayload(format string, sig []byte, content []byte) string

WrapSignedPayload wraps a signature and content into a single FLOWSIGNED:v1:<format>:<b64(sig)>:<b64(content)> envelope.

func ZeroBytes

func ZeroBytes(b []byte)

ZeroBytes overwrites a sensitive byte slice with zeroes to minimize exposure in memory.

Types

type VerificationResult

type VerificationResult struct {
	Valid       bool              `json:"valid"`
	Format      string            `json:"format"`
	Algorithm   string            `json:"algorithm"`
	SignerInfo  string            `json:"signer_info"`
	Certificate *x509.Certificate `json:"-"`
}

VerificationResult contains metadata about a successful signature verification.

func VerifyAuto

func VerifyAuto(data []byte, sigBytes []byte, keyOrCertBytes []byte) (*VerificationResult, error)

VerifyAuto verifies data against a signature using whatever key, certificate, or keyring is provided. Automatically recognizes FLOWSIG:v1: envelopes, PGP armored blocks, PKCS#7 DER, and standard OpenSSL signatures.

func VerifyAutoWithCA

func VerifyAutoWithCA(data []byte, sigBytes []byte, keyOrCertBytes []byte, caCertBytes []byte) (*VerificationResult, error)

VerifyAutoWithCA verifies data against a signature with an optional CA certificate for chain validation.

func VerifyOpenPGP

func VerifyOpenPGP(data []byte, sigArmored []byte, keyRingArmored []byte) (*VerificationResult, error)

VerifyOpenPGP verifies data against an armored detached OpenPGP signature using an armored public keyring.

func VerifyOpenSSL

func VerifyOpenSSL(data []byte, sig []byte, pubKeyOrCertPEM []byte) (*VerificationResult, error)

VerifyOpenSSL verifies data against a signature using an RSA, ECDSA, or Ed25519 public key or certificate.

func VerifyOpenSSLWithCA

func VerifyOpenSSLWithCA(data []byte, sig []byte, pubKeyOrCertPEM []byte, caCertPEM []byte) (*VerificationResult, error)

VerifyOpenSSLWithCA verifies data against a signature and optionally validates the signer's certificate chain.

func VerifyPKCS7Detached

func VerifyPKCS7Detached(data []byte, p7Bytes []byte, caCertPEM []byte) (*VerificationResult, error)

VerifyPKCS7Detached verifies data against a detached PKCS#7 / CMS signature (DER, PEM, or FLOWSIG).

func VerifyPKCS7DetachedWithCA

func VerifyPKCS7DetachedWithCA(data []byte, p7Bytes []byte, signerCertPEM []byte, caCertPEM []byte) (*VerificationResult, error)

VerifyPKCS7DetachedWithCA verifies data against a detached PKCS#7 / CMS signature (DER, PEM, or FLOWSIG) with optional signer certificate PEM and optional CA certificate PEM.

func VerifyPayloadAuto

func VerifyPayloadAuto(payload []byte, keyOrCertBytes []byte) ([]byte, *VerificationResult, error)

VerifyPayloadAuto unwraps and verifies a FLOWSIGNED:v1 envelope using the provided key, certificate, or keyring. Returns the verified inner content and verification metadata.

func VerifyPayloadAutoWithCA

func VerifyPayloadAutoWithCA(payload []byte, keyOrCertBytes []byte, caCertBytes []byte) ([]byte, *VerificationResult, error)

VerifyPayloadAutoWithCA unwraps and verifies a FLOWSIGNED:v1 envelope using key/cert and optional CA root.

Jump to

Keyboard shortcuts

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