mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
1f0cda0324
Former-commit-id: 4cd1a24aec45fbfeff04fab18abde8e2f9f01b16 [formerly f10b9db62ba2289af4b48b2086258b7995f16caf] [formerly d7020bfd45870f48485982fc05e671df8ffc9307 [formerly 33df65ef54db9744efc17ee10c197362835795e0 [formerlye97bfb05a9
]]] Former-commit-id: f6cdb6634f8329f8d87634a86d2ff8af1f5d4c3c [formerly 6ddc94157b82f131bcfd890ab27e8021931b65cd] Former-commit-id: 2287c335a383ed5c85b97b76459fd49e62a65f8c Former-commit-id:b99cb7ce70
74 lines
1.6 KiB
Go
Executable File
74 lines
1.6 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io/ioutil"
|
|
"log"
|
|
|
|
"golang.org/x/crypto/openpgp"
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
)
|
|
|
|
var encryptionType string
|
|
|
|
func init() {
|
|
encryptionType = "PGP SIGNATURE"
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func decryptString(decryptionString string, encryptionPassphraseString string) (string, error) {
|
|
encryptionPassphrase := []byte(encryptionPassphraseString)
|
|
decbuf := bytes.NewBuffer([]byte(decryptionString))
|
|
result, err := armor.Decode(decbuf)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
alreadyPrompted := false
|
|
md, err := openpgp.ReadMessage(result.Body, nil, func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
|
|
if alreadyPrompted {
|
|
return nil, errors.New("Could not decrypt using passphrase")
|
|
} else {
|
|
alreadyPrompted = true
|
|
}
|
|
return encryptionPassphrase, nil
|
|
}, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
bytes, err := ioutil.ReadAll(md.UnverifiedBody)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// func main() {
|
|
// test := encryptString("This is some string", "golang")
|
|
// fmt.Println(test)
|
|
// testD := decryptString(test, "golang")
|
|
// fmt.Println(testD)
|
|
//
|
|
// }
|