<commit_before>package cred

import (
	"os"
	"encoding/json"
	"bytes"
	"encoding/base64"
	"strings"
	"io/ioutil"
	"golang.org/x/crypto/ssh"
	"path"
	"gopkg.in/yaml.v2"
)

var sshKeyFileCandidates = []string{"/.ssh/id_rsa", "/.ssh/id_dsa"}
var DefaultKey = []byte{<KEY>}
var PasswordCipher = GetDefaultPasswordCipher()

type Config struct {
	Username          string
	Password          string
	EncryptedPassword string
	PrivateKeyPath    string
	clientConfig      *ssh.ClientConfig
}

func (c *Config) Load(filename string) error {
	reader, err := os.Open(filename)
	if err != nil {
		return err
	}
	ext := path.Ext(filename)
	if strings.Contains(ext,"yaml")  || strings.Contains(ext,"yml")  {
		var data, err = ioutil.ReadAll(reader)
		if err != nil {
			return err
		}
		err =yaml.Unmarshal(data, c)
		if err != nil {
			return err
		}
	} else {
		err = json.NewDecoder(reader).Decode(c)
		if err != nil {
			return nil
		}
	}

	if c.EncryptedPassword != "" {
		decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(c.EncryptedPassword))
		data, err := ioutil.ReadAll(decoder)
		if err != nil {
			return err
		}
		c.Password = string(PasswordCipher.Decrypt(data))
	} else if c.Password != "" {
		c.encryptPassword(c.Password)
	}
	return nil
}

func (c *Config) Save(filename string) error {
	var password = c.Password
	defer func() { c.Password = password }()
	if password != "" {
		c.encryptPassword(password)
		c.Password = ""
	}

	_ = os.Remove(filename)
	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return err
	}
	defer file.Close()
	return json.NewEncoder(file).Encode(c)
}
func (c *Config) encryptPassword(password string) {

	encrypted := PasswordCipher.Encrypt([]byte(password))
	buf := new(bytes.Buffer)
	encoder := base64.NewEncoder(base64.StdEncoding, buf)
	defer encoder.Close()
	encoder.Write(encrypted)
	encoder.Close()
	c.EncryptedPassword = string(buf.Bytes())
}

func (c *Config) applyDefaultIfNeeded() {
	if c.Username == "" {
		c.Username = os.Getenv("USER")
	}
	if c.PrivateKeyPath == "" && c.Password == "" {
		homeDirectory := os.Getenv("HOME")
		if homeDirectory != "" {
			for _, candidate := range sshKeyFileCandidates {
				filename := path.Join(homeDirectory, candidate)
				file, err := os.Open(filename)
				if err == nil {
					file.Close()
					c.PrivateKeyPath = filename
					break
				}
			}
		}
	}
}

//ClientConfig returns a new instance of sshClientConfig
func (c *Config) ClientConfig() (*ssh.ClientConfig, error) {
	if c.clientConfig != nil {
		return c.clientConfig, nil
	}
	c.applyDefaultIfNeeded()
	result := &ssh.ClientConfig{
		User:            c.Username,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Auth:            make([]ssh.AuthMethod, 0),
	}


	if c.Password != "" {
		result.Auth = append(result.Auth, ssh.Password(c.Password))
	}
	if c.PrivateKeyPath != "" {
		privateKeyBytes, err := ioutil.ReadFile(c.PrivateKeyPath)
		if err != nil {
			return nil, err
		}

		if len(c.clientConfig.Ciphers) == 0 {
			c.clientConfig.Ciphers = make([]string, 0)
		}
		c.clientConfig.Ciphers=append(c.clientConfig.Ciphers, "aes128-cbc")

		key, err := ssh.ParsePrivateKey(privateKeyBytes)
		if err != nil {
			return nil, err
		}
		result.Auth = append(result.Auth, ssh.PublicKeys(key))

	}
	c.clientConfig = result
	return result, nil
}

func NewConfig(filename string) (*Config, error) {
	var config = &Config{}
	err := config.Load(filename)
	if err != nil {
		return nil, err
	}
	config.applyDefaultIfNeeded()
	return config, nil
}

