simplify EntranceServer crypto

This commit is contained in:
wish
2023-12-31 19:12:33 +11:00
parent 1aa2e36087
commit 57bf1ca6e4

View File

@@ -12,45 +12,40 @@ var (
// CalcSum32 calculates the custom MHF "sum32" checksum of the given data. // CalcSum32 calculates the custom MHF "sum32" checksum of the given data.
func CalcSum32(data []byte) uint32 { func CalcSum32(data []byte) uint32 {
tableIdx0 := int(len(data) & 0xFF) tableIdx0 := (len(data) + 1) & 0xFF
tableIdx1 := int(data[len(data)>>1] & 0xFF) tableIdx1 := int((data[len(data)>>1] + 1) & 0xFF)
out := make([]byte, 4) out := make([]byte, 4)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
tableIdx0++ key := _sum32Table0[(tableIdx0+i)%7] ^ _sum32Table1[(tableIdx1+i)%9]
tableIdx1++ out[i&3] = (out[i&3] + (data[i] ^ key)) & 0xFF
tmp := byte((_sum32Table1[tableIdx1%9] ^ _sum32Table0[tableIdx0%7]) ^ data[i])
out[i&3] = (out[i&3] + tmp) & 0xFF
} }
return binary.BigEndian.Uint32(out) return binary.BigEndian.Uint32(out)
} }
func rotate(k *uint32) {
*k = uint32(((54323 * uint(*k)) + 1) & 0xFFFFFFFF)
}
// EncryptBin8 encrypts the given data using MHF's "binary8" encryption. // EncryptBin8 encrypts the given data using MHF's "binary8" encryption.
func EncryptBin8(data []byte, key byte) []byte { func EncryptBin8(data []byte, key byte) []byte {
curKey := uint32(((54323 * uint(key)) + 1) & 0xFFFFFFFF) _key := uint32(key)
var output []byte var output []byte
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
tmp := (_bin8Key[i&7] ^ byte((curKey>>13)&0xFF)) rotate(&_key)
tmp := _bin8Key[i&7] ^ byte((_key>>13)&0xFF)
output = append(output, data[i]^tmp) output = append(output, data[i]^tmp)
curKey = uint32(((54323 * uint(curKey)) + 1) & 0xFFFFFFFF)
} }
return output return output
} }
// DecryptBin8 decrypts the given MHF "binary8" data. // DecryptBin8 decrypts the given MHF "binary8" data.
func DecryptBin8(data []byte, key byte) []byte { func DecryptBin8(data []byte, key byte) []byte {
curKey := uint32(((54323 * uint(key)) + 1) & 0xFFFFFFFF) _key := uint32(key)
var output []byte var output []byte
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
tmp := (data[i] ^ byte((curKey>>13)&0xFF)) rotate(&_key)
tmp := data[i] ^ byte((_key>>13)&0xFF)
output = append(output, tmp^_bin8Key[i&7]) output = append(output, tmp^_bin8Key[i&7])
curKey = uint32(((54323 * uint(curKey)) + 1) & 0xFFFFFFFF)
} }
return output return output
} }