summaryrefslogtreecommitdiff
path: root/vendor/github.com/getlantern/hidden
diff options
context:
space:
mode:
authorKali Kaneko (leap communications) <kali@leap.se>2019-01-12 18:39:45 +0100
committerRuben Pollan <meskio@sindominio.net>2019-01-17 12:30:32 +0100
commitb1247d2d0d51108c910a73891ff3116e5f032ab1 (patch)
treee9948964f0bfb1ad2df3bc7bad02aa1f41ccfbd8 /vendor/github.com/getlantern/hidden
parentefcb8312e31b5c2261b1a1e95ace55b322cfcc27 (diff)
[pkg] all your deps are vendored to us
Diffstat (limited to 'vendor/github.com/getlantern/hidden')
-rw-r--r--vendor/github.com/getlantern/hidden/hidden.go66
-rw-r--r--vendor/github.com/getlantern/hidden/hidden_test.go33
2 files changed, 99 insertions, 0 deletions
diff --git a/vendor/github.com/getlantern/hidden/hidden.go b/vendor/github.com/getlantern/hidden/hidden.go
new file mode 100644
index 0000000..0c52b08
--- /dev/null
+++ b/vendor/github.com/getlantern/hidden/hidden.go
@@ -0,0 +1,66 @@
+// Package hidden provides the ability to "hide" binary data in a string using
+// a hex encoding with non-printing characters. Hidden data is demarcated with
+// a leading and trailing NUL character.
+package hidden
+
+import (
+ "bytes"
+ "fmt"
+ "regexp"
+
+ "github.com/getlantern/hex"
+)
+
+// 16 non-printing characters
+const hextable = "\x01\x02\x03\x04\x05\x06\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17"
+
+var (
+ hexencoding = hex.NewEncoding(hextable)
+
+ re *regexp.Regexp
+)
+
+func init() {
+ var err error
+ re, err = regexp.Compile(fmt.Sprintf("\x00[%v]+\x00", hextable))
+ if err != nil {
+ panic(err)
+ }
+}
+
+// ToString encodes the given data as a hidden string, including leadnig and
+// trailing NULs.
+func ToString(data []byte) string {
+ buf := bytes.NewBuffer(make([]byte, 0, 2+hex.EncodedLen(len(data))))
+ // Leading NUL
+ buf.WriteByte(0)
+ buf.WriteString(hexencoding.EncodeToString(data))
+ // Trailing NUL
+ buf.WriteByte(0)
+ return buf.String()
+}
+
+// FromString extracts the hidden data from a string, which is expected to
+// contain leading and trailing NULs.
+func FromString(str string) ([]byte, error) {
+ return hexencoding.DecodeString(str[1 : len(str)-1])
+}
+
+// Extract extracts all hidden data from an arbitrary string.
+func Extract(str string) ([][]byte, error) {
+ m := re.FindAllString(str, -1)
+ result := make([][]byte, 0, len(m))
+ for _, s := range m {
+ b, err := FromString(s)
+ if err != nil {
+ return nil, err
+ }
+ result = append(result, b)
+ }
+ return result, nil
+}
+
+// Clean removes any hidden data from an arbitrary string.
+func Clean(str string) string {
+ return re.ReplaceAllString(str, "")
+}
diff --git a/vendor/github.com/getlantern/hidden/hidden_test.go b/vendor/github.com/getlantern/hidden/hidden_test.go
new file mode 100644
index 0000000..420ce64
--- /dev/null
+++ b/vendor/github.com/getlantern/hidden/hidden_test.go
@@ -0,0 +1,33 @@
+package hidden
+
+import (
+ "encoding/binary"
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestRoundTrip(t *testing.T) {
+ str := "H"
+ encoded := ToString([]byte(str))
+ rt, err := FromString(encoded)
+ if assert.NoError(t, err) {
+ assert.Equal(t, str, string(rt))
+ }
+}
+
+func TestExtract(t *testing.T) {
+ a := []byte("Here is my string")
+ b := make([]byte, 8)
+ binary.BigEndian.PutUint64(b, 56)
+ str := fmt.Sprintf("hidden%s data%s is fun", ToString(a), ToString(b))
+ t.Log(str)
+ out, err := Extract(str)
+ if assert.NoError(t, err) {
+ if assert.Len(t, out, 2) {
+ assert.Equal(t, out, [][]byte{a, b})
+ }
+ }
+ assert.Equal(t, "hidden data is fun", Clean(str))
+}