-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymmetric.go
More file actions
31 lines (26 loc) · 1.26 KB
/
symmetric.go
File metadata and controls
31 lines (26 loc) · 1.26 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
package ecies
import (
"io"
)
type SymmetricCipherMode struct {
Encryptor func(io.Reader, *ECIESParams, []byte, []byte, []byte) ([]byte, error)
Decryptor func(io.Reader, *ECIESParams, []byte, []byte, []byte) ([]byte, error)
}
// Generate an initialisation vector for CTR mode.
func GenerateIV(length int, rand io.Reader) (iv []byte, err error) {
iv = make([]byte, length)
_, err = io.ReadFull(rand, iv)
return
}
// SymmetricEncrypt encrypts the `plaintext` bytes with a symmetric cipher
// specified by `params` using key `key` and optionally authenticates
// the message with `authenticationData` (for AEAD ciphers (GCM) only)
func SymmetricEncrypt(rand io.Reader, params *ECIESParams, key, plaintext, authenticationData []byte) ([]byte, error) {
return params.SymmetricCipherMode.Encryptor(rand, params, key, plaintext, authenticationData)
}
// SymmetricDecrypt decrypts the `ciphertext` bytes with a symmetric cipher
// specified by `params` using key `key` and optionally authenticates
// the message with `authenticationData` (for AEAD ciphers (GCM) only)
func SymmetricDecrypt(rand io.Reader, params *ECIESParams, key, ciphertext, authenticationData []byte) ([]byte, error) {
return params.SymmetricCipherMode.Decryptor(rand, params, key, ciphertext, authenticationData)
}