package go_pgp import ( "crypto" "errors" "fmt" // required in order to use the crypto.SHA256- and -SHA512-Hashes _ "crypto/sha256" _ "crypto/sha512" "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" ) var ( defaultConfig = packet.Config{ RSABits: 4096, DefaultHash: crypto.SHA512, DefaultCompressionAlgo: packet.CompressionZIP, DefaultCipher: packet.CipherAES256, CompressionConfig: &packet.CompressionConfig{ Level: 5, }, } // ErrUndefinedPGPEntity defines the error for a null-reference to an openpgp.Entity ErrUndefinedPGPEntity = errors.New("pgp entity undefined") // ErrNoData defines the error for missing processable data ErrNoData = errors.New("no data to process") ) type Entity struct { openpgp.Entity cfg *packet.Config } // SetPassword encrypts all contained unencrypted private keys // using the given passphrase. func (e *Entity) SetPassword(passphrase []byte) error { return e.Entity.EncryptPrivateKeys(passphrase, e.cfg) } // CreatePGPEntity creates an OpenPGP Entity with only a name given func CreatePGPEntity(name string, options ...createOption) (*Entity, error) { return createPGPEntity(name, "", "", options...) } // CreatePGPEntityEmail creates an OpenPGP Entity with a name and email given func CreatePGPEntityEmail(name, email string, options ...createOption) (*Entity, error) { return createPGPEntity(name, "", email, options...) } // CreateCommentedPGPEntity creates an OpenPGP Entity with a name and comment given func CreateCommentedPGPEntity(name, comment string, options ...createOption) (entity *Entity, err error) { return createPGPEntity(name, comment, "", options...) } // CreateCommentedPGPEntity creates an OpenPGP Entity with a name and comment given func createPGPEntity(name, comment, email string, options ...createOption) (entity *Entity, err error) { if name == "" && email == "" { return nil, fmt.Errorf("name or email must be specified") } cfg := defaultConfig for _, option := range options { if option != nil { option(&cfg) } } var e *openpgp.Entity e, err = openpgp.NewEntity(name, comment, email, &cfg) if nil == err { for _, identity := range e.Identities { if nil != identity && nil != identity.SelfSignature { identity.SelfSignature.PreferredHash = []uint8{8, 9, 10} //cf. "golang.org/x/crypto/openpgp/s2k" -> s2k.HashIdToHash } } entity = &Entity{ Entity: *e, cfg: &cfg, } } return }