func (noiseConfig *NoiseConfig) Encrypt(message []byte) []byte {
	if !noiseConfig.initialized {
		return []byte("Error: noise session not initialized")
	}
	if noiseConfig.pairingVerificationRequired {
		message = []byte("Error: encrypted connection not verified")
	}
	return noiseConfig.sendCipher.Encrypt(nil, nil, message)
}
Encrypt takes a (plaintext) byte array message as arguments and returns a noise encrypted byte array per the configuration in NoiseConfig
If the encryption fails the function returns an empty byte array and an error.
package noisemanager
import (
	"bytes"
	"crypto/rand"
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"github.com/flynn/noise"
	"github.com/gorilla/websocket"
)
const (
	opICanHasHandShaek          = "h"
	responseSuccess             = "\x00"
	responseFailure             = "\x01"
	responseNeedsPairing        = "\x01"
	opICanHasPairinVerificashun = byte('v')
)
type NoiseConfig struct {
	clientStaticPubkey          []byte
	channelHash                 []byte
	sendCipher, receiveCipher   *noise.CipherState
	pairingVerificationRequired bool
	initialized                 bool
	dataDir                     string
	verifyPairing func(channelHash []byte) (bool, error)
}
func NewNoiseConfig(dataDir string, verifyPairing func([]byte) (bool, error)) *NoiseConfig {
	noise := &NoiseConfig{
		dataDir:       dataDir,
		initialized:   false,
		verifyPairing: verifyPairing,
	}
	return noise
}
func (noiseConfig *NoiseConfig) InitializeNoise(ws *websocket.Conn) error {
	cipherSuite := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
	keypair := noiseConfig.getMiddlewareNoiseStaticKeypair()
	if keypair == nil {
		if true {
			kp, err := cipherSuite.GenerateKeypair(rand.Reader)
			if err != nil {
				return errors.New("failed to generate a new noise keypair")
			}
			keypair = &kp
			if err := noiseConfig.setMiddlewareNoiseStaticKeypair(keypair); err != nil {
				log.Println("could not store app noise static keypair")
			}
		}
	}
	handshake, err := noise.NewHandshakeState(noise.Config{
		CipherSuite:   cipherSuite,
		Random:        rand.Reader,
		Pattern:       noise.HandshakeXX,
		StaticKeypair: *keypair,
		Prologue:      []byte("Noise_XX_25519_ChaChaPoly_SHA256"),
		Initiator:     false,
	})
	if err != nil {
		return errors.New("failed to generate a new noise handshake state for the wallet app communication with the BitBoxBase")
	}
	_, responseBytes, err := ws.ReadMessage()
	if err != nil {
		return errors.New("websocket failed to read noise handshake request")
	}
	if string(responseBytes) != string(opICanHasHandShaek) {
		return errors.New("initial response bytes did not match what we were expecting")
	}
	err = ws.WriteMessage(1, []byte(responseSuccess))
	if err != nil {
		return errors.New("websocket failed to write the noise handshake request response")
	}
	_, responseBytes, err = ws.ReadMessage()
	if err != nil {
		return errors.New("websocket failed to read first noise handshake message")
	}
	_, _, _, err = handshake.ReadMessage(nil, responseBytes)
	if err != nil {
		return errors.New("noise failed to read first noise handshake message")
	}
	msg, _, _, err := handshake.WriteMessage(nil, nil)
	if err != nil {
		return errors.New("noise failed to write second noise handshake message")
	}
	err = ws.WriteMessage(1, msg)
	if err != nil {
		return errors.New("websocket failed to write second noise handshake message")
	}
	_, responseBytes, err = ws.ReadMessage()
	if err != nil {
		return errors.New("websocket failed to read third noise handshake message")
	}
	_, noiseConfig.sendCipher, noiseConfig.receiveCipher, err = handshake.ReadMessage(nil, responseBytes)
	if err != nil {
		return errors.New("noise failed to read the third noise handshake message")
	}
	noiseConfig.clientStaticPubkey = handshake.PeerStatic()
	if len(noiseConfig.clientStaticPubkey) != 32 {
		return errors.New("expected 32 byte remote static pubkey")
	}
	noiseConfig.pairingVerificationRequired = !noiseConfig.containsClientStaticPubkey(noiseConfig.clientStaticPubkey)
	if noiseConfig.pairingVerificationRequired {
		err = ws.WriteMessage(websocket.BinaryMessage, []byte(responseNeedsPairing))
		if err != nil {
			return errors.New("websocket failed to write second noise handshake message")
		}
	} else {
		err = ws.WriteMessage(websocket.BinaryMessage, []byte(responseSuccess))
		if err != nil {
			return errors.New("websocket failed to write second noise handshake message")
		}
	}
	noiseConfig.channelHash = handshake.ChannelBinding()
	noiseConfig.initialized = true
	_, responseBytes, err = ws.ReadMessage()
	if err != nil {
		return errors.New("websocket failed to read verification request")
	}
	if responseBytes[0] == opICanHasPairinVerificashun {
		log.Println("Need to verify pairing hash")
		msg, err := noiseConfig.CheckVerification()
		if err != nil {
			return err
		}
		err = ws.WriteMessage(websocket.BinaryMessage, msg)
		if err != nil {
			log.Println("Error, websocket failed to write channel hash verification message")
		}
	}
	log.Println("Successfully completed noise handshake with client")
	return nil
}
func (noiseConfig *NoiseConfig) CheckVerification() ([]byte, error) {
	accepted, err := noiseConfig.verifyPairing(noiseConfig.channelHash)
	if err != nil {
		return nil, err
	}
	if accepted {
		err = noiseConfig.addClientStaticPubkey(noiseConfig.clientStaticPubkey)
		if err != nil {
			log.Println("Pairing Successful, but unable to write baseNoiseStaticPubkey to file")
		}
		noiseConfig.pairingVerificationRequired = false
		return []byte(responseSuccess), nil
	}
	return []byte(responseFailure), nil
}