github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/obfuscate.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/base64"
     5  	"errors"
     6  )
     7  
     8  func deobfuscate(in string, key []byte) ([]byte, error) {
     9  	if len(key) == 0 {
    10  		return nil, errors.New("key cannot be zero length")
    11  	}
    12  
    13  	decoded, err := base64.StdEncoding.DecodeString(in)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  
    18  	out := make([]byte, len(decoded))
    19  	for i, c := range decoded {
    20  		out[i] = c ^ key[i%len(key)]
    21  	}
    22  
    23  	return out, nil
    24  }
    25  
    26  func obfuscate(in, key []byte) (string, error) {
    27  	if len(key) == 0 {
    28  		return "", errors.New("key cannot be zero length")
    29  	}
    30  
    31  	out := make([]byte, len(in))
    32  	for i, c := range in {
    33  		out[i] = c ^ key[i%len(key)]
    34  	}
    35  
    36  	return base64.StdEncoding.EncodeToString(out), nil
    37  }