mirror of
https://git.um-react.app/um/cli.git
synced 2025-11-28 03:33:02 +00:00
refactor: use io.Reader instead of custom method
This commit is contained in:
31
algo/kwm/kwm_cipher.go
Normal file
31
algo/kwm/kwm_cipher.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package kwm
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type kwmCipher struct {
|
||||
mask []byte
|
||||
}
|
||||
|
||||
func newKwmCipher(key []byte) *kwmCipher {
|
||||
return &kwmCipher{mask: generateMask(key)}
|
||||
}
|
||||
|
||||
func generateMask(key []byte) []byte {
|
||||
keyInt := binary.LittleEndian.Uint64(key)
|
||||
keyStr := strconv.FormatUint(keyInt, 10)
|
||||
keyStrTrim := padOrTruncate(keyStr, 32)
|
||||
mask := make([]byte, 32)
|
||||
for i := 0; i < 32; i++ {
|
||||
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
|
||||
}
|
||||
return mask
|
||||
}
|
||||
|
||||
func (c kwmCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := range buf {
|
||||
buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user