-
Notifications
You must be signed in to change notification settings - Fork 0
/
enshamir_test.go
83 lines (70 loc) · 1.73 KB
/
enshamir_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package enshamir
import (
"strings"
"testing"
)
func TestEncryptSplit(t *testing.T) {
encryptionPassword := []byte("hello1234567890!@#$%^&*()_+~`")
secret := []byte("this is a secret `1234567890-=~!@#$%^&*()_+")
parts := 4
threshold := 3
hashedPassword, shares, err := EncryptSplit(encryptionPassword, secret, parts, threshold)
if err != nil {
t.Fatal(err)
}
// Verify if we can really combine and decrypt the shares.
validIndexes := [][]int{{
0, 1, 2, 3, // all parts
}, {
0, 1, 2,
}, {
0, 1, 3,
}, {
0, 2, 3,
}, {
1, 2, 3,
}}
for _, indexes := range validIndexes {
var s [][]byte
for _, i := range indexes {
s = append(s, shares[i])
}
if len(s) < threshold {
t.FailNow()
}
plaintext, err := CombineDecrypt(encryptionPassword, hashedPassword, s)
if err != nil {
t.Fatal(err)
}
if string(secret) != string(plaintext) {
t.FailNow()
}
}
invalidIndexes := [][]int{{0, 1}, {1, 2}, {0, 3}, {1, 3}}
for _, indexes := range invalidIndexes {
var s [][]byte
for _, i := range indexes {
s = append(s, shares[i])
}
// This case is ensure we can't restore our plaintext if the number of shares is not enough
if len(s) >= threshold {
t.FailNow()
}
// It's expected to fail to decrypt the shares since we don't have enough shares.
_, err := CombineDecrypt(encryptionPassword, hashedPassword, s)
if !strings.Contains(err.Error(), "unable to decrypt the secret") {
t.Fatal("unexpected err: " + err.Error())
}
}
}
func Test_randomBytes(t *testing.T) {
for length := 0; length <= 256; length++ {
b, err := randomBytes(uint32(length))
if err != nil {
t.Fatal(err)
}
if len(b) != length {
t.Fatalf("length does not match, expected: %d, actual: %d", length, len(b))
}
}
}