~bfiedler/go-threema

293b4955ddb3d6787cdf34c6cb77e7525da0d912 — Ben Fiedler 3 years ago 5fff32c
Use hex.EncodeToString where appropriate
4 files changed, 10 insertions(+), 23 deletions(-)

M client/client.go
M client/client_test.go
M client/crypto.go
M cmd/tcli/main.go
M client/client.go => client/client.go +5 -15
@@ 160,14 160,9 @@ func (c *client) ID() string {
	return c.id
}

// Returns the client's hex-encoded public key
//
// TODO: not sure if we maybe even want to return the raw bytes?
func (c *E2EClient) PublicKey() string {
	publicKey := computePublicKey(c.privateKey)
	encodedPublicKey := make([]byte, hex.EncodedLen(len(publicKey)))
	_ = hex.Encode(encodedPublicKey, publicKey[:])
	return string(encodedPublicKey)
// Returns the client's hex-encoded public key.
func (c *E2EClient) PublicKey() PublicKey {
	return computePublicKey(c.privateKey)
}

// Set the base URL for the Threema.Gateway server


@@ 427,16 422,11 @@ func (c *E2EClient) SendTextMessage(to ThreemaID, msg string) error {
		return err
	}

	encodedBox := make([]byte, hex.EncodedLen(len(box)))
	encodedNonce := make([]byte, hex.EncodedLen(len(nonce)))
	_ = hex.Encode(encodedBox, box)
	_ = hex.Encode(encodedNonce, nonce[:])

	form := url.Values{}
	form.Add("from", c.id)
	form.Add("to", to)
	form.Add("nonce", string(encodedNonce))
	form.Add("box", string(encodedBox))
	form.Add("nonce", hex.EncodeToString(nonce[:]))
	form.Add("box", hex.EncodeToString(box))
	form.Add("secret", c.apiSecret)

	resp, err := c.httpClient.PostForm(c.gatewayUrl+"/send_e2e", form)

M client/client_test.go => client/client_test.go +1 -1
@@ 28,7 28,7 @@ func TestID(t *testing.T) {
}

func TestPublicKey(t *testing.T) {
	assert.Equal(t, "9fead8301b0e13fea8569e7ec4e04e8086b2166e4bf7ef5fee0dd9c0a416dc6c", e2eClient.PublicKey())
	assert.Equal(t, publicKey, e2eClient.PublicKey())
}

func TestLookupIDByPhoneHash(t *testing.T) {

M client/crypto.go => client/crypto.go +2 -6
@@ 43,9 43,7 @@ func (c *client) hashPhone(phoneNumber string) (string, error) {

	c.phoneMu.Unlock()

	encodedHash := make([]byte, hex.EncodedLen(len(hashed)))
	hex.Encode(encodedHash, hashed)
	return string(encodedHash), nil
	return hex.EncodeToString(hashed), nil
}

// Hashes the given e-mail address using the defined HMAC function. Returns the


@@ 65,9 63,7 @@ func (c *client) hashEmail(address string) (string, error) {

	c.emailMu.Unlock()

	encodedHash := make([]byte, hex.EncodedLen(len(hashed)))
	hex.Encode(encodedHash, hashed)
	return string(encodedHash), nil
	return hex.EncodeToString(hashed), nil
}

// Constructs, encrypts and encodes `msg` as text message. Returns the

M cmd/tcli/main.go => cmd/tcli/main.go +2 -1
@@ 200,8 200,9 @@ func (qrCommand) run(c client.Client) error {
		return errors.New("The shell command 'qrencode' ist required for this to work.")
	}

	pubKey := e2eClient.PublicKey()
	// 3mid == Threema ID I guess?
	inp := fmt.Sprintf("3mid:%s,%s", c.ID(), e2eClient.PublicKey())
	inp := fmt.Sprintf("3mid:%s,%s", c.ID(), hex.EncodeToString(pubKey[:]))
	fmt.Println(inp)

	cmd := exec.Command(qrencode, "-t", "ANSI")