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0xE6, 0x3C, 0x44, 0x27, 0x51, 0x0D, 0x5F, 0xC7, 0x9C, 0xD6, 0x63, 0x67, 0xA5, 0x27, 0x97, 0x38, 0xFB, 0x2D, 0xD3, 0xD6, 0x60, 0x25, 0x83, 0x4D, 0x37, 0x5B, 0x40, 0x59, 0x11, 0x77, 0x51, 0x11, 0x14, 0x18, 0x07, 0x63, 0xB1, 0x34, 0x3D, 0xB8, 0x60, 0x13, 0xC2, 0xE8, 0x13, 0x82}
)

// Encrypt encrypts the given data using MHF's custom encryption+checksum method.
// if a overrideByteKey value is supplied (!= nil), it will be used to override the derived/truncated key byte.
func Encrypt(data []byte, key uint32, overrideByteKey *byte) (outputData []byte, combinedCheck uint16, check0 uint16, check1 uint16, check2 uint16) {
	return _generalCrypt(data, key, 0, overrideByteKey)
}

// Decrypt decrypts the given data using MHF's custom decryption+checksum method.
// if a overrideByteKey value is supplied (!= nil), it will be used to override the derived/truncated key byte.
func Decrypt(data []byte, key uint32, overrideByteKey *byte) (outputData []byte, combinedCheck uint16, check0 uint16, check1 uint16, check2 uint16) {
	return _generalCrypt(data, key, 1, overrideByteKey)
}

// _generalCrypt is a generalized MHF crypto function that can perform both encryption and decryption,
// these two crypto operations are combined into a single function because they shared most of their logic.
//		encrypt: cryptType==0
//		decrypt: cryptType==1
func _generalCrypt(data []byte, rotKey uint32, cryptType int, overrideByteKey *byte) ([]byte, uint16, uint16, uint16, uint16) {
	cryptKeyTruncByte := byte(((rotKey >> 1) % 999983) & 0xFF)
	if overrideByteKey != nil {
		cryptKeyTruncByte = *overrideByteKey
	}

	derivedCryptKey := int32((uint32(len(data)) * (uint32(cryptKeyTruncByte) + 1)) & 0xFFFFFFFF)
	sharedBufIdx := byte(1)
	accumulator0 := uint32(0)
	accumulator1 := uint32(0)
	accumulator2 := uint32(0)

	var outputData []byte
	if cryptType == 0 {
		for i := 0; i < len(data); i++ {
			// Do the encryption for this iteration
			encKeyIdx := int32(((uint32(derivedCryptKey) >> 10) ^ uint32(data[i])) & 0xFF)
			derivedCryptKey = (0x4FD * (derivedCryptKey + 1))
			encKeyByte := _encryptKey[encKeyIdx]

			// Update the checksum accumulators.
			accumulator2 = uint32((accumulator2 + (uint32(sharedBufIdx) * uint32(data[i]))) & 0xFFFFFFFF)
			accumulator1 = uint32((accumulator1 + uint32(encKeyIdx)) & 0xFFFFFFFF)
			accumulator0 = uint32((accumulator0 + (uint32(encKeyByte)<<(i&7))&0xFFFFFFFF) & 0xFFFFFFFF)

			// Append the output.
			outputData = append(outputData, _sharedCryptKey[sharedBufIdx]^encKeyByte)

			// Update the sharedBufIdx for the next iteration.
			sharedBufIdx = data[i]
		}

	} else if cryptType == 1 {
		for i := 0; i < len(data); i++ {
			// Do the decryption for this iteration
			oldSharedBufIdx := sharedBufIdx
			tIdx := data[i] ^ _sharedCryptKey[sharedBufIdx]
			decKeyByte := _decryptKey[tIdx]
			sharedBufIdx = byte(((uint32(derivedCryptKey) >> 10) ^ uint32(decKeyByte)) & 0xFF)

			// Update the checksum accumulators.
			accumulator0 = (accumulator0 + ((uint32(tIdx) << (i & 7)) & 0xFFFFFFFF))
			accumulator1 = (accumulator1 + uint32(decKeyByte)) & 0xFFFFFFFF
			accumulator2 = (accumulator2 + ((uint32(oldSharedBufIdx) * uint32(sharedBufIdx)) & 0xFFFFFFFF)) & 0xFFFFFFFF

			// Append the output.
			outputData = append(outputData, sharedBufIdx)

			// Update the key pos for next iteration.
			derivedCryptKey = (0x4FD * (derivedCryptKey + 1))
		}
	}

	combinedCheck := uint16((accumulator1 + (accumulator0 >> 1) + (accumulator2 >> 2)) & 0xFFFF)
	check0 := uint16((accumulator0 ^ ((accumulator0 & 0xFFFF0000) >> 16)) & 0xFFFF)
	check1 := uint16((accumulator1 ^ ((accumulator1 & 0xFFFF0000) >> 16)) & 0xFFFF)
	check2 := uint16((accumulator2 ^ ((accumulator2 & 0xFFFF0000) >> 16)) & 0xFFFF)

	return outputData, combinedCheck, check0, check1, check2
}
