1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

Re-implement Obscure/Reveal so they use AES-CTR encryption

This commit is contained in:
Nick Craig-Wood
2016-08-14 12:04:43 +01:00
parent ebb67c135e
commit 6a4e424630
9 changed files with 127 additions and 32 deletions

View File

@@ -1,6 +1,8 @@
package fs
import (
"bytes"
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
@@ -85,17 +87,48 @@ func TestSizeSuffixSet(t *testing.T) {
}
}
func TestReveal(t *testing.T) {
func TestObscure(t *testing.T) {
for _, test := range []struct {
in string
want string
iv string
}{
{"", ""},
{"2sTcyNrA", "potato"},
{"", "YWFhYWFhYWFhYWFhYWFhYQ", "aaaaaaaaaaaaaaaa"},
{"potato", "YWFhYWFhYWFhYWFhYWFhYXMaGgIlEQ", "aaaaaaaaaaaaaaaa"},
{"potato", "YmJiYmJiYmJiYmJiYmJiYp3gcEWbAw", "bbbbbbbbbbbbbbbb"},
} {
got := Reveal(test.in)
cryptRand = bytes.NewBufferString(test.iv)
got, err := Obscure(test.in)
cryptRand = rand.Reader
assert.NoError(t, err)
assert.Equal(t, test.want, got)
assert.Equal(t, test.in, Obscure(got), "not bidirectional")
recoveredIn, err := Reveal(got)
assert.NoError(t, err)
assert.Equal(t, test.in, recoveredIn, "not bidirectional")
// Now the Must variants
cryptRand = bytes.NewBufferString(test.iv)
got = MustObscure(test.in)
cryptRand = rand.Reader
assert.Equal(t, test.want, got)
recoveredIn = MustReveal(got)
assert.Equal(t, test.in, recoveredIn, "not bidirectional")
}
}
// Test some error cases
func TestReveal(t *testing.T) {
for _, test := range []struct {
in string
wantErr string
}{
{"YmJiYmJiYmJiYmJiYmJiYp*gcEWbAw", "base64 decode failed: illegal base64 data at input byte 22"},
{"aGVsbG8", "input too short"},
{"", "input too short"},
} {
gotString, gotErr := Reveal(test.in)
assert.Equal(t, "", gotString)
assert.Equal(t, test.wantErr, gotErr.Error())
}
}