<commit_before>// +build !windows

// Package libcrypto implements okapi interfaces using OpenSSL's libcrypto library.
package libcrypto

// #cgo LDFLAGS:  -L/usr/local/opt/openssl/lib -lcrypto
// #cgo CFLAGS: -I/usr/local/opt/openssl/include
// #include <openssl/evp.h>
import "C"
import (
	"github.com/mkobetic/okapi"
)

func init() {
	okapi.RC4 = RC4
	okapi.BF_ECB = BF_ECB
	okapi.BF_CBC = BF_CBC
	okapi.BF_CFB = BF_CFB
	okapi.BF_OFB = BF_OFB
	okapi.DES3_ECB = DES3_ECB
	okapi.DES3_CBC = DES3_CBC
	okapi.DES3_CFB = DES3_CFB
	okapi.DES3_OFB = DES3_OFB
}

type Cipher struct {
	ctx    *C.EVP_CIPHER_CTX
	cipher *C.EVP_CIPHER // libcrypto constant
}

func RC4(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_rc4(), key, iv, encrypt)
}

func BF_ECB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_ecb(), key, iv, encrypt)
}

func BF_CBC(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_cbc(), key, iv, encrypt)
}

func BF_CFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_cfb(), key, iv, encrypt)
}

func BF_OFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_ofb(), key, iv, encrypt)
}

func DES3_ECB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_ecb(), key, iv, encrypt)
}

func DES3_CBC(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_cbc(), key, iv, encrypt)
}

func DES3_CFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_cfb(), key, iv, encrypt)
}

func DES3_OFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_ofb(), key, iv, encrypt)
}

func NewCipher(algorithm *C.EVP_CIPHER, key, iv []byte, encrypt bool) okapi.Cipher {
	c := &Cipher{cipher: algorithm}
	c.ctx = new(C.EVP_CIPHER_CTX)
	C.EVP_CIPHER_CTX_init(c.ctx)

	var ivp *C.uchar
	if iv != nil {
		ivp = (*C.uchar)(&iv[0])
	}
	var enc C.int = 0
	if encrypt {
		enc = 1
	}
	check(C.EVP_CipherInit_ex(c.ctx, algorithm, nil, nil, nil, enc))
	C.EVP_CIPHER_CTX_set_key_length(c.ctx, len(key))
	C.EVP_CIPHER_CTX_set_padding(c.ctx, 0)
	check(C.EVP_CipherInit_ex(c.ctx, nil, nil, (*C.uchar)(&key[0]), ivp, -1))
	return c
}

func (c *Cipher) KeySize() int {
	return int(C.EVP_CIPHER_CTX_key_length(c.cipher))
}

func (c *Cipher) BlockSize() int {
	return int(C.EVP_CIPHER_CTX_block_size(c.cipher))
}

func (c *Cipher) Update(in, out []byte) int {
	var outl C.int
	check(C.EVP_CipherUpdate(c.ctx, (*C.uchar)(&out[0]), &outl, (*C.uchar)(&in[0]), C.int(len(in))))
	return int(outl)
}

func (c *Cipher) Close() {
	if c.ctx == nil {
		return
	}
	defer func() {
		c.ctx = nil
	}()
	C.EVP_CIPHER_CTX_cleanup(c.ctx)
}
<commit_msg>fixed AES test<commit_after>// +build !windows

// Package libcrypto implements okapi interfaces using OpenSSL's libcrypto library.
package libcrypto

// #cgo LDFLAGS:  -L/usr/local/opt/openssl/lib -lcrypto
// #cgo CFLAGS: -I/usr/local/opt/openssl/include
// #include <openssl/evp.h>
import "C"
import (
	"github.com/mkobetic/okapi"
)

func init() {
	okapi.RC4 = RC4
	okapi.BF_ECB = BF_ECB
	okapi.BF_CBC = BF_CBC
	okapi.BF_CFB = BF_CFB
	okapi.BF_OFB = BF_OFB
	okapi.DES3_ECB = DES3_ECB
	okapi.DES3_CBC = DES3_CBC
	okapi.DES3_CFB = DES3_CFB
	okapi.DES3_OFB = DES3_OFB
}

type Cipher struct {
	ctx    *C.EVP_CIPHER_CTX
	cipher *C.EVP_CIPHER // libcrypto constant
}

func RC4(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_rc4(), key, iv, encrypt)
}

func BF_ECB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_ecb(), key, iv, encrypt)
}

func BF_CBC(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_cbc(), key, iv, encrypt)
}

func BF_CFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_cfb(), key, iv, encrypt)
}

func BF_OFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_bf_ofb(), key, iv, encrypt)
}

func DES3_ECB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_ecb(), key, iv, encrypt)
}

func DES3_CBC(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_cbc(), key, iv, encrypt)
}

func DES3_CFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_cfb(), key, iv, encrypt)
}

func DES3_OFB(key, iv []byte, encrypt bool) okapi.Cipher {
	return NewCipher(C.EVP_des_ede3_ofb(), key, iv, encrypt)
}

func NewCipher(algorithm *C.EVP_CIPHER, key, iv []byte, encrypt bool) okapi.Cipher {
	c := &Cipher{cipher: algorithm}
	c.ctx = new(C.EVP_CIPHER_CTX)
	C.EVP_CIPHER_CTX_init(c.ctx)

	var ivp *C.uchar
	if iv != nil {
		ivp = (*C.uchar)(&iv[0])
	}
	var enc C.int = 0
	if encrypt {
		enc = 1
	}
	check(C.EVP_CipherInit_ex(c.ctx, algorithm, nil, nil, nil, enc))
	C.EVP_CIPHER_CTX_set_key_length(c.ctx, C.int(len(key)))
	C.EVP_CIPHER_CTX_set_padding(c.ctx, 0)
	check(C.EVP_CipherInit_ex(c.ctx, nil, nil, (*C.uchar)(&key[0]), ivp, -1))
	return c
}

func (c *Cipher) KeySize() int {
	return int(C.EVP_CIPHER_CTX_key_length(c.ctx))
}

func (c *Cipher) BlockSize() int {
	return int(C.EVP_CIPHER_CTX_block_size(c.ctx))
}

func (c *Cipher) Update(in, out []byte) int {
	var outl C.int
	check(C.EVP_CipherUpdate(c.ctx, (*C.uchar)(&out[0]), &outl, (*C.uchar)(&in[0]), C.int(len(in))))
	return int(outl)
}

func (c *Cipher) Close() {
	if c.ctx == nil {
		return
	}
	defer func() {
		c.ctx = nil
	}()
	C.EVP_CIPHER_CTX_cleanup(c.ctx)
}

{'fraction_non_alphanumeric': 0.15513433934486567, 'fraction_numerical': 0.010305483989694516, 'mean_word_length': 4.5233739837398375, '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': '5961194', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2460, 'n_words': 516}