/
key.go
65 lines (59 loc) 路 1.33 KB
/
key.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"io/ioutil"
"github.com/aegistudio/shaft"
)
var aes256File string
func init() {
rootCmd.PersistentFlags().StringVar(
&aes256File, "aes256", aes256File,
"read content as AES key from a file")
options = append(options, shaft.Provide(
func() ([]cipher.AEAD, error) {
if aes256File != "" {
data, err := ioutil.ReadFile(aes256File)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(data)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return []cipher.AEAD{aead}, nil
}
return nil, nil
}))
}
var aes256Sha256File string
func init() {
rootCmd.PersistentFlags().StringVar(
&aes256Sha256File, "aes256-sha256", aes256Sha256File,
"digest content as AES key from a file")
options = append(options, shaft.Provide(
func() ([]cipher.AEAD, error) {
if aes256Sha256File != "" {
data, err := ioutil.ReadFile(aes256Sha256File)
if err != nil {
return nil, err
}
digest := sha256.Sum256(data)
block, err := aes.NewCipher(digest[:])
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return []cipher.AEAD{aead}, nil
}
return nil, nil
}))
}