Documentation
¶
Index ¶
- Constants
- Variables
- func DecryptArmored(armoredText string, key string) ([]byte, error)
- func DecryptAuto(data []byte, key string, requireEncrypted bool) ([]byte, error)
- func DecryptBytes(packed []byte, key string) ([]byte, error)
- func DeriveKey(passphrase string, salt []byte) []byte
- func EncryptArmored(plaintext []byte, key string) (string, error)
- func EncryptBytes(plaintext []byte, key string) ([]byte, error)
- func GenerateECDSAKeyPair(curve string) (privPEM []byte, pubPEM []byte, err error)
- func GenerateEd25519KeyPair() (privPEM []byte, pubPEM []byte, err error)
- func GenerateOpenPGPKeyPair(name, comment, email string) (privArmored, pubArmored []byte, err error)
- func GenerateRSAKeyPair(bits int) (privPEM []byte, pubPEM []byte, err error)
- func GenerateSecureKey(byteLength int) (string, error)
- func GenerateSelfSignedCertificate(privKey crypto.PrivateKey, pubKey crypto.PublicKey, commonName string, ...) (certPEM []byte, err error)
- func IsEncryptedPayload(data []byte) bool
- func IsSignaturePayload(data []byte) bool
- func IsSignedPayload(data []byte) bool
- func ParseCertificatePEM(certPEM []byte) (*x509.Certificate, error)
- func ParsePrivateKeyPEM(keyPEM []byte) (crypto.PrivateKey, error)
- func ParsePublicKeyOrCertPEM(pemBytes []byte) (crypto.PublicKey, *x509.Certificate, error)
- func ParseSignedPayload(data []byte) (format string, sig []byte, content []byte, err error)
- func SignData(data []byte, sigType string, privKeyBytes []byte, certBytes []byte, ...) (sigBytes []byte, format string, err error)
- func SignOpenPGP(data []byte, privKeyArmored []byte, passphrase string) (sigArmored []byte, err error)
- func SignOpenSSL(data []byte, privKeyPEM []byte) (sigBytes []byte, keyType string, err error)
- func SignPKCS7(data []byte, cert *x509.Certificate, privKey crypto.PrivateKey) ([]byte, error)
- func SignPKCS7Detached(data []byte, certPEM []byte, privKeyPEM []byte) (p7DER []byte, err error)
- func VerifyPKCS7(data []byte, p7Bytes []byte, rootCert *x509.Certificate) (*x509.Certificate, error)
- func VerifyPKCS7WithCA(data []byte, p7Bytes []byte, signerCert *x509.Certificate, ...) (*x509.Certificate, error)
- func WrapSignatureOnly(format string, sig []byte) string
- func WrapSignedPayload(format string, sig []byte, content []byte) string
- func ZeroBytes(b []byte)
- type VerificationResult
- func VerifyAuto(data []byte, sigBytes []byte, keyOrCertBytes []byte) (*VerificationResult, error)
- func VerifyAutoWithCA(data []byte, sigBytes []byte, keyOrCertBytes []byte, caCertBytes []byte) (*VerificationResult, error)
- func VerifyOpenPGP(data []byte, sigArmored []byte, keyRingArmored []byte) (*VerificationResult, error)
- func VerifyOpenSSL(data []byte, sig []byte, pubKeyOrCertPEM []byte) (*VerificationResult, error)
- func VerifyOpenSSLWithCA(data []byte, sig []byte, pubKeyOrCertPEM []byte, caCertPEM []byte) (*VerificationResult, error)
- func VerifyPKCS7Detached(data []byte, p7Bytes []byte, caCertPEM []byte) (*VerificationResult, error)
- func VerifyPKCS7DetachedWithCA(data []byte, p7Bytes []byte, signerCertPEM []byte, caCertPEM []byte) (*VerificationResult, error)
- func VerifyPayloadAuto(payload []byte, keyOrCertBytes []byte) ([]byte, *VerificationResult, error)
- func VerifyPayloadAutoWithCA(payload []byte, keyOrCertBytes []byte, caCertBytes []byte) ([]byte, *VerificationResult, error)
Constants ¶
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 )
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:" )
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 ¶
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") )
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 ¶
DecryptArmored decrypts a text-safe armored string formatted with FLOWENC:v1:.
func DecryptAuto ¶
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 ¶
DecryptBytes decrypts a packed binary ciphertext produced by EncryptBytes.
func DeriveKey ¶
DeriveKey derives a 32-byte (256-bit) AES key from a passphrase and salt using PBKDF2-HMAC-SHA256.
func EncryptArmored ¶
EncryptArmored encrypts plaintext bytes and formats them as a text-safe armored string. Format: FLOWENC:v1:<base64(salt || nonce || ciphertext + tag)>
func EncryptBytes ¶
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 ¶
GenerateECDSAKeyPair creates a new ECDSA private/public key pair in PEM format.
func GenerateEd25519KeyPair ¶
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 ¶
GenerateRSAKeyPair creates a new RSA private/public key pair in PEM format.
func GenerateSecureKey ¶
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 ¶
IsEncryptedPayload checks if the data begins with the FLOWENC envelope prefix.
func IsSignaturePayload ¶
IsSignaturePayload checks if the data begins with the FLOWSIG envelope prefix.
func IsSignedPayload ¶
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 ¶
ParsePublicKeyOrCertPEM parses a public key from PEM containing either a public key or an X.509 certificate.
func ParseSignedPayload ¶
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 ¶
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 ¶
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 ¶
WrapSignatureOnly wraps a raw or armored signature into the FLOWSIG:v1:<format>:<base64> envelope.
func WrapSignedPayload ¶
WrapSignedPayload wraps a signature and content into a single FLOWSIGNED:v1:<format>:<b64(sig)>:<b64(content)> envelope.
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.