<commit_before>package main

import (
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path"

	"golang.org/x/crypto/openpgp/armor"

	"github.com/pkg/errors"
	"github.com/rakoo/goax"
)

func send(peer string) {
	r, err := openRatchet(peer)
	if err != nil {
		if err == errNoRatchet {
			fmt.Printf("No ratchet for %s, please send this to the peer and \"receive\" what they send you back", peer)
			fmt.Println("\n")
			sendRatchet(peer)
			fmt.Println("")
			os.Exit(0)
		} else {
			log.Fatal(err)
		}
	}

	msg, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal("Couldn't read all stdin")
	}
	cipherText := r.Encrypt(msg)
	if err := saveRatchet(r, peer); err != nil {
		log.Println("Couldn't save ratchet:", err)
		os.Remove(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
		os.Exit(1)
	}

	log.Println(len(cipherText))
}

var errNoRatchet = errors.New("No ratchet")

var errInvalidRatchet = errors.New("Invalid ratchet")

func openRatchet(peer string) (r *goax.Ratchet, err error) {
	f, err := os.Open(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
	if err != nil {
		return nil, errNoRatchet
	}
	defer f.Close()

	r = new(goax.Ratchet)
	armorDecoder, err := armor.Decode(f)
	if err != nil {
		return nil, errors.Wrap(err, "Error opening decoder")
	}
	err = json.NewDecoder(armorDecoder.Body).Decode(r)
	if err != nil {
		return nil, errInvalidRatchet
	}

	return r, nil
}

func createRatchet(peer string) (r *goax.Ratchet, err error) {
	myIdentityKeyPrivate := getPrivateKey()
	var asArray [32]byte
	copy(asArray[:], myIdentityKeyPrivate)
	r = goax.New(rand.Reader, asArray)
	err = saveRatchet(r, peer)
	return r, err
}

func saveRatchet(r *goax.Ratchet, peer string) error {
	os.MkdirAll("ratchets", 0755)
	f, err := os.Create(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
	if err != nil {
		return errors.Wrap(err, "Couldn't create ratchet file")
	}
	defer f.Close()

	armorEncoder, err := armor.Encode(f, "GOAX RATCHET", nil)
	if err != nil {
		return errors.Wrap(err, "Couldn't create armor encoder")
	}
	err = json.NewEncoder(armorEncoder).Encode(r)
	if err != nil {
		return errors.Wrap(err, "Couldn't marshall ratchet")
	}
	err = armorEncoder.Close()
	if err != nil {<commit_msg>Print encrypted message in armor encoding<commit_after>package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"

	"golang.org/x/crypto/openpgp/armor"

	"github.com/pkg/errors"
	"github.com/rakoo/goax"
)

func send(peer string) {
	r, err := openRatchet(peer)
	if err != nil {
		if err == errNoRatchet {
			fmt.Printf("No ratchet for %s, please send this to the peer and \"receive\" what they send you back", peer)
			fmt.Println("\n")
			sendRatchet(peer)
			fmt.Println("")
			os.Exit(0)
		} else {
			log.Fatal(err)
		}
	}

	msg, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal("Couldn't read all stdin")
	}
	cipherText := r.Encrypt(msg)
	if err := saveRatchet(r, peer); err != nil {
		log.Println("Couldn't save ratchet:", err)
		os.Remove(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
		os.Exit(1)
	}

	encoder, err := armor.Encode(os.Stdout, "GOAX ENCRYPTED MESSAGE", nil)
	if err != nil {
		log.Fatal("Couldn't create armor encoder: ", err)
	}

	io.Copy(encoder, bytes.NewReader(cipherText))
	encoder.Close()
	fmt.Println("")
}

var errNoRatchet = errors.New("No ratchet")

var errInvalidRatchet = errors.New("Invalid ratchet")

func openRatchet(peer string) (r *goax.Ratchet, err error) {
	f, err := os.Open(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
	if err != nil {
		return nil, errNoRatchet
	}
	defer f.Close()

	var q goax.Ratchet
	armorDecoder, err := armor.Decode(f)
	if err != nil {
		return nil, errors.Wrap(err, "Error opening decoder")
	}
	err = json.NewDecoder(armorDecoder.Body).Decode(&q)
	if err != nil {
		return nil, errInvalidRatchet
	}

	return &q, nil
}

func createRatchet(peer string) (r *goax.Ratchet, err error) {
	myIdentityKeyPrivate := getPrivateKey()
	var asArray [32]byte
	copy(asArray[:], myIdentityKeyPrivate)
	r = goax.New(rand.Reader, asArray)
	err = saveRatchet(r, peer)
	return r, err
}

func saveRatchet(r *goax.Ratchet, peer string) error {
	os.MkdirAll("ratchets", 0755)
	f, err := os.Create(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
	if err != nil {
		return errors.Wrap(err, "Couldn't create ratchet file")
	}
	defer f.Close()

	armorEncoder, err := armor.Encode(f, "GOAX RATCHET", nil)
	if err != nil {
		return errors.Wrap(err, "Couldn't create armor encoder")
	}
	err = json.NewEncoder(armorEncoder).Encode(r)
	if err != nil {
		return errors.Wrap(err, "Couldn't marshall ratchet")
	}
	err = armorEncoder.Close()
	if err != nil {
{'fraction_non_alphanumeric': 0.15952980688497062, 'fraction_numerical': 0.0033585222502099076, 'mean_word_length': 4.19629225736096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2674846', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1830, 'n_words': 434}