示例#1
0
文件: openpgp.go 项目: bndw/pick
// 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
}
示例#2
0
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()
}
示例#3
0
// 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()
}
示例#4
0
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()
}