Browse Source

Preserve whitespace

master
maze 11 months ago
parent
commit
6bff1b982d
4 changed files with 53 additions and 21 deletions
  1. +24
    -6
      entropy.go
  2. +26
    -14
      entropy_test.go
  3. +2
    -0
      password.go
  4. +1
    -1
      scrub.go

+ 24
- 6
entropy.go View File

@ -20,8 +20,8 @@ const (
// EntropyScrubber splits the input by Whitespace and matches each part's entropy with the configured threshold.
type EntropyScrubber struct {
// Whitespace characters.
Whitespace string
// Whitespace runes.
Whitespace []rune
// Threshold for scrubbing. If not set the ideal entropy is calculated based on the length of the input string.
Threshold float64
@ -38,8 +38,8 @@ func (es EntropyScrubber) Scrub(s string) string {
threshold = es.Threshold
correction = es.Correction
)
if whitespace == "" {
whitespace = DefaultWhitespace
if len(whitespace) == 0 {
whitespace = []rune(DefaultWhitespace)
}
if threshold == 0 {
if correction == 0 {
@ -51,7 +51,7 @@ func (es EntropyScrubber) Scrub(s string) string {
f := splitAfter(s, []rune(whitespace))
for i, p := range f {
if e := entropy(p); e >= threshold {
f[i] = Replacement
f[i] = replaceNoWhitespace(f[i], Replacement, whitespace)
}
}
return strings.Join(f, "")
@ -76,7 +76,7 @@ func EntropyWithThreshold(s string, threshold float64) string {
f := strings.Fields(s)
for i, p := range f {
if entropy(strings.TrimSpace(p)) >= threshold {
f[i] = Replacement
f[i] = replaceNoWhitespace(f[i], Replacement, []rune(DefaultWhitespace))
}
}
return strings.Join(f, " ")
@ -131,3 +131,21 @@ func splitAfter(s string, separators []rune) (out []string) {
}
return
}
func replaceNoWhitespace(s, r string, whitespace []rune) string {
var (
out = make([]rune, 0, len(s))
j int
t = []rune(r)
l = len(t)
)
for _, c := range s {
if strings.ContainsRune(string(whitespace), c) {
out = append(out, c)
} else if j < l {
out = append(out, t[j])
j++
}
}
return string(out)
}

+ 26
- 14
entropy_test.go View File

@ -2,20 +2,21 @@ package scrub
import "testing"
var testEntropy = []struct {
Test, Want string
}{
{"", ""},
{"1223334444", "1223334444"},
{"password123", Replacement},
{"Tr0ub4dour&3", Replacement},
{"test correcthorsebatterystaple", "test correcthorsebatterystaple"},
{"test coRrecth0rseba++ery9.23.2007staple$", "test " + Replacement},
{"rWibMFACxAUGZmxhVncy test", Replacement + " test"},
{"test Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$ test", "test " + Replacement + " test"},
}
func TestEntropy(t *testing.T) {
tests := []struct {
Test, Want string
}{
{"", ""},
{"1223334444", "1223334444"},
{"password123", Replacement},
{"Tr0ub4dour&3", Replacement},
{"test correcthorsebatterystaple", "test correcthorsebatterystaple"},
{"test coRrecth0rseba++ery9.23.2007staple$", "test " + Replacement},
{"rWibMFACxAUGZmxhVncy test", Replacement + " test"},
{"test Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$ test", "test " + Replacement + " test"},
}
for _, test := range tests {
for _, test := range testEntropy {
t.Run(test.Test, func(it *testing.T) {
if v := Entropy(test.Test); v != test.Want {
it.Errorf("expected %q to return %q, got %q", test.Test, test.Want, v)
@ -24,9 +25,20 @@ func TestEntropy(t *testing.T) {
}
}
func TestEntropyScrubber_Scrub(t *testing.T) {
var scrubber EntropyScrubber // using defaults
for _, test := range testEntropy {
t.Run(test.Test, func(it *testing.T) {
if v := scrubber.Scrub(test.Test); v != test.Want {
it.Errorf("expected %q to return %q, got %q", test.Test, test.Want, v)
}
})
}
}
func TestEntropyScrubber(t *testing.T) {
scrubber := EntropyScrubber{
Whitespace: DefaultWhitespace,
Whitespace: []rune(DefaultWhitespace),
Threshold: DefaultEntropyThreshold,
}
tests := []struct {


+ 2
- 0
password.go View File

@ -8,6 +8,8 @@ const (
reCryptHash = `(?s)\$[-0-9a-z]+\$((?:rounds=\d+\$)?(?:` + reBase64 + `+)+\$?)`
)
// Good source of examples can be found here: https://hashcat.net/wiki/doku.php?id=example_hashes
var (
// CryptHash can scrub hashes in common crypt formats, such as Apache and IANA crypt hashes.
CryptHash Scrubber = regexpScrubber{


+ 1
- 1
scrub.go View File

@ -45,7 +45,7 @@ var All = Scrubbers{
PEMDHParameters,
PEMPrivateKey,
EntropyScrubber{
Whitespace: DefaultWhitespace,
Whitespace: []rune(DefaultWhitespace),
Threshold: DefaultEntropyThreshold,
},
}


Loading…
Cancel
Save