tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit c787512bf95b13fd861fa7d881d2e10dbfb408d0
parent 66f1745bb34516d63b6bbde0e3dedf4dcbc41543
Author: parazyd <parazyd@dyne.org>
Date:   Tue, 13 Mar 2018 11:36:34 +0100

Add some damlib style cleanups.

Diffstat:
Mpkg/damlib/crypto_25519.go | 42++++++++++--------------------------------
Mpkg/damlib/crypto_rsa.go | 14++++----------
Mpkg/damlib/validate.go | 6+++---
3 files changed, 17 insertions(+), 45 deletions(-)

diff --git a/pkg/damlib/crypto_25519.go b/pkg/damlib/crypto_25519.go @@ -48,16 +48,9 @@ func SavePubEd25519(filename string, key ed25519.PublicKey) error { log.Println("Writing ed25519 public key to", filename) const pkprefix = "== ed25519v1-public: type0 ==" var pub []byte - for _, i := range []byte(pkprefix) { - pub = append(pub, i) - } - for _, i := range []byte(key) { - pub = append(pub, i) - } - if err := ioutil.WriteFile(filename, pub, 0600); err != nil { - return err - } - return nil + pub = append(pub, []byte(pkprefix)...) + pub = append(pub, []byte(key)...) + return ioutil.WriteFile(filename, pub, 0600) } // SavePrivEd25519 writes a ed25519.PrivateKey type to a given string filename. @@ -66,16 +59,9 @@ func SavePrivEd25519(filename string, key ed25519.PrivateKey) error { log.Println("Writing ed25519 private key to", filename) const skprefix = "== ed25519v1-secret: type0 ==" var sec []byte - for _, i := range []byte(skprefix) { - sec = append(sec, i) - } - for _, i := range []byte(key) { - sec = append(sec, i) - } - if err := ioutil.WriteFile(filename, sec, 0600); err != nil { - return err - } - return nil + sec = append(sec, []byte(skprefix)...) + sec = append(sec, []byte(key)...) + return ioutil.WriteFile(filename, sec, 0600) } // OnionFromPubkeyEd25519 generates a valid onion address from a given ed25519 @@ -101,24 +87,16 @@ func OnionFromPubkeyEd25519(pubkey ed25519.PublicKey) []byte { const versConst = '\x03' var h []byte - for _, i := range []byte(hashConst) { - h = append(h, i) - } - for _, i := range []byte(pubkey) { - h = append(h, i) - } + h = append(h, []byte(hashConst)...) + h = append(h, []byte(pubkey)...) h = append(h, byte(versConst)) csum := sha3.Sum256(h) checksum := csum[:2] var enc []byte - for _, i := range []byte(pubkey) { - enc = append(enc, i) - } - for _, i := range checksum { - enc = append(enc, i) - } + enc = append(enc, []byte(pubkey)...) + enc = append(enc, checksum...) enc = append(enc, byte(versConst)) encoded := base32.StdEncoding.EncodeToString(enc) diff --git a/pkg/damlib/crypto_rsa.go b/pkg/damlib/crypto_rsa.go @@ -55,10 +55,10 @@ func GenRsa(bitSize int) (*rsa.PrivateKey, error) { func SavePubRsa(filename string, pubkey rsa.PublicKey) error { log.Println("Writing RSA pubkey to", filename) outfile, err := os.Create(filename) - defer outfile.Close() if err != nil { return err } + defer outfile.Close() asn1Bytes, err := asn1.Marshal(pubkey) if err != nil { return err @@ -70,10 +70,7 @@ func SavePubRsa(filename string, pubkey rsa.PublicKey) error { if err = pem.Encode(outfile, pemkey); err != nil { return err } - if err = outfile.Chmod(0400); err != nil { - return err - } - return nil + return outfile.Chmod(0400) } // SavePrivRsa saves a given RSA private key to a given filename. @@ -82,10 +79,10 @@ func SavePubRsa(filename string, pubkey rsa.PublicKey) error { func SavePrivRsa(filename string, privkey *rsa.PrivateKey) error { log.Printf("Writing private key to %s\n", filename) outfile, err := os.Create(filename) - defer outfile.Close() if err != nil { return err } + defer outfile.Close() var pemkey = &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privkey), @@ -93,10 +90,7 @@ func SavePrivRsa(filename string, privkey *rsa.PrivateKey) error { if err = pem.Encode(outfile, pemkey); err != nil { return err } - if err = outfile.Chmod(0400); err != nil { - return err - } - return nil + return outfile.Chmod(0400) } // LoadRsaKeyFromFile loads a RSA private key from a given filename. diff --git a/pkg/damlib/validate.go b/pkg/damlib/validate.go @@ -32,7 +32,7 @@ import ( // ValidateOnionAddress matches a string against a regular expression matching // a Tor hidden service address. Returns true on success and false on failure. func ValidateOnionAddress(addr string) bool { - re, _ := regexp.Compile("^[a-z2-7](?:.{55}|.{15})\\.onion") + re, _ := regexp.Compile(`^[a-z2-7](?:.{55}|.{15})\.onion`) if len(re.FindString(addr)) == 22 || len(re.FindString(addr)) == 62 { return true } @@ -129,7 +129,7 @@ func ValidateFirstHandshake(req map[string]string) (bool, string) { // Validate signature. msg := []byte(req["message"]) decSig, _ := base64.StdEncoding.DecodeString(req["signature"]) - sig := []byte(decSig) + sig := decSig pubkey, err := ParsePubkeyRsa([]byte(pub)) // pubkey is their public key in *rsa.PublicKey type CheckError(err) if val, _ := VerifyMsgRsa(msg, sig, pubkey); !(val) { @@ -221,7 +221,7 @@ func ValidateSecondHandshake(req map[string]string) (bool, string) { // Validate signature. msg := []byte(req["message"]) decSig, _ := base64.StdEncoding.DecodeString(req["signature"]) - sig := []byte(decSig) + sig := decSig pubkey, err := ParsePubkeyRsa([]byte(pub)) // pubkey is their public key in *rsa.PublicKey type CheckError(err) if val, _ := VerifyMsgRsa(msg, sig, pubkey); !(val) {