func GetDefaultPasswordCipher() Cipher {
	var result, err = NewBlowfishCipher(DefaultKey)
	if err != nil {
		return nil
	}<commit_msg>added eas private key<commit_after>package cred

import (
	"os"
	"encoding/json"
	"bytes"
	"encoding/base64"
	"strings"
	"io/ioutil"
	"golang.org/x/crypto/ssh"
	"path"
	"gopkg.in/yaml.v2"
)

var sshKeyFileCandidates = []string{"/.ssh/id_rsa", "/.ssh/id_dsa"}
var DefaultKey = []byte{<KEY>}
var PasswordCipher = GetDefaultPasswordCipher()

type Config struct {
	Username          string
	Password          string
	EncryptedPassword string
	PrivateKeyPath    string
	clientConfig      *ssh.ClientConfig
}

func (c *Config) Load(filename string) error {
	reader, err := os.Open(filename)
	if err != nil {
		return err
	}
	ext := path.Ext(filename)
	if strings.Contains(ext,"yaml")  || strings.Contains(ext,"yml")  {
		var data, err = ioutil.ReadAll(reader)
		if err != nil {
			return err
		}
		err =yaml.Unmarshal(data, c)
		if err != nil {
			return err
		}
	} else {
		err = json.NewDecoder(reader).Decode(c)
		if err != nil {
			return nil
		}
	}

	if c.EncryptedPassword != "" {
		decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(c.EncryptedPassword))
		data, err := ioutil.ReadAll(decoder)
		if err != nil {
			return err
		}
		c.Password = string(PasswordCipher.Decrypt(data))
	} else if c.Password != "" {
		c.encryptPassword(c.Password)
	}
	return nil
}

func (c *Config) Save(filename string) error {
	var password = c.Password
	defer func() { c.Password = password }()
	if password != "" {
		c.encryptPassword(password)
		c.Password = ""
	}

	_ = os.Remove(filename)
	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return err
	}
	defer file.Close()
	return json.NewEncoder(file).Encode(c)
}
func (c *Config) encryptPassword(password string) {

	encrypted := PasswordCipher.Encrypt([]byte(password))
	buf := new(bytes.Buffer)
	encoder := base64.NewEncoder(base64.StdEncoding, buf)
	defer encoder.Close()
	encoder.Write(encrypted)
	encoder.Close()
	c.EncryptedPassword = string(buf.Bytes())
}

func (c *Config) applyDefaultIfNeeded() {
	if c.Username == "" {
		c.Username = os.Getenv("USER")
	}
	if c.PrivateKeyPath == "" && c.Password == "" {
		homeDirectory := os.Getenv("HOME")
		if homeDirectory != "" {
			for _, candidate := range sshKeyFileCandidates {
				filename := path.Join(homeDirectory, candidate)
				file, err := os.Open(filename)
				if err == nil {
					file.Close()
					c.PrivateKeyPath = filename
					break
				}
			}
		}
	}
}

//ClientConfig returns a new instance of sshClientConfig
func (c *Config) ClientConfig() (*ssh.ClientConfig, error) {
	if c.clientConfig != nil {
		return c.clientConfig, nil
	}
	c.applyDefaultIfNeeded()
	result := &ssh.ClientConfig{
		User:            c.Username,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Auth:            make([]ssh.AuthMethod, 0),
	}


	if c.Password != "" {
		result.Auth = append(result.Auth, ssh.Password(c.Password))
	}
	if c.PrivateKeyPath != "" {
		privateKeyBytes, err := ioutil.ReadFile(c.PrivateKeyPath)
		if err != nil {
			return nil, err
		}

		key, err := ssh.ParsePrivateKey(privateKeyBytes)
		if err != nil {
			return nil, err
		}
		result.Auth = append(result.Auth, ssh.PublicKeys(key))

	}
	c.clientConfig = result
	if len(c.clientConfig.Ciphers) == 0 {
		c.clientConfig.Ciphers = make([]string, 0)
	}
	c.clientConfig.Ciphers=append(c.clientConfig.Ciphers, "aes128-cbc")


	return result, nil
}

func NewConfig(filename string) (*Config, error) {
	var config = &Config{}
	err := config.Load(filename)
	if err != nil {
		return nil, err
	}
	config.applyDefaultIfNeeded()
	return config, nil
}

func GetDefaultPasswordCipher() Cipher {
	var result, err = NewBlowfishCipher(DefaultKey)
	if err != nil {
		return nil
	}
{'fraction_non_alphanumeric': 0.14256508109581573, 'fraction_numerical': 0.005724410522011721, 'mean_word_length': 3.623818525519849, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '175980', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2588, 'n_words': 571}