modify crypt, add test

This commit is contained in:
Jay 2018-03-12 17:51:59 +08:00
parent 043f20797e
commit 3873486b30
3 changed files with 301 additions and 67 deletions

94
main.go
View File

@ -1,15 +1,12 @@
package main
import (
"errors"
"flag"
"io"
"log"
"os"
"path"
"github.com/otakukaze/go-pgp-tool/pgpcrypt"
"golang.org/x/crypto/openpgp"
"github.com/otakukaze/go-pgp-tool/libs"
"github.com/otakukaze/go-pgp-tool/tools"
@ -66,11 +63,35 @@ func main() {
flags.DstFile = tools.ParsePath(flags.DstFile)
if flags.Encrypt {
// encryptAction()
encrypt()
// encrypt()
keyIO, err := os.Open(flags.KeyFile)
handleError(err)
srcIO, err := os.Open(flags.SrcFile)
handleError(err)
dstIO, err := os.Create(flags.DstFile)
handleError(err)
err = pgpcrypt.Encrypt2(keyIO, srcIO, dstIO)
// err := pgpcrypt.Encrypt(flags.KeyFile, flags.SrcFile, flags.DstFile)
handleError(err)
}
if flags.Decrypt {
decrypt()
// decrypt()
keyIO, err := os.Open(flags.KeyFile)
handleError(err)
srcIO, err := os.Open(flags.SrcFile)
handleError(err)
dstIO, err := os.Create(flags.DstFile)
handleError(err)
err = pgpcrypt.Decrypt2(keyIO, srcIO, dstIO, flags.Password)
// err := pgpcrypt.Decrypt(flags.KeyFile, flags.SrcFile, flags.DstFile, flags.Password)
handleError(err)
}
}
@ -83,64 +104,3 @@ func handleError(err error) {
func showUsage() {
flag.Usage()
}
func decrypt() {
keyFile, err := os.Open(flags.KeyFile)
handleError(err)
defer keyFile.Close()
keyList, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
if len(keyList) == 0 {
handleError(errors.New("key not found"))
}
key := keyList[0]
passphraseByte := []byte(flags.Password)
key.PrivateKey.Decrypt(passphraseByte)
for _, sub := range key.Subkeys {
sub.PrivateKey.Decrypt(passphraseByte)
}
dstFile, err := os.Create(flags.DstFile)
handleError(err)
defer dstFile.Close()
srcFile, err := os.Open(flags.SrcFile)
handleError(err)
defer srcFile.Close()
md, err := openpgp.ReadMessage(srcFile, keyList, nil, nil)
_, err = io.Copy(dstFile, md.UnverifiedBody)
handleError(err)
}
func encrypt() {
keyFile, err := os.Open(flags.KeyFile)
handleError(err)
defer keyFile.Close()
keyList, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
distFile, err := os.Create(flags.DstFile)
handleError(err)
defer distFile.Close()
srcFile, err := os.Open(flags.SrcFile)
handleError(err)
defer srcFile.Close()
fhint := &openpgp.FileHints{}
fhint.IsBinary = true
tmpWriter, err := openpgp.Encrypt(distFile, keyList, nil, fhint, nil)
handleError(err)
defer tmpWriter.Close()
_, err = io.Copy(tmpWriter, srcFile)
handleError(err)
}

157
pgpcrypt/crypt.go Normal file
View File

@ -0,0 +1,157 @@
package pgpcrypt
import (
"errors"
"io"
"os"
"golang.org/x/crypto/openpgp"
)
// Decrypt2 - decrypt
func Decrypt2(keyReader io.Reader, srcReader io.Reader, dstReader io.Writer, password string) (err error) {
keyList, err := ReadKeyFile(keyReader)
if err != nil {
return
}
if len(keyList) == 0 {
return errors.New("key not found")
}
key := keyList[0]
passphraseByte := []byte(password)
key.PrivateKey.Decrypt(passphraseByte)
for _, sub := range key.Subkeys {
sub.PrivateKey.Decrypt(passphraseByte)
}
md, err := openpgp.ReadMessage(srcReader, keyList, nil, nil)
if md == nil {
return errors.New("key password error")
}
_, err = io.Copy(dstReader, md.UnverifiedBody)
if err != nil {
return err
}
return
}
// Decrypt - decrypt file with pgp
func Decrypt(KeyFile, SrcFile, DstFile, Password string) error {
keyFile, err := os.Open(KeyFile)
if err != nil {
return err
}
defer keyFile.Close()
keyList, err := ReadKeyFile(keyFile)
if err != nil {
return err
}
if len(keyList) == 0 {
return errors.New("key not found")
}
key := keyList[0]
passphraseByte := []byte(Password)
key.PrivateKey.Decrypt(passphraseByte)
for _, sub := range key.Subkeys {
sub.PrivateKey.Decrypt(passphraseByte)
}
dstFile, err := os.Create(DstFile)
if err != nil {
return err
}
defer dstFile.Close()
srcFile, err := os.Open(SrcFile)
if err != nil {
return err
}
defer srcFile.Close()
md, err := openpgp.ReadMessage(srcFile, keyList, nil, nil)
if md == nil {
return errors.New("key password error")
}
_, err = io.Copy(dstFile, md.UnverifiedBody)
if err != nil {
return err
}
return nil
}
// Encrypt2 - encrypt
func Encrypt2(keyReader io.Reader, srcReader io.Reader, dstWriter io.Writer) (err error) {
keyList, err := ReadKeyFile(keyReader)
if err != nil {
return
}
fhint := &openpgp.FileHints{}
fhint.IsBinary = true
tmpWriter, err := openpgp.Encrypt(dstWriter, keyList, nil, fhint, nil)
if err != nil {
return
}
defer tmpWriter.Close()
_, err = io.Copy(tmpWriter, srcReader)
if err != nil {
return
}
return
}
// Encrypt - encrypt file with pgp
func Encrypt(KeyFile, SrcFile, DstFile string) error {
keyFile, err := os.Open(KeyFile)
if err != nil {
return err
}
defer keyFile.Close()
keyList, err := ReadKeyFile(keyFile)
if err != nil {
return err
}
distFile, err := os.Create(DstFile)
if err != nil {
return err
}
defer distFile.Close()
srcFile, err := os.Open(SrcFile)
if err != nil {
return err
}
defer srcFile.Close()
fhint := &openpgp.FileHints{}
fhint.IsBinary = true
tmpWriter, err := openpgp.Encrypt(distFile, keyList, nil, fhint, nil)
if err != nil {
return err
}
defer tmpWriter.Close()
_, err = io.Copy(tmpWriter, srcFile)
if err != nil {
return err
}
return nil
}

117
pgpcrypt/crypt_test.go Normal file
View File

@ -0,0 +1,117 @@
package pgpcrypt
import (
"bytes"
"testing"
)
var privKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: Keybase OpenPGP v1.0.0
Comment: https://keybase.io/crypto
xcFGBFqmSykBBADMW3p3YOiY4b1zi5C/tAYEF1iy55brS119LT0odc43XCTtPtG5
X9SzcSNrM/1azGHpLmgtjSbw8K9/GfS27E5MKGd4/9frpSAMdFutSmMEf+p/3vlb
xIbpI0PP0UdjIki3kK2+1W82Ca7JnnCOtP7o+01Q5pKmIfte+s0/Eg87mwARAQAB
/gkDCC7DXsV1FnCZYMxEN+8nT6bSTqEX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zgi5Th3yqDsTkNuzC+GYiJpxLz8W9TUb6tTHQjINqL8nlVGV6BS+
BEtVnQG1Fgs896jGJiGgn4QMWkCBjFhe6bDVgkt7CZKEXPjbdKL7IlK7sRooVYH2
S1O5T1EM72LKKHUUB/6Coj3+eC5RCl+hNYx5ks3CZsKUb49jkBkirSpkfQPi+OJS
zJK5fg+hio3PnT8kGqIz5j+lWOBSUOKd6Ta1E9/jJwpMLR6QmNZWEM3oUu24PA66
JW6gTGX3E8hEZ4s8Ftx+38kB22jUATLjGQ/5vLc9HY1vu6wAFcnxPRG5sXr3TG3/
JFRmu+z0g4yf6utsxCiT94Xss7lXDozDHuLRaXHVJGsAdWeZ88zof6Jx/V7YvzSy
XKJUMf9wE55rrl2A30hwFlxQF5skxssxVzFQaiPNne2jXcXl6/ZLliKFsP/MWJTA
ew7E38v7+lgkIl8xATUgKrSRPYKjh3ae1T26M/TLwsCDBBgBCgAPBQJapkspBQkP
CZwAAhsuAKgJEBxI5bOJwbX4nSAEGQEKAAYFAlqmSykACgkQXomQbPdo7ke9jgP+
ML1gMXOgtv9ujjJEULcXUldym6G0xu7z1kZcinABedCJ4PnrVD9sU91FYMdG8fA9
qTjKZ36Rf6qD1w49v2UNcBIDFYf97/nJ8/jdKg3cO3cSlmxwqlraXhJ2ZmMjj3mn
kgRfB61h0PICyj5X672MP3wD9Q3dJIYyslOC9i9wYd86rwP+NVrRTtr/cFipCygP
77r8yUptmMZX6KazcWZDL23vavT6mD8FaPMnNiXVyW264SwtnvTTvJcXQOwh/Bzl
sllR5Xi3nPICt/lUUfbXGZ8QpLV1NcQTB1bWD23E9XXfvRZK2hPWI26b5PkkX3bg
MwfEz7bN+ypIsO6oX6vthx/pe5Y=
=2HiH
-----END PGP PRIVATE KEY BLOCK-----`
func TestEncrypt(t *testing.T) {
keyIO := new(bytes.Buffer)
_, err := keyIO.Write([]byte(privKey))
if err != nil {
t.Error(err)
}
srcIO := new(bytes.Buffer)
_, err = srcIO.Write([]byte("test msg"))
if err != nil {
t.Error(err)
}
dstIO := new(bytes.Buffer)
err = Encrypt2(keyIO, srcIO, dstIO)
if err != nil {
t.Error(err)
}
}
func TestDecrypt(t *testing.T) {
keyIO := new(bytes.Buffer)
_, err := keyIO.Write([]byte(privKey))
if err != nil {
t.Error(err)
}
srcIO := new(bytes.Buffer)
_, err = srcIO.Write([]byte("test msg"))
if err != nil {
t.Error(err)
}
encIO := new(bytes.Buffer)
err = Encrypt2(keyIO, srcIO, encIO)
if err != nil {
t.Error(err)
}
keyIO2 := new(bytes.Buffer)
_, err = keyIO2.Write([]byte(privKey))
if err != nil {
t.Error(err)
}
decIO := new(bytes.Buffer)
err = Decrypt2(keyIO2, encIO, decIO, "12345")
if err != nil {
t.Error(err)
}
}