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:
42
algo/ncm/ncm_cipher.go
Normal file
42
algo/ncm/ncm_cipher.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package ncm
|
||||
|
||||
type ncmCipher struct {
|
||||
key []byte
|
||||
box []byte
|
||||
}
|
||||
|
||||
func newNcmCipher(key []byte) *ncmCipher {
|
||||
return &ncmCipher{
|
||||
key: key,
|
||||
box: buildKeyBox(key),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ncmCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := 0; i < len(buf); i++ {
|
||||
buf[i] ^= c.box[(i+offset)&0xff]
|
||||
}
|
||||
}
|
||||
|
||||
func buildKeyBox(key []byte) []byte {
|
||||
box := make([]byte, 256)
|
||||
for i := 0; i < 256; i++ {
|
||||
box[i] = byte(i)
|
||||
}
|
||||
|
||||
var j byte
|
||||
for i := 0; i < 256; i++ {
|
||||
j = box[i] + j + key[i%len(key)]
|
||||
box[i], box[j] = box[j], box[i]
|
||||
}
|
||||
|
||||
ret := make([]byte, 256)
|
||||
var _i byte
|
||||
for i := 0; i < 256; i++ {
|
||||
_i = byte(i + 1)
|
||||
si := box[_i]
|
||||
sj := box[_i+si]
|
||||
ret[i] = box[si+sj]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
Reference in New Issue
Block a user