Adjustable saltLen

This commit is contained in:
Toby 2021-04-21 14:23:40 -07:00
parent f396cae604
commit 51abb06911

View File

@ -7,7 +7,9 @@ import (
"time"
)
// [salt(16)][obfuscated payload]
// [salt][obfuscated payload]
const saltLen = 16
type XPlusObfuscator struct {
Key []byte
@ -24,14 +26,14 @@ func NewXPlusObfuscator(key []byte) *XPlusObfuscator {
}
func (x *XPlusObfuscator) Deobfuscate(in []byte, out []byte) int {
pLen := len(in) - 16
pLen := len(in) - saltLen
if pLen <= 0 || len(out) < pLen {
// Invalid
return 0
}
key := sha256.Sum256(append(x.Key, in[:16]...))
key := sha256.Sum256(append(x.Key, in[:saltLen]...))
// Deobfuscate the payload
for i, c := range in[16:] {
for i, c := range in[saltLen:] {
out[i] = c ^ key[i%sha256.Size]
}
return pLen
@ -39,14 +41,14 @@ func (x *XPlusObfuscator) Deobfuscate(in []byte, out []byte) int {
func (x *XPlusObfuscator) Obfuscate(p []byte) []byte {
pLen := len(p)
buf := make([]byte, 16+pLen)
buf := make([]byte, saltLen+pLen)
x.lk.Lock()
_, _ = x.RandSrc.Read(buf[:16]) // salt
_, _ = x.RandSrc.Read(buf[:saltLen]) // salt
x.lk.Unlock()
// Obfuscate the payload
key := sha256.Sum256(append(x.Key, buf[:16]...))
key := sha256.Sum256(append(x.Key, buf[:saltLen]...))
for i, c := range p {
buf[i+16] = c ^ key[i%sha256.Size]
buf[i+saltLen] = c ^ key[i%sha256.Size]
}
return buf
}