# Golang

### Hash Email and Phone Number

Dependency:

* `crypto/sha256`: This package provides the SHA-256 hash function.
* `encoding/hex`: This package is used to encode the hashed byte array to a hexadecimal string.

{% code lineNumbers="true" %}

```go
package main

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

func main() {
	email := "example@example.com"
	phone := "+1234567890"

	// Hash email
	emailHash := hashSHA256(email)
	fmt.Println("Hashed email:", emailHash)

	// Hash phone number
	phoneHash := hashSHA256(phone)
	fmt.Println("Hashed phone number:", phoneHash)
}

// Function to hash using SHA-256
func hashSHA256(input string) string {
	hasher := sha256.New()
	hasher.Write([]byte(input))
	hashed := hasher.Sum(nil)
	return hex.EncodeToString(hashed)
}

```

{% endcode %}

### Encrypt Signed body

Dependency:

* **`crypto/aes`**:
  * This package provides the Advanced Encryption Standard (AES) symmetric encryption algorithm. AES is widely used for encrypting and decrypting data securely. It supports various key sizes (AES-128, AES-192, AES-256).
* **`crypto/cipher`**:
  * The `cipher` package provides interfaces for symmetric key block ciphers such as AES. It defines common operations and types used in cryptographic implementations, including encryption modes (like CBC, GCM) and padding schemes.
* **`encoding/base64`**:
  * The `base64` package provides functions for encoding and decoding data using Base64 encoding. Base64 encoding converts binary data into a text-like format, which is useful for transmitting binary data over text-based protocols or storing binary data as text.
* **`encoding/hex`**:
  * The `hex` package provides functions for encoding and decoding hexadecimal strings. It allows converting binary data into a hexadecimal representation and vice versa. Hexadecimal encoding is often used to display binary data in a human-readable format.
* **`encoding/json`**:
  * The `json` package provides encoding and decoding functions for JSON data. It allows converting Go data structures (like structs, maps, slices) into JSON format and vice versa. JSON (JavaScript Object Notation) is a popular data interchange format used in web applications and APIs.

<pre class="language-go"><code class="lang-go"><strong>package main
</strong>
import (
	"crypto/aes"
	"crypto/cipher"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
)

type Payload struct {
	PartnerId   string
	Email       string
	PhoneNumber string
}

func main() {
	partnerId := "YOUR-VALID-PARTNER-ID"
	partnerSecret := "8d8dd8af95971d949f4c6a941bc86696"

	// hashed value SHA256
	payload := Payload{
		PartnerId:   partnerId,
		Email:       hashSHA256("user@email.com"),
		PhoneNumber: hashSHA256("085123456789"),
	}

	marshaledPayload, err := json.Marshal(payload)
	finalPayload := string(marshaledPayload)

	res, err := Encrypt(finalPayload, partnerSecret)
	if err != nil {
		panic(err)
	}

	fmt.Println(res)
}

func Encrypt(str string, secret string) (string, error) {
	key, err := hex.DecodeString(secret)
	if err != nil {
		fmt.Println("Invalid AES key:", err)
		return "", err
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}

	strBytes := []byte(str)
	cfb := cipher.NewCFBEncrypter(block, key[:aes.BlockSize])
	cipherText := make([]byte, len(strBytes))
	cfb.XORKeyStream(cipherText, strBytes)

	return base64.StdEncoding.EncodeToString(cipherText), nil
}

</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://responova-pi.populix.co/code-snippet/golang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
