// encrypt uses PGP to symmetrically encrypt and armor text with the // provided password. func (c *OpenPGPClient) Encrypt(plaintext []byte, password []byte) (ciphertext []byte, err error) { encbuf := bytes.NewBuffer(nil) w, err := armor.Encode(encbuf, "PGP MESSAGE", nil) if err != nil { return } defer w.Close() pt, err := openpgp.SymmetricallyEncrypt(w, password, nil, c.packetConfig) if err != nil { return } defer pt.Close() if _, err := pt.Write(plaintext); err != nil { return nil, err } // Force-close writer to flush their cache if err = pt.Close(); err != nil { return } if err = w.Close(); err != nil { return } ciphertext = encbuf.Bytes() return }
func GpgEncrypt(text2encrypt string, headers map[string]string) string { var encrypted bytes.Buffer foo := bufio.NewWriter(&encrypted) w, _ := armor.Encode(foo, "TRIBES PAYLOAD", headers) plaintext, _ := openpgp.SymmetricallyEncrypt(w, []byte(config.GetTribeID()), nil, nil) fmt.Fprintf(plaintext, text2encrypt) plaintext.Close() w.Close() foo.Flush() return encrypted.String() }
// EncryptData encrypts data using symmetric OpenPGP encryption. func EncryptData(input []byte, password string) []byte { var output bytes.Buffer cryptoWriter, err := openpgp.SymmetricallyEncrypt(&output, []byte(password), nil, encryptionConfig) PanicIfErr(err) _, err = cryptoWriter.Write(input) PanicIfErr(err) err = cryptoWriter.Close() PanicIfErr(err) return output.Bytes() }
func encryptString(encryptionText string, encryptionPassphraseString string) string { encryptionPassphrase := []byte(encryptionPassphraseString) encbuf := bytes.NewBuffer(nil) w, err := armor.Encode(encbuf, encryptionType, nil) if err != nil { log.Fatal(err) } plaintext, err := openpgp.SymmetricallyEncrypt(w, encryptionPassphrase, nil, nil) if err != nil { log.Fatal(err) } message := []byte(encryptionText) _, err = plaintext.Write(message) plaintext.Close() w.Close() return encbuf.String() }