<commit_before>// Copyright 2012 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmac

import (
	"crypto/aes"
	"fmt"
	"math"
)

func padBlock(block []byte) []byte {
	blockLen := len(block)
	if blockLen == 16 {
		return block
	}

	result := make([]byte, 16)
	copy(result, block)
	result[blockLen] = 0x80

	return result
}

// Given a 128-bit key and a message, return a MAC that can be used to validate
// the input message. This is the AES-CMAC function of RFC 4493.
func generateCmac(key []byte, msg []byte) ([]byte, error) {
	if len(key) != 16 {
		return nil, fmt.Errorf("generateCmac requires a 16-byte key.")
	}

	msgLen := len(msg)

	// Generate subkeys.
	k1, k2 := generateSubkey(key)

	// Calculate the number of rounds.
	lastBlockComplete := false
	n := uint64(math.Ceil(float64(msgLen) / 16))
	if n == 0 {
		n = 1
	} else {
		if msgLen % 16 == 0 {
			lastBlockComplete = true
		}
	}

	// Calculate M_last.
	lastBlock := msg[msgLen - (msgLen / 16 * 16):]
	var mLast []byte
	if lastBlockComplete {
		mLast = xor(lastBlock, k1)
	} else {
		mLast = xor(padBlock(lastBlock), k2)
	}

	// Create a cipher.
	c, err := aes.NewCipher(key)
	if err != nil {
		return nil, fmt.Errorf("aes.NewCipher: %v", err)
	}

	// Run the rounds.
	x := make([]byte, 16)
	for i := uint64(0); i < n-1; i++ {
		block := msg[16*i:16*(i+1)]
		y := xor(x, block)
		c.Encrypt(x, y)
	}

	y := xor(mLast, x)
	result := make([]byte, 16)
	c.Encrypt(result, y)

	return result, nil
}
<commit_msg>Fixed a bug.<commit_after>// Copyright 2012 <NAME>. All Rights Reserved.
// Author: <EMAIL> (<NAME>)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmac

import (
	"crypto/aes"
	"fmt"
	"math"
)

func padBlock(block []byte) []byte {
	blockLen := len(block)
	if blockLen == 16 {
		return block
	}

	result := make([]byte, 16)
	copy(result, block)
	result[blockLen] = 0x80

	return result
}

// Given a 128-bit key and a message, return a MAC that can be used to validate
// the input message. This is the AES-CMAC function of RFC 4493.
func generateCmac(key []byte, msg []byte) ([]byte, error) {
	if len(key) != 16 {
		return nil, fmt.Errorf("generateCmac requires a 16-byte key.")
	}

	msgLen := len(msg)

	// Generate subkeys.
	k1, k2 := generateSubkey(key)

	// Calculate the number of rounds.
	lastBlockComplete := false
	n := uint64(math.Ceil(float64(msgLen) / 16))
	if n == 0 {
		n = 1
	} else {
		if msgLen % 16 == 0 {
			lastBlockComplete = true
		}
	}

	// Calculate M_last.
	lastBlock := msg[16*(n-1):]
	var mLast []byte
	if lastBlockComplete {
		mLast = xor(lastBlock, k1)
	} else {
		mLast = xor(padBlock(lastBlock), k2)
	}

	// Create a cipher.
	c, err := aes.NewCipher(key)
	if err != nil {
		return nil, fmt.Errorf("aes.NewCipher: %v", err)
	}

	// Run the rounds.
	x := make([]byte, 16)
	for i := uint64(0); i < n-1; i++ {
		block := msg[16*i:16*(i+1)]
		y := xor(x, block)
		c.Encrypt(x, y)
	}

	y := xor(mLast, x)
	result := make([]byte, 16)
	c.Encrypt(result, y)

	return result, nil
}

{'fraction_non_alphanumeric': 0.14313821938022628, 'fraction_numerical': 0.028283325135268076, 'mean_word_length': 3.2057911065149947, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5583486', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1421, 'n_words': 499}