<commit_before>package golem

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha256"
)

// AES Cipher Modes
const (
	ModeCBC = 1 << iota
	ModeCFB
	ModeCTR
	ModeOFB
)

// AES Key Length Sizes
const (
	AES128 keySize = 16
	AES192 keySize = 24
	AES256 keySize = 32
)

var (
	blockModeFuncMap = map[int]map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
		ModeCBC: map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
			"encrypt": cipher.NewCBCEncrypter,
			"decrypt": cipher.NewCBCDecrypter,
		},
	}

	streamFuncMap = map[int]map[string]func(b cipher.Block, iv []byte) cipher.Stream{
		ModeCFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewCFBEncrypter,
			"decrypt": cipher.NewCFBDecrypter,
		},
		ModeCTR: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewCTR,
			"decrypt": cipher.NewCTR,
		},
		ModeOFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewOFB,
			"decrypt": cipher.NewOFB,
		},
	}
)

type blockModeEncryption struct {
	*aesCipher
	encrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
	decrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
}

type streamEncryption struct {
	*aesCipher
	encrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
	decrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
}

type aesCipher struct {
	block  cipher.Block
	keylen keySize
}

// keySize wraps int type to enforce certain AES key sizes
type keySize int

// NewAESCipher returns a new AES block cipher set to a specific cipher mode
func NewAESCipher(mode int) Cipher {
	switch mode {
	case ModeCBC:
		return &blockModeEncryption{
			&aesCipher{
				keylen: AES128,
			},
			blockModeFuncMap[mode]["encrypt"],
			blockModeFuncMap[mode]["decrypt"],
		}
	case ModeCFB, ModeCTR, ModeOFB:
		return &streamEncryption{
			&aesCipher{
				keylen: AES128,
			},
			streamFuncMap[mode]["encrypt"],
			streamFuncMap[mode]["decrypt"],
		}
	default:
		panic("unsupported cipher mode")
	}<commit_msg>changing mode type to be enforcable<commit_after>package golem

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha256"
)

// AES Cipher Modes
const (
	ModeCBC cipherMode = 1 << iota
	ModeCFB
	ModeCTR
	ModeOFB
)

// AES Key Length Sizes
const (
	AES128 keySize = 16
	AES192 keySize = 24
	AES256 keySize = 32
)

var (
	blockModeFuncMap = map[cipherMode]map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
		ModeCBC: map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
			"encrypt": cipher.NewCBCEncrypter,
			"decrypt": cipher.NewCBCDecrypter,
		},
	}

	streamFuncMap = map[cipherMode]map[string]func(b cipher.Block, iv []byte) cipher.Stream{
		ModeCFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewCFBEncrypter,
			"decrypt": cipher.NewCFBDecrypter,
		},
		ModeCTR: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewCTR,
			"decrypt": cipher.NewCTR,
		},
		ModeOFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
			"encrypt": cipher.NewOFB,
			"decrypt": cipher.NewOFB,
		},
	}
)

type blockModeEncryption struct {
	*aesCipher
	encrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
	decrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
}

type streamEncryption struct {
	*aesCipher
	encrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
	decrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
}

type aesCipher struct {
	block  cipher.Block
	keylen keySize
}

// keySize wraps int type to enforce certain AES key sizes
type keySize int

// cipherMode wraps int type to enforce AES mode
type cipherMode int

// NewAESCipher returns a new AES block cipher set to a specific cipher mode
func NewAESCipher(mode cipherMode) Cipher {
	switch mode {
	case ModeCBC:
		return &blockModeEncryption{
			&aesCipher{
				keylen: AES128,
			},
			blockModeFuncMap[mode]["encrypt"],
			blockModeFuncMap[mode]["decrypt"],
		}
	case ModeCFB, ModeCTR, ModeOFB:
		return &streamEncryption{
			&aesCipher{
				keylen: AES128,
			},
			streamFuncMap[mode]["encrypt"],
			streamFuncMap[mode]["decrypt"],
		}
	default:
		panic("unsupported cipher mode")
	}
{'fraction_non_alphanumeric': 0.12094117647058823, 'fraction_numerical': 0.011764705882352941, 'mean_word_length': 4.436061381074169, 'pattern_counts': {'":': 16, '<': 7, '<?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': '2150880', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1565, 'n_words': 377}