Update lint (#44)
* Reduce complexity of SignatureCollector.Accept * Add stylecheck linter, and lint accordingly * Rephrase some comments * godot - Top level comments should end with a dot. * nestif - Reduce nested complexity of code * Review changes Co-authored-by: Aron Wussler <aron@wussler.it>
This commit is contained in:
parent
222decb919
commit
ac8a49c114
15 changed files with 252 additions and 265 deletions
|
|
@ -7,8 +7,9 @@ import (
|
|||
"github.com/ProtonMail/gopenpgp/v2/internal"
|
||||
)
|
||||
|
||||
// SignCleartextMessageArmored signs text given a private key and its passphrase, canonicalizes and trims the newlines,
|
||||
// and returns the PGP-compliant special armoring
|
||||
// SignCleartextMessageArmored signs text given a private key and its
|
||||
// passphrase, canonicalizes and trims the newlines, and returns the
|
||||
// PGP-compliant special armoring.
|
||||
func SignCleartextMessageArmored(privateKey string, passphrase []byte, text string) (string, error) {
|
||||
signingKey, err := crypto.NewKeyFromArmored(privateKey)
|
||||
if err != nil {
|
||||
|
|
@ -29,8 +30,9 @@ func SignCleartextMessageArmored(privateKey string, passphrase []byte, text stri
|
|||
return SignCleartextMessage(keyRing, text)
|
||||
}
|
||||
|
||||
// VerifyCleartextMessageArmored verifies PGP-compliant armored signed plain text given the public key
|
||||
// and returns the text or err if the verification fails
|
||||
// VerifyCleartextMessageArmored verifies PGP-compliant armored signed plain
|
||||
// text given the public key and returns the text or err if the verification
|
||||
// fails.
|
||||
func VerifyCleartextMessageArmored(publicKey, armored string, verifyTime int64) (string, error) {
|
||||
signingKey, err := crypto.NewKeyFromArmored(publicKey)
|
||||
if err != nil {
|
||||
|
|
@ -45,8 +47,8 @@ func VerifyCleartextMessageArmored(publicKey, armored string, verifyTime int64)
|
|||
return VerifyCleartextMessage(verifyKeyRing, armored, verifyTime)
|
||||
}
|
||||
|
||||
// SignCleartextMessage signs text given a private keyring, canonicalizes and trims the newlines,
|
||||
// and returns the PGP-compliant special armoring
|
||||
// SignCleartextMessage signs text given a private keyring, canonicalizes and
|
||||
// trims the newlines, and returns the PGP-compliant special armoring.
|
||||
func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) {
|
||||
text = canonicalizeAndTrim(text)
|
||||
message := crypto.NewPlainMessageFromString(text)
|
||||
|
|
@ -59,8 +61,9 @@ func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error)
|
|||
return crypto.NewClearTextMessage(message.GetBinary(), signature.GetBinary()).GetArmored()
|
||||
}
|
||||
|
||||
// VerifyCleartextMessage verifies PGP-compliant armored signed plain text given the public keyring
|
||||
// and returns the text or err if the verification fails
|
||||
// VerifyCleartextMessage verifies PGP-compliant armored signed plain text
|
||||
// given the public keyring and returns the text or err if the verification
|
||||
// fails.
|
||||
func VerifyCleartextMessage(keyRing *crypto.KeyRing, armored string, verifyTime int64) (string, error) {
|
||||
clearTextMessage, err := crypto.NewClearTextMessageFromArmored(armored)
|
||||
if err != nil {
|
||||
|
|
@ -79,7 +82,7 @@ func VerifyCleartextMessage(keyRing *crypto.KeyRing, armored string, verifyTime
|
|||
|
||||
// ----- INTERNAL FUNCTIONS -----
|
||||
|
||||
// canonicalizeAndTrim alters a string canonicalizing and trimming the newlines
|
||||
// canonicalizeAndTrim alters a string canonicalizing and trimming the newlines.
|
||||
func canonicalizeAndTrim(text string) string {
|
||||
text = internal.TrimNewlines(text)
|
||||
text = strings.Replace(strings.Replace(text, "\r\n", "\n", -1), "\n", "\r\n", -1)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// helper contains several functions with a simple interface to extend usability and compatibility with gomobile
|
||||
// Package helper contains several functions with a simple interface to extend usability and compatibility with gomobile
|
||||
package helper
|
||||
|
||||
import (
|
||||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
)
|
||||
|
||||
// EncryptMessageWithPassword encrypts a string with a passphrase using AES256
|
||||
// EncryptMessageWithPassword encrypts a string with a passphrase using AES256.
|
||||
func EncryptMessageWithPassword(password []byte, plaintext string) (ciphertext string, err error) {
|
||||
var pgpMessage *crypto.PGPMessage
|
||||
|
||||
|
|
@ -41,7 +41,8 @@ func DecryptMessageWithPassword(password []byte, ciphertext string) (plaintext s
|
|||
return message.GetString(), nil
|
||||
}
|
||||
|
||||
// EncryptMessageArmored generates an armored PGP message given a plaintext and an armored public key
|
||||
// EncryptMessageArmored generates an armored PGP message given a plaintext and
|
||||
// an armored public key.
|
||||
func EncryptMessageArmored(key, plaintext string) (ciphertext string, err error) {
|
||||
var publicKey *crypto.Key
|
||||
var publicKeyRing *crypto.KeyRing
|
||||
|
|
@ -68,8 +69,8 @@ func EncryptMessageArmored(key, plaintext string) (ciphertext string, err error)
|
|||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// EncryptSignMessageArmored generates an armored signed PGP message given a plaintext and an armored public key
|
||||
// a private key and its passphrase
|
||||
// EncryptSignMessageArmored generates an armored signed PGP message given a
|
||||
// plaintext and an armored public key a private key and its passphrase.
|
||||
func EncryptSignMessageArmored(
|
||||
publicKey, privateKey string, passphrase []byte, plaintext string,
|
||||
) (ciphertext string, err error) {
|
||||
|
|
@ -111,7 +112,8 @@ func EncryptSignMessageArmored(
|
|||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// DecryptMessageArmored decrypts an armored PGP message given a private key and its passphrase
|
||||
// DecryptMessageArmored decrypts an armored PGP message given a private key
|
||||
// and its passphrase.
|
||||
func DecryptMessageArmored(
|
||||
privateKey string, passphrase []byte, ciphertext string,
|
||||
) (plaintext string, err error) {
|
||||
|
|
@ -144,9 +146,9 @@ func DecryptMessageArmored(
|
|||
return message.GetString(), nil
|
||||
}
|
||||
|
||||
// DecryptVerifyMessageArmored decrypts an armored PGP message given a private key and its passphrase
|
||||
// and verifies the embedded signature.
|
||||
// Returns the plain data or an error on signature verification failure.
|
||||
// DecryptVerifyMessageArmored decrypts an armored PGP message given a private
|
||||
// key and its passphrase and verifies the embedded signature. Returns the
|
||||
// plain data or an error on signature verification failure.
|
||||
func DecryptVerifyMessageArmored(
|
||||
publicKey, privateKey string, passphrase []byte, ciphertext string,
|
||||
) (plaintext string, err error) {
|
||||
|
|
@ -187,9 +189,10 @@ func DecryptVerifyMessageArmored(
|
|||
return message.GetString(), nil
|
||||
}
|
||||
|
||||
// DecryptVerifyAttachment decrypts and verifies an attachment split into the keyPacket, dataPacket
|
||||
// and an armored (!) signature, given a publicKey, and a privateKey with its passphrase.
|
||||
// Returns the plain data or an error on signature verification failure.
|
||||
// DecryptVerifyAttachment decrypts and verifies an attachment split into the
|
||||
// keyPacket, dataPacket and an armored (!) signature, given a publicKey, and a
|
||||
// privateKey with its passphrase. Returns the plain data or an error on
|
||||
// signature verification failure.
|
||||
func DecryptVerifyAttachment(
|
||||
publicKey, privateKey string,
|
||||
passphrase, keyPacket, dataPacket []byte,
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ type ExplicitVerifyMessage struct {
|
|||
SignatureVerificationError *crypto.SignatureVerificationError
|
||||
}
|
||||
|
||||
// DecryptVerifyMessageArmored decrypts an armored PGP message given a private key and its passphrase
|
||||
// and verifies the embedded signature.
|
||||
// Returns the plain data or an error on signature verification failure.
|
||||
// DecryptExplicitVerify decrypts an armored PGP message given a private key
|
||||
// and its passphrase and verifies the embedded signature. Returns the plain
|
||||
// data or an error on signature verification failure.
|
||||
func DecryptExplicitVerify(
|
||||
pgpMessage *crypto.PGPMessage,
|
||||
privateKeyRing, publicKeyRing *crypto.KeyRing,
|
||||
|
|
@ -57,8 +57,9 @@ func DecryptAttachment(keyPacket []byte, dataPacket []byte, keyRing *crypto.KeyR
|
|||
}
|
||||
|
||||
// EncryptAttachment encrypts a file given a plainData and a fileName.
|
||||
// Returns a PGPSplitMessage containing a session key packet and symmetrically encrypted data.
|
||||
// Specifically designed for attachments rather than text messages.
|
||||
// Returns a PGPSplitMessage containing a session key packet and symmetrically
|
||||
// encrypted data. Specifically designed for attachments rather than text
|
||||
// messages.
|
||||
func EncryptAttachment(plainData []byte, fileName string, keyRing *crypto.KeyRing) (*crypto.PGPSplitMessage, error) {
|
||||
plainMessage := crypto.NewPlainMessage(plainData)
|
||||
decrypted, err := keyRing.EncryptAttachment(plainMessage, fileName)
|
||||
|
|
@ -68,8 +69,8 @@ func EncryptAttachment(plainData []byte, fileName string, keyRing *crypto.KeyRin
|
|||
return decrypted, nil
|
||||
}
|
||||
|
||||
// GetJsonSHA256Fingerprints returns the SHA256 fingeprints of key and subkeys, encoded in JSON, since gomobile can not
|
||||
// handle arrays
|
||||
// GetJsonSHA256Fingerprints returns the SHA256 fingeprints of key and subkeys,
|
||||
// encoded in JSON, since gomobile can not handle arrays.
|
||||
func GetJsonSHA256Fingerprints(publicKey string) ([]byte, error) {
|
||||
key, err := crypto.NewKeyFromArmored(publicKey)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue