obfuscate

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const Replacement = "<HIDDEN>"

Replacement is the standard replacement used during obfuscation.

Variables

This section is empty.

Functions

func JoinCommand

func JoinCommand(command string, arguments ...string) (s string)

func NewCommandMatch

func NewCommandMatch(command string, arguments ...string) *regexp.Regexp

func NewExtensionMatch

func NewExtensionMatch(ext string) *regexp.Regexp

func NewRegexpKeyValue

func NewRegexpKeyValue(key string) *regexp.Regexp

NewRegexpKeyValue builds an regexp.Regexp to match multiple key-value kinds.

Should work with INI files and Icinga 2 config.

func ReplacePattern

func ReplacePattern(line string, pattern *regexp.Regexp) (s string, count uint)

ReplacePattern replaces all matches in a line.

func ReplacePatterns

func ReplacePatterns(line string, patterns []*regexp.Regexp) (s string, count uint)

ReplacePatterns replaces all the patterns matches in a line.

Types

type Kind

type Kind uint8

Kind is used by Obfuscator to identify the kind of content to obfuscate.

const (
	// KindAny allows to obfuscate content in any resource.
	KindAny Kind = iota
	// KindFile declares a file resource.
	KindFile
	// KindOutput declares an output resource, e.g. from an command.
	KindOutput
)

type Obfuscator

type Obfuscator struct {
	Kind
	Affecting    []*regexp.Regexp
	Replacements []*regexp.Regexp
	Files        uint
	Replaced     uint
}

Obfuscator provides the basic functionality of an obfuscation engine.

Kind filters the variant of resource we want to work on, while Affecting defines a list of regexp.Regexp, to match against for the file names, or command.

Replacements will be iterated, so all matches or matched groups will be replaced.

Example
o := New(KindFile, regexp.MustCompile(`\.ini$`), regexp.MustCompile(`password\s*=\s*(.*)`))

content := []byte(`password = "secret"`)

if o.IsAccepting(KindFile, "test.ini") {
	count, data, err := o.Process(content)
	fmt.Println(err)
	fmt.Println(count)
	fmt.Println(string(data))
}
Output:

<nil>
1
password = <HIDDEN>

func New

func New(kind Kind, affects, replace *regexp.Regexp) *Obfuscator

New returns a basic Obfuscator with provided regexp.Regexp.

func NewAny

func NewAny(replace string) *Obfuscator

NewAny returns an Obfuscator that can be used to replace any input.

func NewFile

func NewFile(replace, ext string) *Obfuscator

NewFile returns an Obfuscator and will initialize regexp.Regexp based on extension and a string for replacement.

func NewOutput

func NewOutput(replace, command string, arguments ...string) *Obfuscator

NewOutput returns an Obfuscator and will initialize regexp.Regexp based on command and replacement.

func (Obfuscator) IsAccepting

func (o Obfuscator) IsAccepting(kind Kind, name string) bool

IsAccepting checks if we want to work on the resource.

func (*Obfuscator) Process

func (o *Obfuscator) Process(data []byte) (uint, []byte, error)

Process takes data and returns it obfuscated.

func (*Obfuscator) ProcessReader

func (o *Obfuscator) ProcessReader(r io.Reader) (count uint, out bytes.Buffer, err error)

ProcessReader takes an io.Reader and returns a new one obfuscated.

func (*Obfuscator) WithAffecting

func (o *Obfuscator) WithAffecting(a *regexp.Regexp) *Obfuscator

WithAffecting adds a new element to the list.

func (*Obfuscator) WithReplacement

func (o *Obfuscator) WithReplacement(r *regexp.Regexp) *Obfuscator

WithReplacement adds a new element to the list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL