-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.go
More file actions
66 lines (53 loc) · 1.25 KB
/
cipher.go
File metadata and controls
66 lines (53 loc) · 1.25 KB
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
package geheim
import (
"crypto/aes"
"crypto/cipher"
"io"
"golang.org/x/crypto/chacha20"
)
type Cipher int
const (
AES_256_CTR Cipher = 1 + iota
ChaCha20
)
var CipherNames = map[Cipher]string{
AES_256_CTR: "AES-256-CTR",
ChaCha20: "ChaCha20",
}
var nonceSizes = map[Cipher]int{
AES_256_CTR: aes.BlockSize,
ChaCha20: chacha20.NonceSize,
}
var keySizesCipher = map[Cipher]int{
AES_256_CTR: 32,
ChaCha20: chacha20.KeySize,
}
var ciphers = [...]Cipher{
AES_256_CTR,
ChaCha20,
}
var CipherString = getOptionString(ciphers[:], CipherNames)
var newCTR = cipher.NewCTR
func newCipherStream(cipher Cipher, key, nonce []byte) (cipher.Stream, error) {
if err := checkBytesSize(nonceSizes, cipher, nonce, "nonce"); err != nil {
return nil, err
}
switch cipher {
case AES_256_CTR:
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return newCTR(block, nonce), nil
case ChaCha20:
stream, err := chacha20.NewUnauthenticatedCipher(key, nonce)
return stream, err
}
return nil, ErrCipher
}
func newStreamReader(stream cipher.Stream, r io.Reader) io.Reader {
return &cipher.StreamReader{S: stream, R: r}
}
func newStreamWriter(stream cipher.Stream, w io.Writer) io.Writer {
return &cipher.StreamWriter{S: stream, W: w}
}