-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathkey_test.go
61 lines (52 loc) · 1.59 KB
/
key_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
package encryption_test
import (
"crypto/aes"
"crypto/sha256"
"fmt"
"code.cloudfoundry.org/bbs/encryption"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Key", func() {
Describe("NewKey", func() {
It("generates a 256 bit key from a string that can be used as aes keys", func() {
phrases := []string{
"",
"a",
"a short phrase",
"12345678901234567890123456789012",
"1234567890123456789012345678901234567890123456789012345678901234567890",
}
for i, phrase := range phrases {
label := fmt.Sprintf("%d", i)
key, err := encryption.NewKey(label, phrase)
Expect(err).NotTo(HaveOccurred())
Expect(key.Label()).To(Equal(label))
Expect(key.Block().BlockSize()).To(Equal(aes.BlockSize))
phraseHash := sha256.Sum256([]byte(phrase))
block, err := aes.NewCipher(phraseHash[:])
Expect(err).NotTo(HaveOccurred())
Expect(key.Block()).To(Equal(block))
}
})
Context("when a key label is not specified", func() {
It("returns a meaningful error", func() {
_, err := encryption.NewKey("", "phrase")
Expect(err).To(MatchError("A key label is required"))
})
})
Context("when a key label is longer than 127 bytes", func() {
It("returns a meaningful error", func() {
var label string
for i := 0; i < 127; i++ {
label = fmt.Sprintf("%s%d", label, i%10)
}
_, err := encryption.NewKey(label, "phrase")
Expect(err).NotTo(HaveOccurred())
label = label + "0"
_, err = encryption.NewKey(label, "phrase")
Expect(err).To(MatchError("Key label is longer than 127 bytes"))
})
})
})
})