Skip to content
Permalink
Browse files

Additional Go Report fixes (#426)

* go fmt: reformat the code
* go lint: use camelCase instead of snake_case
* go vet: fix error printing in tools

We should use fmt.Fprintln() which actually accepts io.Writer as the
first argument, not fmt.Println() which always prints to stdout.
  • Loading branch information...
ilammy committed Mar 12, 2019
1 parent c072a00 commit f322b55504f1f2ebe5e97dc7294f749b92b5ee40
@@ -8,31 +8,31 @@ import (
)

func main() {
args_count := len(os.Args)
argsCount := len(os.Args)

if args_count != 1 && args_count != 3 {
if argsCount != 1 && argsCount != 3 {
fmt.Printf("Usage: go %s <private_key_path> <public_key_path>\n", os.Args[0])
os.Exit(1)
}

var private_key_path, public_key_path string
if args_count == 1 {
private_key_path = "key"
public_key_path = "key.pub"
} else if args_count == 3 {
private_key_path = os.Args[1]
public_key_path = os.Args[2]
var privateKeyPath, publicKeyPath string
if argsCount == 1 {
privateKeyPath = "key"
publicKeyPath = "key.pub"
} else if argsCount == 3 {
privateKeyPath = os.Args[1]
publicKeyPath = os.Args[2]
}

keypair, err := keys.New(keys.KEYTYPE_EC)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(private_key_path, keypair.Private.Value, 0400)
err = ioutil.WriteFile(privateKeyPath, keypair.Private.Value, 0400)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(public_key_path, keypair.Public.Value, 0666)
err = ioutil.WriteFile(publicKeyPath, keypair.Public.Value, 0666)
if err != nil {
panic(err)
}
@@ -25,26 +25,26 @@ func main() {
if "enc" == command {
encData, _, err := sc.Protect([]byte(message), context)
if nil != err {
fmt.Println(os.Stderr, "Error encrypting message")
fmt.Fprintln(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))

} else if "dec" == command {
decoded_message, err := base64.StdEncoding.DecodeString(message)
decodedMessage, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Println(os.Stderr, "Error decoding message")
fmt.Fprintln(os.Stderr, "Error decoding message")
os.Exit(1)
}
decData, err := sc.Unprotect(decoded_message, nil, context)
decData, err := sc.Unprotect(decodedMessage, nil, context)
if nil != err {
fmt.Println(os.Stderr, "Error decrypting message")
fmt.Fprintln(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))

} else {
fmt.Println(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
fmt.Fprintln(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
os.Exit(1)
}
}
@@ -29,26 +29,26 @@ func main() {
if "enc" == command {
encData, _, err := sc.Protect([]byte(message), context)
if nil != err {
fmt.Println(os.Stderr, "Error encrypting message")
fmt.Fprintln(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))

} else if "dec" == command {
decoded_message, err := base64.StdEncoding.DecodeString(message)
decodedMessage, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Println(os.Stderr, "Error decoding message")
fmt.Fprintln(os.Stderr, "Error decoding message")
os.Exit(1)
}
decData, err := sc.Unprotect(decoded_message, nil, context)
decData, err := sc.Unprotect(decodedMessage, nil, context)
if nil != err {
fmt.Println(os.Stderr, "Error decrypting message")
fmt.Fprintln(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))

} else {
fmt.Println(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
fmt.Fprintln(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
os.Exit(1)
}
}
@@ -19,12 +19,12 @@ func main() {
command := args[1]
key := args[2]

message_and_token := strings.Split(args[3], ",")
message := message_and_token[0]
messageAndToken := strings.Split(args[3], ",")
message := messageAndToken[0]

var token string
if len(message_and_token) == 2 {
token = message_and_token[1]
if len(messageAndToken) == 2 {
token = messageAndToken[1]
}

var context []byte
@@ -38,31 +38,31 @@ func main() {
if "enc" == command {
encData, encToken, err := sc.Protect([]byte(message), context)
if nil != err {
fmt.Println(os.Stderr, "Error encrypting message")
fmt.Fprintln(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData) + "," + base64.StdEncoding.EncodeToString(encToken))

} else if "dec" == command {
decoded_message, err := base64.StdEncoding.DecodeString(message)
decodedMessage, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Println(os.Stderr, "Error decoding message")
fmt.Fprintln(os.Stderr, "Error decoding message")
os.Exit(1)
}
decoded_token, err := base64.StdEncoding.DecodeString(token)
decodedToken, err := base64.StdEncoding.DecodeString(token)
if nil != err {
fmt.Println("Error decoding token")
fmt.Fprintln(os.Stderr, "Error decoding token")
os.Exit(1)
}
decData, err := sc.Unprotect(decoded_message, decoded_token, context)
decData, err := sc.Unprotect(decodedMessage, decodedToken, context)
if nil != err {
fmt.Println(os.Stderr, "Error decrypting message")
fmt.Fprintln(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))

} else {
fmt.Println(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
fmt.Fprintln(os.Stderr, "Wrong command, use \"enc\" or \"dec\"")
os.Exit(1)
}
}
@@ -3,10 +3,10 @@ package main
import (
"encoding/base64"
"fmt"
"github.com/cossacklabs/themis/gothemis/keys"
smessage "github.com/cossacklabs/themis/gothemis/message"
"os"
"io/ioutil"
"github.com/cossacklabs/themis/gothemis/keys"
"os"
)

func main() {
@@ -18,65 +18,65 @@ func main() {
}

command := args[1]
private_key_path := args[2]
private_key, err := ioutil.ReadFile(string(private_key_path))
privateKeyPath := args[2]
privateKey, err := ioutil.ReadFile(string(privateKeyPath))
if err != nil {
panic(err)
}
public_key_path := args[3]
public_key, err := ioutil.ReadFile(string(public_key_path))
publicKeyPath := args[3]
publicKey, err := ioutil.ReadFile(string(publicKeyPath))
if err != nil {
panic(err)
}
message := args[4]

message_encrypter := smessage.New(&keys.PrivateKey{Value: private_key}, &keys.PublicKey{Value: public_key})
messageEncrypter := smessage.New(&keys.PrivateKey{Value: privateKey}, &keys.PublicKey{Value: publicKey})

if "enc" == command {
encData, err := message_encrypter.Wrap([]byte(message))
encData, err := messageEncrypter.Wrap([]byte(message))
if nil != err {
fmt.Println(os.Stderr, "Error encrypting message")
fmt.Fprintln(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))

} else if "dec" == command {
decoded_message, err := base64.StdEncoding.DecodeString(message)
decodedMessage, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Println(os.Stderr, "Error decoding message")
fmt.Fprintln(os.Stderr, "Error decoding message")
os.Exit(1)
}

decData, err := message_encrypter.Unwrap(decoded_message)
decData, err := messageEncrypter.Unwrap(decodedMessage)
if nil != err {
fmt.Println(os.Stderr, "Error decrypting message")
fmt.Fprintln(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))

} else if "sign" == command {
encData, err := message_encrypter.Sign([]byte(message))
if nil != err {
fmt.Println(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))
} else if "verify" == command {
decoded_message, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Println(os.Stderr, "Error decoding message")
os.Exit(1)
}
encData, err := messageEncrypter.Sign([]byte(message))
if nil != err {
fmt.Fprintln(os.Stderr, "Error encrypting message")
os.Exit(1)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))
} else if "verify" == command {
decodedMessage, err := base64.StdEncoding.DecodeString(message)
if nil != err {
fmt.Fprintln(os.Stderr, "Error decoding message")
os.Exit(1)
}

decData, err := message_encrypter.Verify(decoded_message)
if nil != err {
fmt.Println(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))
decData, err := messageEncrypter.Verify(decodedMessage)
if nil != err {
fmt.Fprintln(os.Stderr, "Error decrypting message")
os.Exit(1)
}
fmt.Println(string(decData[:]))

} else {
fmt.Println(os.Stderr, "Wrong command, use <enc | dev | sign | verify>")
} else {
fmt.Fprintln(os.Stderr, "Wrong command, use <enc | dev | sign | verify>")
os.Exit(1)
}
}

0 comments on commit f322b55

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.