Skip to content

Commit

Permalink
simplify EntranceServer crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
sekaiwish committed Dec 31, 2023
1 parent 1aa2e36 commit 57bf1ca
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions server/entranceserver/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,40 @@ var (

// CalcSum32 calculates the custom MHF "sum32" checksum of the given data.
func CalcSum32(data []byte) uint32 {
tableIdx0 := int(len(data) & 0xFF)
tableIdx1 := int(data[len(data)>>1] & 0xFF)

tableIdx0 := (len(data) + 1) & 0xFF
tableIdx1 := int((data[len(data)>>1] + 1) & 0xFF)
out := make([]byte, 4)
for i := 0; i < len(data); i++ {
tableIdx0++
tableIdx1++

tmp := byte((_sum32Table1[tableIdx1%9] ^ _sum32Table0[tableIdx0%7]) ^ data[i])
out[i&3] = (out[i&3] + tmp) & 0xFF
key := _sum32Table0[(tableIdx0+i)%7] ^ _sum32Table1[(tableIdx1+i)%9]
out[i&3] = (out[i&3] + (data[i] ^ key)) & 0xFF
}

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.
func EncryptBin8(data []byte, key byte) []byte {
curKey := uint32(((54323 * uint(key)) + 1) & 0xFFFFFFFF)

_key := uint32(key)
var output []byte
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)
curKey = uint32(((54323 * uint(curKey)) + 1) & 0xFFFFFFFF)
}

return output
}

// DecryptBin8 decrypts the given MHF "binary8" data.
func DecryptBin8(data []byte, key byte) []byte {
curKey := uint32(((54323 * uint(key)) + 1) & 0xFFFFFFFF)

_key := uint32(key)
var output []byte
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])
curKey = uint32(((54323 * uint(curKey)) + 1) & 0xFFFFFFFF)
}

return output
}

0 comments on commit 57bf1ca

Please sign in to comment.