Partner Integration Docs
  • Getting Started
  • Architecture Overview
  • Survey Wall Integration
  • UAT Test
  • Troubleshooting
  • Code Snippet
    • Golang
    • Nodejs
    • Flutter
    • Java / Kotlin
Powered by GitBook
On this page
  • Hash Email and Phone Number
  • Encrypt Signed body
  1. Code Snippet

Golang

Golang Code Snippet

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.

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)
}

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.

package main

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
}
PreviousCode SnippetNextNodejs

Last updated 10 months ago