Documentation ¶
Overview ¶
Package crypto provides a high-level API for common OpenPGP functionality.
Index ¶
- func EncryptCore(w io.Writer, encryptEntities []*openpgp.Entity, signEntity *openpgp.Entity, ...) (io.WriteCloser, error)
- func SeparateKeyAndData(kr *KeyRing, r io.Reader, estimatedLength, garbageCollector int) (outSplit *models.EncryptedSplit, err error)
- func SplitArmor(encrypted string) (*models.EncryptedSplit, error)
- type AttachmentProcessor
- type GopenPGP
- func (pgp *GopenPGP) BuildKeyRing(binKeys []byte) (kr *KeyRing, err error)
- func (pgp *GopenPGP) BuildKeyRingArmored(key string) (kr *KeyRing, err error)
- func (pgp *GopenPGP) BuildKeyRingNoError(binKeys []byte) (kr *KeyRing)
- func (pgp *GopenPGP) CheckKey(pubKey string) (string, error)
- func (pgp *GopenPGP) DecryptAttachment(keyPacket, dataPacket []byte, kr *KeyRing, passphrase string) ([]byte, error)
- func (pgp *GopenPGP) DecryptMIMEMessage(encryptedText string, verifierKey, privateKeyRing *KeyRing, passphrase string, ...)
- func (pgp *GopenPGP) DecryptMessage(encryptedText string, privateKey *KeyRing, passphrase string) (string, error)
- func (pgp *GopenPGP) DecryptMessageStringKey(encryptedText, privateKey, passphrase string) (string, error)
- func (pgp *GopenPGP) DecryptMessageVerify(encryptedText string, verifierKey, privateKeyRing *KeyRing, passphrase string, ...) (*models.DecryptSignedVerify, error)
- func (pgp *GopenPGP) DecryptMessageWithPassword(encrypted string, password string) (string, error)
- func (pgp *GopenPGP) EncryptAttachment(plainData []byte, fileName string, publicKey *KeyRing) (*models.EncryptedSplit, error)
- func (pgp *GopenPGP) EncryptAttachmentLowMemory(estimatedSize int, fileName string, publicKey *KeyRing) (*AttachmentProcessor, error)
- func (pgp *GopenPGP) EncryptMessage(plainText string, publicKey, privateKey *KeyRing, passphrase string, trim bool) (string, error)
- func (pgp *GopenPGP) EncryptMessageWithPassword(plainText string, password string) (string, error)
- func (pgp *GopenPGP) GenerateKey(userName, domain, passphrase, keyType string, bits int) (string, error)
- func (pgp *GopenPGP) GenerateRSAKeyWithPrimes(userName, domain, passphrase string, bits int, ...) (string, error)
- func (pgp *GopenPGP) GetSessionFromKeyPacket(keyPacket []byte, privateKey *KeyRing, passphrase string) (*SymmetricKey, error)
- func (pgp *GopenPGP) GetSessionFromSymmetricPacket(keyPacket []byte, password string) (*SymmetricKey, error)
- func (pgp *GopenPGP) GetTime() time.Time
- func (pgp *GopenPGP) GetTimeUnix() int64
- func (pgp *GopenPGP) IsKeyExpired(publicKey string) (bool, error)
- func (pgp *GopenPGP) IsKeyExpiredBin(publicKey []byte) (bool, error)
- func (pgp *GopenPGP) KeyPacketWithPublicKey(sessionSplit *SymmetricKey, publicKey string) ([]byte, error)
- func (pgp *GopenPGP) KeyPacketWithPublicKeyBin(sessionSplit *SymmetricKey, publicKey []byte) ([]byte, error)
- func (pgp *GopenPGP) RandomToken() ([]byte, error)
- func (pgp *GopenPGP) RandomTokenWith(size int) ([]byte, error)
- func (pgp *GopenPGP) SymmetricKeyPacketWithPassword(sessionSplit *SymmetricKey, password string) ([]byte, error)
- func (pgp *GopenPGP) UpdatePrivateKeyPassphrase(privateKey string, oldPassphrase string, newPassphrase string) (string, error)
- func (pgp *GopenPGP) UpdateTime(newTime int64)
- type Identity
- type KeyRing
- func (kr *KeyRing) CheckPassphrase(passphrase string) bool
- func (kr *KeyRing) Decrypt(r io.Reader) (decrypted io.Reader, signed *Signature, err error)
- func (kr *KeyRing) DecryptArmored(r io.Reader) (decrypted io.Reader, signed *Signature, err error)
- func (kr *KeyRing) DecryptMessage(encrypted string) (SignedString, error)
- func (kr *KeyRing) DecryptMessageIfNeeded(data string) (decrypted string, err error)
- func (kr *KeyRing) Encrypt(w io.Writer, sign *KeyRing, filename string, canonicalizeText bool) (io.WriteCloser, error)
- func (kr *KeyRing) EncryptArmored(w io.Writer, sign *KeyRing) (wc io.WriteCloser, err error)
- func (kr *KeyRing) EncryptKey(symKey *SymmetricKey) (packets string, err error)
- func (kr *KeyRing) EncryptMessage(s string, sign *KeyRing) (encrypted string, err error)
- func (kr *KeyRing) EncryptSymmetric(textToEncrypt string, canonicalizeText bool) (outSplit *models.EncryptedSplit, err error)
- func (kr *KeyRing) GetArmoredPublicKey() (s string, err error)
- func (kr *KeyRing) GetEntities() openpgp.EntityList
- func (kr *KeyRing) GetFingerprint() (string, error)
- func (kr *KeyRing) GetPublicKey() (b []byte, err error)
- func (kr *KeyRing) GetSigningEntity(passphrase string) (*openpgp.Entity, error)
- func (kr *KeyRing) Identities() []*Identity
- func (kr *KeyRing) KeyIds() []uint64
- func (kr *KeyRing) SignBinDetached(plainData []byte, passphrase string) (string, error)
- func (kr *KeyRing) SignTextDetached(plainText string, passphrase string, trimNewlines bool) (string, error)
- func (kr *KeyRing) Unlock(passphrase []byte) error
- func (kr *KeyRing) UnmarshalJSON(b []byte) (err error)
- func (kr *KeyRing) VerifyBinDetachedSig(signature string, plainData []byte, verifyTime int64) (bool, error)
- func (kr *KeyRing) VerifyTextDetachedSig(signature string, plainText string, verifyTime int64, trimNewlines bool) (bool, error)
- func (kr *KeyRing) WriteArmoredPublicKey(w io.Writer) (err error)
- func (kr *KeyRing) WritePublicKey(w io.Writer) (err error)
- type MIMECallbacks
- type Signature
- type SignatureCollector
- type SignedString
- type SymmetricKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncryptCore ¶
func EncryptCore(w io.Writer, encryptEntities []*openpgp.Entity, signEntity *openpgp.Entity, filename string, canonicalizeText bool, timeGenerator func() time.Time) (io.WriteCloser, error)
EncryptCore is lower-level encryption method used by KeyRing.Encrypt.
func SeparateKeyAndData ¶
func SeparateKeyAndData( kr *KeyRing, r io.Reader, estimatedLength, garbageCollector int, ) (outSplit *models.EncryptedSplit, err error)
SeparateKeyAndData reads a binary PGP message from r and splits it into its session key packet and symmetrically encrypted data packet.
func SplitArmor ¶
func SplitArmor(encrypted string) (*models.EncryptedSplit, error)
SplitArmor is a helper method which splits an armored message into its session key packet and symmetrically encrypted data packet.
Types ¶
type AttachmentProcessor ¶
type AttachmentProcessor struct {
// contains filtered or unexported fields
}
AttachmentProcessor keeps track of the progress of encrypting an attachment (optimized for encrypting large files).
func (*AttachmentProcessor) Finish ¶
func (ap *AttachmentProcessor) Finish() (*models.EncryptedSplit, error)
Finish closes the attachment and returns the encrypted data
func (*AttachmentProcessor) Process ¶
func (ap *AttachmentProcessor) Process(plainData []byte)
Process writes attachment data to be encrypted
type GopenPGP ¶
type GopenPGP struct {
// contains filtered or unexported fields
}
GopenPGP is used as a "namespace" for many of the functions in this package. It is a struct that keeps track of time skew between server and client.
func (*GopenPGP) BuildKeyRing ¶
BuildKeyRing reads keyring from binary data
func (*GopenPGP) BuildKeyRingArmored ¶
BuildKeyRingArmored reads armored string and returns keyring
func (*GopenPGP) BuildKeyRingNoError ¶
BuildKeyRingNoError does not return error on fail
func (*GopenPGP) CheckKey ¶
CheckKey is a debug helper function that prints the key and subkey fingerprints.
func (*GopenPGP) DecryptAttachment ¶
func (pgp *GopenPGP) DecryptAttachment( keyPacket, dataPacket []byte, kr *KeyRing, passphrase string, ) ([]byte, error)
DecryptAttachment takes a session key packet and symmetrically encrypted data packet. privateKeys is a KeyRing that can contain multiple keys. The passphrase is used to unlock keys in privateKeys.
func (*GopenPGP) DecryptMIMEMessage ¶
func (pgp *GopenPGP) DecryptMIMEMessage( encryptedText string, verifierKey, privateKeyRing *KeyRing, passphrase string, callbacks MIMECallbacks, verifyTime int64, )
DecryptMIMEMessage decrypts a MIME message.
func (*GopenPGP) DecryptMessage ¶
func (pgp *GopenPGP) DecryptMessage(encryptedText string, privateKey *KeyRing, passphrase string) (string, error)
DecryptMessage decrypts encrypted string using keyring encryptedText : string armored encrypted privateKey : keyring with private key to decrypt message, could be multiple keys passphrase : match with private key to decrypt message
func (*GopenPGP) DecryptMessageStringKey ¶
func (pgp *GopenPGP) DecryptMessageStringKey( encryptedText, privateKey, passphrase string, ) (string, error)
DecryptMessageStringKey decrypts encrypted message use private key (string) encryptedText : string armored encrypted privateKey : armored private use to decrypt message passphrase : match with private key to decrypt message
func (*GopenPGP) DecryptMessageVerify ¶
func (pgp *GopenPGP) DecryptMessageVerify( encryptedText string, verifierKey, privateKeyRing *KeyRing, passphrase string, verifyTime int64, ) (*models.DecryptSignedVerify, error)
DecryptMessageVerify decrypts message and verify the signature encryptedText: string armored encrypted verifierKey []byte: unarmored verifier keys privateKeyRing []byte: unarmored private key to decrypt. could be multiple passphrase: match with private key to decrypt message
func (*GopenPGP) DecryptMessageWithPassword ¶
DecryptMessageWithPassword decrypts a pgp message with a password encrypted string : armored pgp message output string : clear text
func (*GopenPGP) EncryptAttachment ¶
func (pgp *GopenPGP) EncryptAttachment( plainData []byte, fileName string, publicKey *KeyRing, ) (*models.EncryptedSplit, error)
EncryptAttachment encrypts a file. fileName
func (*GopenPGP) EncryptAttachmentLowMemory ¶
func (pgp *GopenPGP) EncryptAttachmentLowMemory( estimatedSize int, fileName string, publicKey *KeyRing, ) (*AttachmentProcessor, error)
EncryptAttachmentLowMemory creates an AttachmentProcessor which can be used to encrypt a file. It takes an estimatedSize and fileName as hints about the file. It is optimized for low-memory environments and collects garbage every megabyte.
func (*GopenPGP) EncryptMessage ¶
func (pgp *GopenPGP) EncryptMessage( plainText string, publicKey, privateKey *KeyRing, passphrase string, trim bool, ) (string, error)
EncryptMessage encrypts message with unarmored public key, if pass private key and passphrase will also sign the message publicKey : bytes unarmored public key plainText : the input privateKey : optional required when you want to sign passphrase : optional required when you pass the private key and this passphrase should decrypt the private key trim : bool true if need to trim new lines
func (*GopenPGP) EncryptMessageWithPassword ¶
EncryptMessageWithPassword encrypts a plain text to pgp message with a password plainText string: clear text output string: armored pgp message
func (*GopenPGP) GenerateKey ¶
func (pgp *GopenPGP) GenerateKey(userName, domain, passphrase, keyType string, bits int) (string, error)
GenerateKey generates a key of the given keyType ("rsa" or "x25519"). If keyType is "rsa", bits is the RSA bitsize of the key. If keyType is "x25519", bits is unused.
func (*GopenPGP) GenerateRSAKeyWithPrimes ¶
func (pgp *GopenPGP) GenerateRSAKeyWithPrimes( userName, domain, passphrase string, bits int, primeone, primetwo, primethree, primefour []byte, ) (string, error)
GenerateRSAKeyWithPrimes generates a RSA key using the given primes.
func (*GopenPGP) GetSessionFromKeyPacket ¶
func (pgp *GopenPGP) GetSessionFromKeyPacket( keyPacket []byte, privateKey *KeyRing, passphrase string, ) (*SymmetricKey, error)
GetSessionFromKeyPacket returns the decrypted session key from a binary public-key encrypted session key packet.
func (*GopenPGP) GetSessionFromSymmetricPacket ¶
func (pgp *GopenPGP) GetSessionFromSymmetricPacket(keyPacket []byte, password string) (*SymmetricKey, error)
GetSessionFromSymmetricPacket decrypts the binary symmetrically encrypted session key packet and returns the session key.
func (*GopenPGP) GetTimeUnix ¶
GetTimeUnix gets latest cached time
func (*GopenPGP) IsKeyExpired ¶
IsKeyExpired checks whether the given armored key is expired.
func (*GopenPGP) IsKeyExpiredBin ¶
IsKeyExpiredBin checks whether the given (unarmored, binary) key is expired.
func (*GopenPGP) KeyPacketWithPublicKey ¶
func (pgp *GopenPGP) KeyPacketWithPublicKey(sessionSplit *SymmetricKey, publicKey string) ([]byte, error)
KeyPacketWithPublicKey encrypts the session key with the armored publicKey and returns a binary public-key encrypted session key packet.
func (*GopenPGP) KeyPacketWithPublicKeyBin ¶
func (pgp *GopenPGP) KeyPacketWithPublicKeyBin(sessionSplit *SymmetricKey, publicKey []byte) ([]byte, error)
KeyPacketWithPublicKeyBin encrypts the session key with the unarmored publicKey and returns a binary public-key encrypted session key packet.
func (*GopenPGP) RandomToken ¶
RandomToken generates a random token with the key size of the default cipher.
func (*GopenPGP) RandomTokenWith ¶
RandomTokenWith generates a random token with the given key size.
func (*GopenPGP) SymmetricKeyPacketWithPassword ¶
func (pgp *GopenPGP) SymmetricKeyPacketWithPassword(sessionSplit *SymmetricKey, password string) ([]byte, error)
SymmetricKeyPacketWithPassword encrypts the session key with the password and returns a binary symmetrically encrypted session key packet.
func (*GopenPGP) UpdatePrivateKeyPassphrase ¶
func (pgp *GopenPGP) UpdatePrivateKeyPassphrase( privateKey string, oldPassphrase string, newPassphrase string, ) (string, error)
UpdatePrivateKeyPassphrase decrypts the given armored privateKey with oldPassphrase, re-encrypts it with newPassphrase, and returns the new armored key.
func (*GopenPGP) UpdateTime ¶
UpdateTime updates cached time
type KeyRing ¶
type KeyRing struct { // FirstKeyID as obtained from API to match salt FirstKeyID string // contains filtered or unexported fields }
KeyRing contains multiple private and public keys.
func FilterExpiredKeys ¶
FilterExpiredKeys takes a given KeyRing list and it returns only those KeyRings which contain at least, one unexpired Key. It returns only unexpired parts of these KeyRings.
func ReadArmoredKeyRing ¶
ReadArmoredKeyRing reads an armored data into keyring.
func ReadKeyRing ¶
ReadKeyRing reads an binary data into keyring.
func (*KeyRing) CheckPassphrase ¶
CheckPassphrase checks if private key passphrase is correct for every sub key.
func (*KeyRing) Decrypt ¶
Decrypt decrypts a message sent to the keypair's owner. If the message is not signed, signed will be nil. If error is errors.ErrSignatureExpired (from golang.org/x/crypto/openpgp/errors), contents are still provided if library clients wish to process this message further.
func (*KeyRing) DecryptArmored ¶
DecryptArmored decrypts an armored message sent to the keypair's owner. If error is errors.ErrSignatureExpired (from golang.org/x/crypto/openpgp/errors), contents are still provided if library clients wish to process this message further.
func (*KeyRing) DecryptMessage ¶
func (kr *KeyRing) DecryptMessage(encrypted string) (SignedString, error)
DecryptMessage decrypts an armored string sent to the keypair's owner. If error is errors.ErrSignatureExpired (from golang.org/x/crypto/openpgp/errors), contents are still provided if library clients wish to process this message further.
func (*KeyRing) DecryptMessageIfNeeded ¶
DecryptMessageIfNeeded data if has armored PGP message format, if not return original data. If error is errors.ErrSignatureExpired (from golang.org/x/crypto/openpgp/errors), contents are still provided if library clients wish to process this message further.
func (*KeyRing) Encrypt ¶
func (kr *KeyRing) Encrypt(w io.Writer, sign *KeyRing, filename string, canonicalizeText bool) (io.WriteCloser, error)
Encrypt encrypts data to this keyring's owner. If sign is not nil, it also signs data with it. The keyring sign must be unlocked to be able to sign data, if not an error will be returned.
func (*KeyRing) EncryptArmored ¶
EncryptArmored encrypts and armors data to the keyring's owner. Wrapper of Encrypt.
func (*KeyRing) EncryptKey ¶
func (kr *KeyRing) EncryptKey(symKey *SymmetricKey) (packets string, err error)
EncryptKey encrypts the provided key.
func (*KeyRing) EncryptMessage ¶
EncryptMessage encrypts and armors a string to the keyring's owner. Wrapper of Encrypt.
func (*KeyRing) EncryptSymmetric ¶
func (kr *KeyRing) EncryptSymmetric(textToEncrypt string, canonicalizeText bool) (outSplit *models.EncryptedSplit, err error)
EncryptSymmetric data using generated symmetric key encrypted with this KeyRing. Wrapper of Encrypt.
func (*KeyRing) GetArmoredPublicKey ¶
GetArmoredPublicKey returns the armored public keys from this keyring.
func (*KeyRing) GetEntities ¶
func (kr *KeyRing) GetEntities() openpgp.EntityList
GetEntities returns openpgp entities contained in this KeyRing.
func (*KeyRing) GetFingerprint ¶
GetFingerprint gets the fingerprint from the keyring.
func (*KeyRing) GetPublicKey ¶
GetPublicKey returns the unarmored public keys from this keyring.
func (*KeyRing) GetSigningEntity ¶
GetSigningEntity returns first private unlocked signing entity from keyring.
func (*KeyRing) Identities ¶
Identities returns the list of identities associated with this key ring.
func (*KeyRing) SignBinDetached ¶
SignBinDetached creates an armored detached signature of binary data.
func (*KeyRing) SignTextDetached ¶
func (kr *KeyRing) SignTextDetached(plainText string, passphrase string, trimNewlines bool) (string, error)
SignTextDetached creates an armored detached signature of a given string.
func (*KeyRing) Unlock ¶
Unlock tries to unlock as many keys as possible with the following password. Note that keyrings can contain keys locked with different passwords, and thus err == nil does not mean that all keys have been successfully decrypted. If err != nil, the password is wrong for every key, and err is the last error encountered.
func (*KeyRing) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler.
func (*KeyRing) VerifyBinDetachedSig ¶
func (kr *KeyRing) VerifyBinDetachedSig(signature string, plainData []byte, verifyTime int64) (bool, error)
VerifyBinDetachedSig verifies an armored detached signature given the plaintext as binary data.
func (*KeyRing) VerifyTextDetachedSig ¶
func (kr *KeyRing) VerifyTextDetachedSig( signature string, plainText string, verifyTime int64, trimNewlines bool, ) (bool, error)
VerifyTextDetachedSig verifies an armored detached signature given the plaintext as a string.
func (*KeyRing) WriteArmoredPublicKey ¶
WriteArmoredPublicKey outputs armored public keys from the keyring to w.
type MIMECallbacks ¶
type MIMECallbacks interface { OnBody(body string, mimetype string) OnAttachment(headers string, data []byte) // Encrypted headers can be in an attachment and thus be placed at the end of the mime structure. OnEncryptedHeaders(headers string) OnVerified(verified int) OnError(err error) }
MIMECallbacks defines callback methods to process a MIME message.
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
Signature is be used to check a signature. Because the signature is checked when the reader is consumed, Signature must only be used after EOF has been seen. A signature is only valid if s.Err() returns nil, otherwise the sender's identity cannot be trusted.
type SignatureCollector ¶
type SignatureCollector struct {
// contains filtered or unexported fields
}
SignatureCollector structure
func (*SignatureCollector) Accept ¶
func (sc *SignatureCollector) Accept( part io.Reader, header textproto.MIMEHeader, hasPlainSibling, isFirst, isLast bool, ) (err error)
Accept collects the signature
func (SignatureCollector) GetSignature ¶
func (sc SignatureCollector) GetSignature() string
GetSignature collected by Accept
type SignedString ¶
SignedString wraps string with a Signature
type SymmetricKey ¶
type SymmetricKey struct { // The decrypted binary session key. Key []byte // The symmetric encryption algorithm used with this key. Algo string }
SymmetricKey stores a decrypted session key.
func DecryptAttKey ¶
func DecryptAttKey(kr *KeyRing, keyPacket string) (key *SymmetricKey, err error)
DecryptAttKey decrypts a public-key encrypted session key and returns the decrypted symmetric session key.
func (*SymmetricKey) GetBase64Key ¶
func (sk *SymmetricKey) GetBase64Key() string
GetBase64Key returns the session key as base64 encoded string.
func (*SymmetricKey) GetCipherFunc ¶
func (sk *SymmetricKey) GetCipherFunc() packet.CipherFunction
GetCipherFunc returns the cipher function corresponding to the algorithm used with this SymmetricKey.