diff options
author | 2018-03-21 17:16:13 -0700 | |
---|---|---|
committer | 2018-03-21 17:18:53 -0700 | |
commit | a02a75b47cd9850c3f9be17186a21316ac880184 (patch) | |
tree | 51166ff264b2ab869857e98a0314c71741134828 /sms-service/src | |
parent | 6e8ef8ac3a9f9504ff4a5ea9991197010930d834 (diff) |
Adding PGP decrypt to auth package
Adding PGP decrypt function to auth.
This can be used by quorum client in its code for decryption
Issue-ID: AAF-165
Change-Id: I6d236c3164ccb93c11527ac8c2e725987236f9f0
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'sms-service/src')
-rw-r--r-- | sms-service/src/sms/auth/auth.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/sms-service/src/sms/auth/auth.go b/sms-service/src/sms/auth/auth.go index 341f377..ad5afdc 100644 --- a/sms-service/src/sms/auth/auth.go +++ b/sms-service/src/sms/auth/auth.go @@ -22,6 +22,7 @@ import ( "crypto/x509" "encoding/base64" "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/packet" "io/ioutil" smslogger "sms/log" @@ -93,3 +94,38 @@ func GeneratePGPKeyPair() (string, string, error) { return pbkey, prkey, nil } + +// DecryptPGPBytes decrypts a PGP encoded input string and returns +// a base64 representation of the decoded string +func DecryptPGPBytes(data string, prKey string) (string, error) { + // Convert private key to bytes from base64 + prKeyBytes, err := base64.StdEncoding.DecodeString(prKey) + if err != nil { + smslogger.WriteError("Error Decoding base64 private key: " + err.Error()) + return "", err + } + + dataBytes, err := base64.StdEncoding.DecodeString(data) + if err != nil { + smslogger.WriteError("Error Decoding base64 data: " + err.Error()) + return "", err + } + + prEntity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(prKeyBytes))) + if err != nil { + smslogger.WriteError("Error reading entity from PGP key: " + err.Error()) + return "", err + } + + prEntityList := &openpgp.EntityList{prEntity} + message, err := openpgp.ReadMessage(bytes.NewBuffer(dataBytes), prEntityList, nil, nil) + if err != nil { + smslogger.WriteError("Error Decrypting message: " + err.Error()) + return "", err + } + + var retBuf bytes.Buffer + retBuf.ReadFrom(message.UnverifiedBody) + + return retBuf.String(), nil +} |